编写mapreduce实现HBase中数据的迁移
来源:互联网 发布:linux 查看网络通不通 编辑:程序博客网 时间:2024/06/05 17:40
需求:将HBase中user表的列族为info,列名为name和age的数据迁移到另一个表basic中
代码如下
package com.bpf.hbase;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.HBaseConfiguration;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.util.Tool;import org.apache.hadoop.util.ToolRunner;public class User2Basic extends Configured implements Tool { //Mapper Class public static class ReadUserMapper extends TableMapper<Text, Put> { private Text mapOututKey = new Text(); @Override public void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException { //get rowkey String rowkey = Bytes.toString(key.get()); mapOututKey.set(rowkey); Put put = new Put(key.get()); //iterator for(Cell cell : value.rawCells()) { // family:info if("info".equals(Bytes.toString(CellUtil.cloneFamily(cell)))){ //column:name if("name".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))) { put.add(cell); } //column:name if("age".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))) { put.add(cell); } } } context.write(mapOututKey, put); } } //Reducer Class public static class WriteBasicReducer extends TableReducer<Text, Put, ImmutableBytesWritable> { @Override protected void reduce(Text key, Iterable<Put> values, Context context) throws IOException, InterruptedException { for (Put put : values) { context.write(null, put); } } } //driver public int run(String[] arg0) throws Exception { //create job Job job = Job.getInstance(this.getConf(),this.getClass().getSimpleName()); //set run job class job.setJarByClass(this.getClass()); //set job Scan scan = new Scan(); scan.setCaching(500); // 1 is the default in Scan, which will be bad for MapReduce jobs scan.setCacheBlocks(false); // don't set to true for MR jobs // set other scan attrs //set input and mapper TableMapReduceUtil.initTableMapperJob( "user", // input table scan, // Scan instance to control CF and attribute selection ReadUserMapper.class, // mapper class Text.class, // mapper output key Put.class, // mapper output value job); //set output and reducer TableMapReduceUtil.initTableReducerJob( "basic", // output table WriteBasicReducer.class, // reducer class job); job.setNumReduceTasks(1); // at least one, adjust as required boolean isSuccess = job.waitForCompletion(true); return isSuccess ? 0 : 1; } public static void main(String[] args) throws Exception { //get configuration Configuration conf = HBaseConfiguration.create(); //submit job int status = ToolRunner.run(conf, new User2Basic(), args); //exit program System.exit(status); }}打包后(不需要将jar包打进去)上传到HADOOP环境中运行
阅读全文
0 0
- 编写mapreduce实现HBase中数据的迁移
- hbase数据迁移的mapreduce代码
- mapreduce中实现对hbase中表数据的添加
- mapreduce中实现对hbase中表数据的添加
- HBase数据迁移(3)-自己编写MapReduce Job导入数据
- HBase数据迁移(3)-自己编写MapReduce Job导入数据
- HBase数据迁移(3)-自己编写MapReduce Job导入数据
- HBase数据迁移(3)-自己编写MapReduce Job导入数据
- HBase数据迁移(3)-自己编写MapReduce Job导入数据 .
- hbase 下mapreduce 读取hbase中数据
- sqoop1.4.6实现Mysql与HDFS/Hbase的数据迁移
- 通过MapReduce程序实现对hbase表数据的插入
- hbase通过mapreduce进行数据导入迁移报错
- MapReduce中实现对HBase中表的操作一
- MapReduce中实现对HBase中表的操作二
- 在不同hbase集群中迁移数据
- mapreduce输出数据存入HBase中
- 在MapReduce中连接Hbase数据
- structs2
- 【C++】关于typedef的用法总结
- bgpdump的安装与使用
- 单选按钮
- Struts2 S2-052
- 编写mapreduce实现HBase中数据的迁移
- 关于priority_queue的运用(两道例题:序列合并 + 最小函数值)
- Apache防止图片盗链的配置规则
- resultType之二redirect重定向
- 对oracle的临时表空间的增删查改
- redis学习系列(九)--redis-AOF和RDB实践
- pascals-triangle Java code
- 进程间通信———消息队列
- java实现文件下载