博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sqoop 补充
阅读量:4315 次
发布时间:2019-06-06

本文共 3543 字,大约阅读时间需要 11 分钟。

1、用 sqoop 将MySQL中的数据导入hbase中    

sqoop import \

--connect jdbc:mysql://***.***.*.***:3306/mysql \
--hbase-table Nbigdata \
--column-family gps \
--hbase-row-key id \
--username root \
--password 123456 \
--table ceshi

--compression-codec lzo -z      ( -z 表示采用压缩方式)

mysql :数据库中的数据库名          Nbigdata:导入数据到hbase 中的表名     gps:列簇名   id:mysql中作为rowkey的字段名   ceshi:mysql表名                        username ,password :连接MySQL的用户名和密码

如果 没有 指定--hbase-row-key id ,会自动用MySQL的第一列作为rowkey

2、用 sqoop 将MySQL中的数据导入hbase时,用MySQL中多个字段连接起来作为rowkey

   第一种方法:

    在MySQL表中插入一列 (列名:rowkey):  update mysqltable set rowkey=姓名_出生年月_地点

   再用sqoop语句导入

   第二种方法:

   扩展PutTransformer类,自定义导入HBase的put格式。并在执行导入命令时指定该类

   操作步骤:

package com.gamewave.sqoop.extensions.tansformat_row_key; import java.io.IOException;import java.util.Collections;import java.util.List;import java.util.Map;import java.util.TreeMap;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.util.Bytes;import org.apache.sqoop.hbase.PutTransformer; public class DdtMapInfoTransFormat extends PutTransformer {   public static final Log LOG = LogFactory.getLog(DdtMapInfoTransFormat.class.getName());  private Map
serializedFieldNames; public DdtMapInfoTransFormat() { serializedFieldNames = new TreeMap
(); }
/** * Return the serialized bytes for a field name, using the cache if it's * already in there. */ private byte[] getFieldNameBytes(String fieldName) { byte[] cachedName = serializedFieldNames.get(fieldName); if (null != cachedName) { // Cache hit. We're done. return cachedName; } // Do the serialization and memoize the result. byte[] nameBytes = Bytes.toBytes(fieldName); serializedFieldNames.put(fieldName, nameBytes); return nameBytes; } @Override /** {@inheritDoc} */ public List
getPutCommand(Map
fields)throws IOException { String rowKeyCol = getRowKeyColumn(); String colFamily = getColumnFamily(); byte[] colFamilyBytes = Bytes.toBytes(colFamily); Object rowKey = fields.get(rowKeyCol); if (null == rowKey) { // If the row-key column is null, we don't insert this row. LOG.warn("Could not insert row with null value for row-key column: "+ rowKeyCol); return null; } Put put = new Put(Bytes.toBytes(rowKey.toString() + ":custom")); for (Map.Entry
fieldEntry : fields.entrySet()) { String colName = fieldEntry.getKey(); if (!colName.equals(rowKeyCol)) { // This is a regular field, not the row key. // Add it if it's not null. Object val = fieldEntry.getValue(); if (null != val) { put.add(colFamilyBytes, getFieldNameBytes(colName),Bytes.toBytes(val.toString())); } } } return Collections.singletonList(put); }}

  (1)扩展PutTransformer类

       (2)包并放进sqoop-lib目录下,执行命令

./sqoop-import -D sqoop.hbase.insert.put.transformer.class=com.gamewave.sqoop.extensions.tansformat_row_key.DdtMapInfoTransFormat –connect jdbc:mysql://xxx.xxx.xxx.xxx/service --table ddt_map_info_copy --hbase-create-table --hbase-table ddt_map_info_copy --column-family info --split-by mapId --username root --P --compression-codec lzo

命令中-D为指定属性名称 参数值为键值对

sqoop.hbase.insert.put.transformer.class属性值为指定格式化类名称

     (3)验证       

     进入hbase shell              可以查看得到rowkey后缀均为:custom,修改成功

 

 

转载于:https://www.cnblogs.com/liuwei6/p/7228085.html

你可能感兴趣的文章
poj3368 RMQ
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
Part 2 - Fundamentals(4-10)
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
CentOs7安装rabbitmq
查看>>
(转))iOS App上架AppStore 会遇到的坑
查看>>
解决vmware与主机无法连通的问题
查看>>
做好产品
查看>>
项目管理经验
查看>>
笔记:Hadoop权威指南 第8章 MapReduce 的特性
查看>>
JMeter响应数据出现乱码的处理-三种解决方式
查看>>