public class HBaseMapTest extends Configured implements Tool {
private static Configuration conf;
public static class RandomReadMapper extends
Mapper<Object, Text, Text, Text> {
HTable testTable = null;
public void setup(Context context) throws IOException,
InterruptedException {
super.setup(context);
try {
testTable = new HTable(context.getConfiguration(),""); //获取table
} catch (Exception e) {
System.out.println("Error Info: " + e.getMessage());
}
}
public void map(Object arg0, Text arg1, Context context)
throws IOException, InterruptedException {
}
}
public int run(String[] args) throws Exception {
conf = getConf();
HBaseConfiguration.merge(conf, HBaseConfiguration.create(conf)); //合并 jobconf hbase_conf
final Job job = new Job(conf, "HBaseMRTest");
job.setJarByClass(HBaseMapTest.class);
job.setMapperClass(RandomReadMapper.class);
job.setNumReduceTasks(0);
job.setSpeculativeExecution(false);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(job, inDir);
FileOutputFormat.setOutputPath(job, outDir);
TableMapReduceUtil.addDependencyJars(job); //解决jar包 依赖
TableMapReduceUtil.addDependencyJars(job.getConfiguration(),
org.apache.hadoop.hbase.util.Bytes.class);
TableMapReduceUtil.initCredentials(job); //给job赋予访问hbase的权限
job.waitForCompletion(true);
return 0;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new HBaseMapTest(), args);
System.exit(res);
}
}