MapReduce on Hbase
短信预约 -IT技能 免费直播动态提醒
org.apache.hadoop.hbase.mapreduce
TableMapper TableReducer
一个region对应一个map
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
public class HbaseMR {
public class MyMapper extends TableMapper<Text, Text> {
@Override
protected void map(ImmutableBytesWritable key, Result value,
Context context) throws IOException, InterruptedException {
// key代表rowkey
Text k = new Text(Bytes.toString(key.get()));
Text v = new Text(Bytes.toString(value.getValue(
"basicinfo".getBytes(), "age".getBytes())));
context.write(v, k);
}
}
public class MyReducer extends TableReducer<Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Put put = new Put(Bytes.toBytes(key.toString()));
for (Text value : values) {
put.add(Bytes.toBytes("f1"), Bytes.toBytes(value.toString()),
Bytes.toBytes(value.toString()));
}
context.write(null, put);
}
}
public static void main(String[] args) {
Configuration conf= HBaseConfiguration.create();
try {
Job job=new Job(conf, "mapreduce on hbase");
job.setJarByClass(HbaseMR.class);
Scan scan=new Scan();
scan.setCaching(1000);//
TableMapReduceUtil.initTableMapperJob("students", scan, MyMapper.class, Text.class, Text.class, job);
TableMapReduceUtil.initTableReducerJob("student-age", MyReducer.class, job);
job.waitForCompletion(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341