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 <<EOF
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 <<EOF
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" <HBase Table Name> <ElasticSearch Type Name>"
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 <<EOF
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 <<EOF
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" <HBase Table Name>"
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 查看