Java tutorial
/** * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.intropro.prairie.format.seq; import com.intropro.prairie.format.OutputFormatWriter; import org.apache.commons.io.IOUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.SequenceFile; import org.apache.hadoop.io.SequenceFile.Writer; import org.apache.hadoop.io.Text; import java.io.*; /** * Created by presidentio on 10/11/15. */ public class SequenceFormatWriter implements OutputFormatWriter<String> { private OutputStream outputStream; private Writer writer; private Text text = new Text(); private File tmpFile; SequenceFormatWriter(OutputStream outputStream) { this.outputStream = outputStream; } @Override public void write(String line) throws IOException { if (writer == null) { tmpFile = File.createTempFile("seq-", ".dat"); writer = SequenceFile.createWriter(new Configuration(), Writer.file(new Path(tmpFile.toURI())), Writer.keyClass(NullWritable.class), Writer.valueClass(Text.class)); } text.set(line); writer.append(NullWritable.get(), text); } @Override public void close() throws IOException { writer.close(); InputStream inputStream = new FileInputStream(tmpFile); IOUtils.copy(inputStream, outputStream); inputStream.close(); outputStream.close(); tmpFile.delete(); } }