Reading SeqeunceFile hadoop mapreduce

Reading SeqeunceFile hadoop mapreduce , You have created a sequence using sequence writer,
once done now you want to check whether sequence file in hadoop created successfully or 
not by reading the sequence file present in HDFS

Input to the program is the location of the Sequence File in Hadoop HDFS.

package org.convert.hadoop;

import java.io.IOException;
import java.net.URI;
import java.util.List;

import mrdp.logging.LogWriter;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocalFileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.ByteWritable;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.util.ReflectionUtils;

public class SequenceFileReadDemo {

/** Krishna - Reading a Sequence File from HDFS
* @param args
*/

public static void main(String[] args) throws IOException {
String uri = args[0];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf);
Path path = new Path(uri);
SequenceFile.Reader reader = null;
try {
reader = new SequenceFile.Reader(fs, path, conf);
Writable key = (Writable) ReflectionUtils.newInstance(reader.getKeyClass(), conf);
Writable value = (Writable) ReflectionUtils.newInstance(reader.getValueClass(), conf);
long position = reader.getPosition();
while (reader.next(key, value)) {
String syncSeen = reader.syncSeen() ? "*" : "";
System.out.printf("[%s%s]\t%s\t%s\n", position, syncSeen, key, value);
// LogWriter.getInstance().WriteLog("key : " + key +
// " - value : " + value);
position = reader.getPosition(); // beginning of next record

}
} finally {
IOUtils.closeStream(reader);
}
}
}

You can run this program by using hadoop jar [location of the jar] [packagename.classname]

Leave a Reply