1、把txt文件传到hdfs上
数据以tab键分隔
2、map端
public class ReadFruitFromHDFSMapper extends Mapper<LongWritable, Text, Text, Put>{
@SuppressWarnings("deprecation")
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
//从hdfs上读取数据
String line = value.toString();
//读取出来的每行数据使用\t 进行分割,存于 String 数组
String[] split = line.split("\t");
String rowkey=split[0];
String name = split[1];
String age = split[2];
Put put = new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes(name));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(age));
context.write(new Text(rowkey), put);
}
3、reduce端
public class WriteFruitMRFromTxtReducer extends TableReducer<Text, Put, NullWritable>{
@Override
protected void reduce(Text key, Iterable<Put> values, Context context)
throws IOException, InterruptedException {
//读出来的每一行数据写入到 fruit_hdfs 表中
for(Put put: values) {
context.write(NullWritable.get(), put);
}
}
4、driver端
public class M {
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","slm001");
conf.set("hbase.zookeeper.property.clientPort","2181");
//conf.set(TableOutputFormat.OUTPUT_TABLE, "student");
Job job = Job.getInstance(conf, M.class.getSimpleName());
// TableMapReduceUtil.addDependencyJars(job);
//这个是实现在Hbase中建好的表
TableMapReduceUtil.initTableReducerJob("student", WriteFruitMRFromTxtReducer.class, job);
job.setJarByClass(M.class);
job.setMapperClass(ReadFruitFromHDFSMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Put.class);
FileInputFormat.addInputPath(job, new Path("hdfs://slm001:9000/slm.txt"));
job.setOutputFormatClass(TableOutputFormat.class);
job.waitForCompletion(true);
}
运行结果