1.問題
有多個表需要導(dǎo)入HBase及es中猛计,在HBase中創(chuàng)建多個表沒有問題唠摹,但是通過HBase Observer同步數(shù)據(jù)到es中,多個索引/類型的映射奉瘤,ElasticSearch會串?dāng)?shù)據(jù)勾拉,以最后的table-att為準(zhǔn)
2.解決建議
- 為每個表復(fù)制生成不同的jar包路徑,Observer加載不同的jar包盗温。
- ES中多表可以區(qū)分不同的index藕赞、type。
- 但是卖局,問題是單個jar包有77mb斧蜕,如果導(dǎo)入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 "參數(shù)輸入有誤批销,請確認(rèn)后重新運(yùn)行。"
echo "Usage: sh "$0" <HBase Table Name> <ElasticSearch Type Name>"
fi
3.思路改變
- 多個表都導(dǎo)入到同一個es的index和type中染坯,通過修改id來區(qū)分均芽。
現(xiàn)在id是各個表的主鍵(1),修改成 庫名-表名-主鍵形式(database1-table1-1)单鹿。 - 多個表在ES中index掀宋、type都相同,通過id前綴區(qū)分仲锄。
- 多個表都用同一個jar包劲妙,節(jié)省空間
4.如何實現(xiàn)修改id
- sqoop導(dǎo)入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.實現(xiàn)sqoop修改rowKey
5.1 修改 --hbase-row-key 參數(shù)
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 "參數(shù)輸入有誤,請確認(rèn)后重新運(yùn)行掸驱。"
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導(dǎo)入數(shù)據(jù)
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 查看