1. 程式人生 > >Hbase二級索引+CDH+Lily

Hbase二級索引+CDH+Lily

1.更改表結構,允許複製

已存在的表
disable 'tableName'
alter 'tableName',{NAME =>'fn', REPLICATION_SCOPE =>1}
enable 'tableName'
不存在的表
create ‘table‘,{NAME =>‘cf‘, REPLICATION_SCOPE =>1}
#其中1表示開啟replication功能,0表示不開啟,預設為0

2.建立相應的SolrCloud集合
接下來在安裝有Solr的機器上執行
這裡得路徑和使用者名稱都可以自己定義

# 生成實體配置檔案:
 solrctl instancedir --generate /opt/hbase-indexer/index1
 # index1 意思代表使用者

此時/opt/hbase-indexer/index1目錄下會有個conf資料夾,我們修改下面得schema.xml檔案.
在最下面新加一個欄位

<field name="HBase_Indexer_Test_cf1_name" type="string" indexed="true" stored="true"/>
屬性解析:
name:這裡的name是自定義,但是後面要使用到,要和後面的Morphline.conf檔案中的outputField屬性對應。
type:欄位型別
indexed:是否建立索引
stored:是否儲存

注意:這裡name欄位它對應了我們後續需要修改Morphline.conf檔案中的outputField屬性。因此可以看成是hbase中需要建立索引的值。因此我們建議將其與表名和列族結合,格式建議如下:

HBase_Indexer_ZDTable_fn_name
Hbase_indexer_表名_列簇_列名

再修改solrconfig.xml,找到下面的配置將false改為true,這個是硬提交,會影響效能

    <autoCommit>
       <maxTime>${solr.autoCommit.maxTime:60000}</maxTime>
       <openSearcher>true</openSearcher>
     </autoCommit>

3.建立 collection例項並將配置檔案上傳到 zookeeper:

solrctl instancedir --create index1 /opt/hbase-indexer/index1

4.上傳到 zookeeper之後,其他節點就可以從zookeeper下載配置檔案。接下來建立 collection:

solrctl collection --create index1

如果希望將資料分散到各個節點進行儲存和檢索,則需要建立多個shard,需要使用如下命令

    solrctl collection --create bqjr -s 7-r 3-m 21

其中-s表示設定Shard數為7,-r表示設定的replica數為3,-m表示最大shards數目(7*3)

5.建立 Lily HBase Indexer 配置

[[email protected] index1]# cat morphline-hbase-mapper.xml 
<?xml version="1.0"?>
<!-- table:需要索引的HBase表名稱-->
<!-- mapper:用來實現和讀取指定的Morphline配置檔案類,固定為MorphlineResultToSolrMapper-->
<indexer table="zh_ams_ns:zhongda_custom_task_cp" mapper="com.ngdata.hbaseindexer.morphline.MorphlineResultToSolrMapper" read-row="never" >
<!--param中的name引數用來指定當前配置為morphlineFile檔案 -->
<!--value用來指定morphlines.conf檔案的路徑,絕對或者相對路徑用來指定本地路徑,如果是使用Cloudera Manager來管理morphlines.conf就直接寫入值morphlines.conf"-->
    <param name="morphlineFile" value="morphlines.conf"/>
<!--value="ZDTableMap",這裡test3Map是自定義,接下來要使用。其他的mapper,param name等屬性預設即可-->
    <param name="morphlineId" value="ZDTableMap"/>
</indexer>

6.配置Morphline
通過CM頁面進入到Key-Value Store Indexer的配置頁面,裡面有一個Morphlines檔案。修改它:

修改完後重啟Key-Value Store服務

SOLR_LOCATOR : {
  # Name of solr collection
  collection : hbaseindexer
  
  # ZooKeeper ensemble
  zkHost : "$ZK_HOST" 
}


morphlines : [
{
#與morphline-hbase-mapper中value相同
id : ZDTableMap
importCommands : ["org.kitesdk.**", "com.ngdata.**"]

commands : [                    
  {
    extractHBaseCells {
      mappings : [
        {
          inputColumn : "fn:name"
          outputField : "HBase_Indexer_ZDTable_fn_name" 
          type : string 
          source : value
        }
      ]
    }
  }


  { logDebug { format : "output record: {}", args : ["@{}"] } }
]
}
]

注:

id:表示當前morphlines的名稱,與上一步的value="test3Map"要一致
importCommands:需要引入的命令包地址
extractHBaseCells:該命令用來讀取HBase列資料並寫入到SolrInputDocument物件中,該命令必須包含零個或者多個mappings命令物件。
mappings:用來指定HBase列限定符的欄位對映。
inputColumn:需要寫入到solr中的HBase列欄位。值包含列族和列限定符,並用‘ : ’分開。其中列限定符也可以使用萬用字元*來表示,譬如可以使用c1:*表示讀取只要列族為data的所有hbase列資料,也可以通過c1:na*來表示讀取列族為c1列限定符已na開頭的欄位值.
outputField:用來表示morphline讀取的記錄需要輸出的資料欄位名稱,該名稱必須和solr中的schema.xml檔案的field節點自定義的name名稱保持一致,否則寫入不正確
type:用來定義讀取HBase資料的資料型別,HBase中的資料都是以byte[]的形式儲存,但是所有的內容在Solr中索引為text形式,所以需要一個方法來把byte[]型別轉換為實際的資料型別。type引數的值就是用來做這件事情的。現在支援的資料型別有:byte,int,long,string,boolean,float,double,short和bigdecimal。當然你也可以指定自定的資料型別,只需要實現com.ngdata.hbaseindexer.parse.ByteArrayValueMapper介面即可
source:用來指定HBase的KeyValue那一部分作為索引輸入資料,可選的有‘value’和'qualifier',當為value的時候表示使用HBase的列值作為索引輸入,當為qualifier的時候表示使用HBase的列限定符作為索引輸入

7.註冊 Lily HBase Indexer Configuration 和 Lily HBase Indexer Service

hbase-indexer add-indexer \
--name ZDindexer \
--indexer-conf /opt/hbase-indexer/index1/morphline-hbase-mapper.xml --connection-param solr.zk=test110:2181,test115:2181,test119:2181/solr \
--connection-param solr.collection=index1 \
--zookeeper test110:2181,test115:2181,test119:2181

執行hbase-indexer list-indexers檢視新增成功,
此時新增資料已經可以通過solr查詢
8.批量同步索引
在執行命令的目錄下必須有morphlines.conf檔案,執行

find / |grep morphlines.conf$

一般我們選擇最新的那個process
cd 進入目錄

hadoop --config /etc/hadoop/conf \
jar /opt/cloudera/parcels/CDH/lib/hbase-solr/tools/hbase-indexer-mr-1.5-cdh5.14.0-job.jar  \
--conf /etc/hbase/conf/hbase-site.xml \
--hbase-indexer-file /opt/hbase-indexer/index1/morphline-hbase-mapper.xml \
--zk-host test110:2181,test115:2181,test119:2181/solr \
--collection index1 \
--reducers 0 \
--go-live