import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.
mapreduce.TableMapReduceUtil;
// 通过新API载入HBASE数据
public class NewLoadToHBase {
// Map内部类
public static class Map extends Mapper{
private Configuration conf = null;
private HTable table = null;
@Override
protected void setup(Context context)throws IOException, InterruptedException{
super.setup(context);
conf = context.getConfiguration(); // 在map任务执行前将conf设置好
HBaseConfiguration.
addHbaseResources(conf); // 重要!没有会出现 java.io.IOException: Unable to determine ZooKeeper ensemble
try{
this.table = new HTable(conf , "observations"); // 构建HBase中的表对象,向这个表中载入数据
}catch(IOException e){
throw new RuntimeException("Failed HTable construction", e);
}
}
// 在map任务执行结束时,释放相关资源
@Override
protected void cleanup(Context context) throws IOException, InterruptedException{
super.cleanup(context);
table.close();
}
// 以下是map方法,主要完成 数据的处理,并写入HBase
public void map(LongWritable key, Text value, Context context) throws IOException{
String[] valuelist = value.toString().split("\t");
String uid = valuelist[0];
byte[] rowKey = uid.getBytes();
Put p = new Put(rowKey);
p.add("content".getBytes(), "attr1".getBytes(), "testvalue".getBytes());
table.put(p);
}
}
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if(otherArgs.length != 1){
System.err.println("Usage: wordcount ");
System.exit(2);
}
Job job = new Job(conf, "NewLoadToHBase!");
TableMapReduceUtil.
addDependencyJars(job);
job.setJarByClass(
NewLoadToHBase.class);
job.setMapperClass(Map.class);
job.setNumReduceTasks(0);
job.setInputFormatClass(
TextInputFormat.class);
job.setOutputFormatClass(
NullOutputFormat.class);
FileInputFormat.addInputPath(
job, new Path(otherArgs[0]));
System.exit(job.
waitForCompletion(true) ? 0 : 1);
}
}
参考:
https://groups.google.com/forum/?fromgroups=#!topic/hadoopors/V_woQid4FYc