hbase 导入到es_sqoop导入多表,hbase存es的数据串表解决

本文介绍了在HBase中导入多表到Elasticsearch时遇到的数据串表问题及其解决方法。通过为每个表生成不同jar包、使用相同的index和type并修改id前缀以区分数据。同时,提供了利用sqoop修改rowKey的方案,包括源码修改和打包,并给出了测试步骤。
摘要由CSDN通过智能技术生成

1.问题

有多个表需要导入HBase及es中,在HBase中创建多个表没有问题,但是通过HBase Observer同步数据到es中,多个索引/类型的映射,ElasticSearch会串数据,以最后的table-att为准

2.解决建议

为每个表复制生成不同的jar包路径,Observer加载不同的jar包。

ES中多表可以区分不同的index、type。

但是,问题是单个jar包有77mb,如果导入100个表,jar包占用空间是个问题。

2.1 生成建表脚本

#!/bin/bash

htablename=$1

estypename=$2

if [ $# -eq 2 ]; then

date

echo `hbase shell <

exists $htablename

quit

EOF` | grep 'does exist' >/dev/null 2>&1

rtstatus=$?

if [ $rtstatus -ne 0 ]; then

echo "copying new observer.jar to observer-"$htablename".jar..."

hdfs dfs -cp /hbase/lib/observer.jar /hbase/lib/observer-$htablename.jar

echo $htablename" hbase-table is creating and configuring..."

`hbase shell <

create '$htablename','data'

disable '$htablename'

alter '$htablename', METHOD => 'table_att', 'coprocessor' => 'hdfs:///hbase/lib/observer-$htablename.jar|com.jusfoun.data.DataSyncObserver|1001|es_cluster=elas2.3.4,es_type=$estypename,es_index=hbase,es_port=9300,es_host=localhost--127.0.0.1'

enable '$htablename'

quit

EOF` >/dev/null 2>&1

echo $htablename" hbase-table is successed for create and configure."

else

echo "hbase-table already exists for name:"$htablename

fi

date

else

echo "参数输入有误,请确认后重新运行。"

echo "Usage: sh "$0" "

fi

3.思路改变

多个表都导入到同一个es的index和type中,通过修改id来区分。

现在id是各个表的主键(1),修改成 库名-表名-主键形式(database1-table1-1)。

多个表在ES中index、type都相同,通过id前缀区分。

多个表都用同一个jar包,节省空间

4.如何实现修改id

sqoop导入hbase时,修改hbase的rowKey

bin/sqoop import --connect jdbc:oracle:thin:@192.168.16.223:1521/orcl --username sitts --password password --table SITTS.ESB_SERVICE_PARAM --split-by PARAM_ID --hbase-table test --hbase-row-key PARAM_ID --column-family data

修改 –hbase-row-key 的值,在主键前加上String类型的库名-表名。

Observer同步es时,修改es的id

String indexId = new String(put.getRow());

ElasticSearchOperator.addUpdateBuilderToBulk(client.prepareUpdate(Config.indexName, Config.typeName, indexId).setDoc(json).setUpsert(json));

修改 indexId 在主键前加上String类型的库名-表名。

5.实现sqoop修改rowKey

5.1 修改 –hbase-row-key 参数

bin/sqoop import -D sqoop.hbase.add.row.key=true --connect jdbc:oracle:thin:@192.168.16.223:1521/orcl --username sitts --password password --table SITTS.ESB_SERVICE_PARAM --split-by PARAM_ID --hbase-table test --hbase-row-key orcl,SITTS_ESB_SERVICE_PARAM,PARAM_ID --column-family data

5.2 异常:

不支持String直接生成组合rowKey

16/09/12 19:32:05 INFO mapreduce.Job: Task Id : attempt_1473133321920_0086_m_000000_2, Status : FAILED

Error: java.io.IOException: Could not insert row with null value for row-key column: name

at org.apache.sqoop.hbase.ToStringPutTransformer.getPutCommand(ToStringPutTransformer.java:146)

5.3 源码修改:

使得sqoop支持String混合主键生成rowKey。

package org.apache.sqoop.hbase;

ToStringPutTransformer.class

line 122

Object fieldValue = fields.get(fieldName);

if (null == fieldValue) {

// If the row-key column value is null, we don't insert this row.

// throw new IOException("Could not insert row with null "

// + "value for row-key column: " + fieldName);

// lisiyu add - If the fieldValue column is null, we insert this fieldValue = fieldName.

fieldValue = fieldName;

}

line 146

Object rowKey = fields.get(rowKeyCol);

if (null == rowKey) {

// // If the row-key column is null, we don't insert this row.

// throw new IOException("Could not insert row with null "

// + "value for row-key column: " + rowKeyCol);

// lisiyu add - If the row-key column is null, we insert this rowKey = rowKeyCol.

rowKey = rowKeyCol;

}

5.4 打包并上传jar包

ant jar

sqoop源码打包成一个Jar放到你的$HADOOP_HOME/lib下

6. 测试

6.1 做了一个建表脚本

#!/bin/bash

htablename=$1

if [ $# -eq 1 ]; then

date

echo `hbase shell <

exists $htablename

quit

EOF` | grep 'does exist' >/dev/null 2>&1

rtstatus=$?

if [ $rtstatus -ne 0 ]; then

echo $htablename" hbase-table is creating and configuring..."

`hbase shell <

create '$htablename','data'

disable '$htablename'

alter '$htablename', METHOD => 'table_att', 'coprocessor' => 'hdfs:///hbase/lib/observer-$htablename.jar|com.jusfoun.data.DataSyncObserver|1001|es_cluster=elas2.3.4,es_type=data,es_index=hbase,es_port=9300,es_host=localhost--127.0.0.1'

enable '$htablename'

quit

EOF` >/dev/null 2>&1

echo $htablename" hbase-table is successed for create and configure."

else

echo "hbase-table already exists for name:"$htablename

fi

date

else

echo "参数输入有误,请确认后重新运行。"

echo "Usage: sh "$0" "

fi

6.2 建两个表

sh create-hbase-table.sh test

sh create-hbase-table.sh test1

6.3 用sqoop导入数据

bin/sqoop import -D sqoop.hbase.add.row.key=true --connect jdbc:oracle:thin:@192.168.16.223:1521/orcl --username sitts --password password --table SITTS.ESB_SERVICE_PARAM --split-by PARAM_ID --hbase-table test --hbase-row-key name,PARAM_ID --column-family data

bin/sqoop import -D sqoop.hbase.add.row.key=true --connect jdbc:oracle:thin:@192.168.16.223:1521/orcl --username sitts --password password --table SITTS.ESB_FLOW --split-by FLOW_ID --hbase-table test1 --hbase-row-key name,FLOW_ID --column-family data

6.4 校验

ES head 查看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值