How to create hadoop sequence file in local file s

2019-07-21 02:24发布

问题:

Is it possible to create hadoop sequence file from java only without installing hadoop? I need a standalone java program that create sequence file locally. My java program will run in env that does not have hadoop install.

回答1:

You would need the libraries but not the installation. Use

SequenceFile.Writer

Sample code :

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
public class SequenceFileCreator {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Configuration config = new Configuration();
        FileSystem fs = FileSystem.get(config);

        SequenceFile.Writer writer = new SequenceFile.Writer(fs, config, new Path("LocalPath"), NullWritable.class, Text.class);
        writer.append(NullWritable.get(), new Text(""));
        writer.close();
    }

}