Skip to content

Instantly share code, notes, and snippets.

@woodywang
Created May 20, 2013 01:49
Show Gist options
  • Save woodywang/5609958 to your computer and use it in GitHub Desktop.
Save woodywang/5609958 to your computer and use it in GitHub Desktop.
An example for HFileInputFormat
package com.woodywang;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class App {
private static class HFileMapper extends Mapper<ImmutableBytesWritable, KeyValue, Text, Text> {
public void map(ImmutableBytesWritable key, KeyValue keyValue, Context context) throws IOException, InterruptedException {
Text row = new Text(key.get());
String column = Bytes.toString(keyValue.getQualifier());
String value = Bytes.toString(keyValue.getValue());
Text outputValue = new Text(column + ": " + value);
context.write(row, outputValue);
}
}
public static void main(String[] args) throws Exception {
String path = "/Users/woody/hfile";
Job job = new Job();
job.setMapperClass(HFileMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setNumReduceTasks(0);
job.setInputFormatClass(HFileInputFormat.class);
FileInputFormat.addInputPath(job, new Path(path));
FileOutputFormat.setOutputPath(job, new Path("/tmp/hfile"));
job.waitForCompletion(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment