大數據學習筆記——使用Solr建立HBASE索引

最近工作需要接觸HBase存儲數據,並對數據進行加工分析處理。在此將個人學習過程及踩過的坑記錄一下,防止個人大腦遺忘,希望在緩解個人腦容量不足的情況下,也能給他人提供幫助。

環境: 阿里雲Hbase企業版
流程: Hbase表的創建、數據插入、Solr的Collection創建、Solr索引創建、Java通過Solr訪問查詢數據。

  • Hbase表、Solr Collection、Solr索引三者之間的關係
    創建Solr索引之前要明確Hbase表、Solr Collection、Solr索引三者之間的關係。Hbase表、Solr Collection兩者是相互獨立的,均可以直接進行創建,Solr索引是兩者的中間橋樑,將二者聯繫起來,並將Hbase表中的特定Column數據映射到Solr Collection中。其關係如下圖:
    Hbase和Solr之間的關係
  • 創建Hbase表
    在Hbase所在的服務器上,下載並安裝Hbase客戶端,在Hbase目錄下,使用如下命令:
./bin/hbase shell

進入Hbase,通過

create 'solrdemo', {NAME=>'info', REPLICATION_SCOPE=>'1'}

創建solrdemo表,並創建info列簇。當然可以建立多個列簇。
在Hbase中,數據的存放方式是以Key-Value的方式存放,即列名-值。表的結構如下表所示:

ROW COL1 COL2
row1 info1 info2

其中ROW相當於Mysql中的ID,COL1表示列簇1,COL2表示列簇2。
其中每一個列簇下,可以分爲多個列,比如,COL1下可以新建多個列,info1:name,info1:age等等。
這是Hbase相關的一些知識,有興趣的可以去詳細Hbase,這裏就不細說了。

  • 創建Solr Collection
    在使用Solr之前,需要安裝配置Solr,然後才能使用網頁的方式去訪問。配置方式在這裏就不細說了,主要說一下,如何通過Solr的Web界面去創建Solr Collection。
    通過用戶密碼登陸Solr的第一個界面如下所示
    在這裏插入圖片描述
    通過此界面,可以查看Solr版本、JVM的相關配置、物理內存的使用情況以及JVM的內存使用清空。
    接下來就是建立Collection了,點擊頁面左側第四個按鈕Collections,再點擊Add Collection按鈕,出現下圖所示界面:
    在這裏插入圖片描述
    填寫,您的Collection的名稱、選擇config set,初次使用,有兩個選項:_default和_indexer_default,這個config是可以自定義創建的,本文暫時不對此做介紹,有興趣的可以自查學習。最後點擊Add Collection按鈕,就成功創建Solr Collection了。
    創建成功後,會在此界面出現您的Collection,您可以通過點擊它查看其基本信息,具體情況如下圖:
    在這裏插入圖片描述
  • 創建Hbase表和Solr Collection關聯的配置文件
    通過上面兩個步驟創建完成Hbase表和Solr Collection之後,需要配置xml配置文件,將Hbase表與Solr Collection關聯起來。並實現通過Solr查看Hbase數據。
    xml配置文件的一個樣例如下所示:
<indexer table="solrdemo">
   <field name="name_s" value="info:name" type="string" />
   <param name="update_version_l" value="true"/>
</indexer>

其中,table對應Hbase表的名稱,field中,name表示Solr Collection中顯示的字段名稱,如果使用Solr默認的config set,那麼,name 的命名需要按照一定的方式,比如此例,因爲field中,type爲string類型,則name 的命名爲name_s,其中s表示string。如果是其它類型,則需要添加不同的標誌,具體的情況,請參考下面:

    <dynamicField name="*_i"  type="pint"    indexed="true"  stored="true"/>
    <dynamicField name="*_is" type="pints"    indexed="true"  stored="true"/>
    <dynamicField name="*_s"  type="string"  indexed="true"  stored="true" />
    <dynamicField name="*_ss" type="strings"  indexed="true"  stored="true"/>
    <dynamicField name="*_l"  type="plong"   indexed="true"  stored="true"/>
    <dynamicField name="*_ls" type="plongs"   indexed="true"  stored="true"/>
    <dynamicField name="*_t" type="text_general" indexed="true" stored="true" multiValued="false"/>
    <dynamicField name="*_txt" type="text_general" indexed="true" stored="true"/>
    <dynamicField name="*_b"  type="boolean" indexed="true" stored="true"/>
    <dynamicField name="*_bs" type="booleans" indexed="true" stored="true"/>
    <dynamicField name="*_f"  type="pfloat"  indexed="true"  stored="true"/>
    <dynamicField name="*_fs" type="pfloats"  indexed="true"  stored="true"/>
    <dynamicField name="*_d"  type="pdouble" indexed="true"  stored="true"/>
    <dynamicField name="*_ds" type="pdoubles" indexed="true"  stored="true"/>
    <dynamicField name="*_str" type="strings" stored="false" docValues="true" indexed="false" />
    <dynamicField name="*_dt"  type="pdate"    indexed="true"  stored="true"/>
    <dynamicField name="*_dts" type="pdate"    indexed="true"  stored="true" multiValued="true"/>
    <dynamicField name="*_p"  type="location" indexed="true" stored="true"/>
    <dynamicField name="*_srpt"  type="location_rpt" indexed="true" stored="true"/>

  • 創建Solr索引
    配置好xml文件之後,採用如下命令創建Solr索引,將Hbase表與Solr Collection關聯起來。

    ./solr-7.3.1/bin/solr-indexer add -n indexerName -f xml path -c solr collection
    其中,indexerName爲您希望的Solr索引名稱,xml path爲xml配置文件的路徑,
    solr Collection爲上面步驟中創建的solr Collection名稱
    

    創建Solr索引之後,通過如下命令,查看Solr索引是否創建成功以及索引狀態

    查看solr的所有索引
    ./solr-7.3.1/bin/solr-indexer list
    
    查看指定Hbase表的索引狀況
    ./solr-7.3.1/bin/solr-indexer list -dump -t solrdemo
    其中solrdemo爲Hbase表的名稱
    
    對Hbase表中已有數據進行索引創建
    ./solr-7.3.1/bin/solr-indexer rebuild -n indexerName -t solrdemo -r 10
    其中,indexerName爲索引名稱,solrdemo爲Hbase表的名稱
    
    查看Hbase表中已有數據進行索引創建的狀態
    ./solr-7.3.1/bin/solr-indexer rebuild-status -n indexerName -t solrdemo
    其中,indexerName爲索引名稱,solrdemo爲Hbase表的名稱
    

    如果後期,您修改了xml配置文件,可以通過如下命令使其生效:

    ./solr-7.3.1/bin/solr-indexer update -n indexerName -f xml path -c solr collection -w 3000
    其中,indexerName爲您希望的Solr索引名稱,xml path爲xml配置文件的路徑,
    solr Collection爲上面步驟中創建的solr Collection名稱,3000爲3s後生效
    

    創建索引之後,在往Hbase中推送數據,則自動創建索引。

  • 通過Solr訪問Hbase數據
    如果您的Solr索引創建成功,並對Hbase表中的已有數據成功進行索引創建,則可以通過Solr直接查詢訪問Hbase表中的數據。
    打開Solr界面,通過左側目錄欄,下方下拉菜單中選擇您的Solr Collection,將出現該Collection的相關信息,如下圖所示:
    在這裏插入圖片描述
    點擊Query通過條件查詢Hbase數據,查詢結果如下圖所示
    在這裏插入圖片描述
    也可以通過Solr界面下發一下指令對Solr數據進行操作,操作界面爲Documents,其界面情況如下圖:
    在這裏插入圖片描述
    舉個例子,希望將Solr Collection中的所有數據刪除清空,首先需要將Docment Type修改爲XML,然後下發如下指令:

<delete><query>*:*</query></delete>
<commit/>

再點擊Submit Document即可將Solr Collection中的數據清空,但是Hbase中的數據不會被清除

至此,整個過程就結束了,有錯誤的話,歡迎指正,一起學習一起進步。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章