【代码样板】HBase 数据导入处理

一、Shell 交互式命令行

bin/hbase shell
帮助命令 help ‘create’

二、使用API操作HBase

eg:插入数据

		//1、读取配置信息
        Configuration conf = HBaseConfiguration.create();
        //System.out.println(conf);
        Connection conn = null;
        Table table = null;
        try {
            //2.获取连接
            conn = ConnectionFactory.createConnection( conf );
            //3.获取HBASE table的句柄,可以对表中的数据进行CURD操作
            table = conn.getTable( TableName.valueOf( "ns2:stu_info" ) );
            putData(table);
            delete(table);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            IOUtils.closeStream( table );
            IOUtils.closeStream( conn);
        }
	/**
     *  向表中插入数据,
     *  使用场景:一条记录(一行数据)的所有列一并存储到HBase中
     *      添加用户(id,name,age)
     */
    private static void putData(Table table) {
        //模拟数据
        HashMap<String, String> stuMap = new HashMap<>();
        stuMap.put( "id","1122" );
        stuMap.put( "name","zs22" );
        stuMap.put( "age","12" );
        stuMap.put( "address","zhejaingjiaxing22" );
        //使用put插入数据
        //put "ns2:stu_info" rowkey,"CF:Column"  值
        Put put = new Put(Bytes.toBytes( "100001" ));
        byte [] cf = Bytes.toBytes( "info" );
        for (Map.Entry<String,String> entry:stuMap.entrySet()) {
            put.addColumn( cf, Bytes.toBytes( entry.getKey() ), Bytes.toBytes( entry.getValue() )   );
        }
        try {
            //将数据插入到表中:单条数据插入
            table.put( put );
            //多条数据插入
            //void put(List<Put> puts)
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

三、使用sqoop导入数据

eg1:表不存在

--将RDBMS中数据导入HBASE表中
bin/sqoop import \
--connect jdbc:mysql://192.168.59.200/db_orders \
--username root \
--password 123456 \
--table so \
--columns "date,order_id,user_id,order_amt" \
--hbase-table "ns1:orders" \
--column-family info \
--hbase-row-key order_id \
--num-mappers 1 \
--hbase-create-table 

eg2:表存在

bin/sqoop import \
--connect jdbc:mysql://192.168.59.200/db_orders \
--username root \
--password 123456 \
--table so_detail \
--columns "date,id,order_id,product_id,user_id,price,product_num,id_new,rk" \
--hbase-table "ns1:sale_orders_detail" \
--column-family info \
--hbase-row-key rk \
--num-mappers 2 \
--split-by rk

四、从文件(HDFS)导入

eg1: 直接导入hbase表中

#!/bin/sh
HADOOP_HOME=/opt/cdh5.7.6/hadoop-2.6.0-cdh5.7.6
HBASE_HOME=/opt/cdh5.7.6/hbase-1.2.0-cdh5.7.6

HADOOP_CLASSPATH=`${HBASE_HOME}/bin/hbase mapredcp`:${HBASE_HOME}/conf  \
${HADOOP_HOME}/bin/yarn jar  \
${HBASE_HOME}/lib/hbase-server-1.2.0-cdh5.7.6.jar  \
importtsv \
-Dimporttsv.columns=order:date,order:orderId,order:userId,order:orderAmt,HBASE_ROW_KEY \
-Dimporttsv.separator=, \
orders:history_orders \
/sale_orders.csv

eg2: 直接写入HFile,不经过MemStore

HADOOP_CLASSPATH=`${HBASE_HOME}/bin/hbase mapredcp`:${HBASE_HOME}/conf  \
${HADOOP_HOME}/bin/yarn jar  \
${HBASE_HOME}/lib/hbase-server-1.2.0-cdh5.7.6.jar  \
importtsv \
-Dimporttsv.columns=order:date,order:orderId,order:userId,order:orderAmt,HBASE_ROW_KEY \
-Dimporttsv.separator=, \
#HFile文件的存放目录
-Dimporttsv.bulk.output=/datas/hfile-output \
#如果有多个Task在运行,其中一个还有完成,推测可能是因为资源的原因
#在其他机器上也启动该任务,2个机器同时运行这个任务,谁先完成,用谁的结果
-Dmapreduce.map.speculative=false \
-Dmapreduce.reduce.speculative=false \
orders:history_orders1 \
#文件在HDFS中的路径
/sale_orders.csv

HADOOP_CLASSPATH=`${HBASE_HOME}/bin/hbase mapredcp`:${HBASE_HOME}/conf  \
${HADOOP_HOME}/bin/yarn jar  \
${HBASE_HOME}/lib/hbase-server-1.2.0-cdh5.7.6.jar  \
completebulkload \
/datas/hfile-output  orders:history_orders1
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值