编写mapreduce实现HBase中数据的迁移

需求:将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环境中运行

dm.jdbc.driver.DMException是一个数据库驱动程序抛出的异常。根据引用\[1\]中的描述,你在连接数据库时遇到了网络通信异常和连接超时的问题。你尝试在达梦数据库和dbeaver中测试连接,但仍然无法解决问题。引用\[2\]提到,如果仍然报网络通信异常,你需要仔细检查username、password、driver-class-name、url中的IP地址和端口号是否正确。特别是在更换环境时容易忘记修改IP地址。因此,你可以再次检查你的连接参数,确保它们是正确的。如果问题仍然存在,可能需要进一步排查网络配置或联系数据库管理员以获取支持。 #### 引用[.reference_title] - *1* *3* [dm.jdbc.driver.DMException: 网络通信异常](https://blog.csdn.net/qq_45685740/article/details/127930182)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [达梦数据库dm.jdbc.driver.DMException: 网络通信异常](https://blog.csdn.net/qq_45415736/article/details/130088548)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值