學習筆記:從0開始學習大資料-28. solr儲存資料在hdfs並從mysql匯入資料
環境 centos7 hadoop2.6.0 solr-7.5.0
一、建立hdfs為儲存的core
1.在hdfs建立索引資料目錄
[[email protected] bin]# hadoop fs -mkdir /user/solr/
[[email protected] bin]# hadoop fs -chown linbin /user/solr
2. 建立core目錄
mkdir solr-7.5.0/server/solr/hdfscore
cp -rf solr-7.5.0/example/files/conf solr-7.5.0/server/solr/hdfscore
3. 修改檔案 hdfscore/conf/solfconfig.xml
設定資料目錄 註釋原來的目錄指定
<!-- <directoryFactory name="DirectoryFactory"
class="${solr.directoryFactory:solr.NRTCachingDirectoryFactory}"/>
-->
<directoryFactory name="DirectoryFactory" class="solr.HdfsDirectoryFactory">
<str name="solr.hdfs.home">hdfs://centos7:8020/user/solr</str>
<bool name="solr.hdfs.blockcache.enabled">true</bool>
<int name="solr.hdfs.blockcache.slab.count">1</int>
<bool name="solr.hdfs.blockcache.direct.memory.allocation">true</bool>
<int name="solr.hdfs.blockcache.blocksperbank">16384</int>
<int name="solr.hdfs.nrtcachingdirectory.maxcachedmb">192</int>
</directoryFactory>
找到 :
<lockType>${solr.lock.type:native}</lockType>
修改為:
<lockType>${solr.lock.type:hdfs}</lockType>
4.啟動solr服務,並在web介面建立新的“Add Core” 名為 "hdfscore"
5. 可以看見按預期建立了hdfscore
二. 從mysql匯入資料
1.在mysql建立需要匯入的樣例資料
2.相關驅動程式查詢並複製到lib目錄
mkdir solr-7.5.0/server/solr/hdfscore/lib
3.修改配置檔案,共三個 solrconfig.xml, data-import.xml, managed-schema.xml,其中 data-config.xml是新建檔案
(1) solrconfig.xml 後面增加
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
(2) data-config.xml 內容
<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"
user="root"
password="123"/>
<document>
<entity name="product" query="select pid,name,catalog_name,price,descrition,picture from products">
<field column="pid" name="id"/>
<field column="name" name="product_name"/>
<field column="catalog_name" name="product_catalog_name"/>
<field column="price" name="product_price"/>
<field column="descrition" name="product_descrition"/>
<field column="picture" name="product_picture"/> </entity>
</document>
</dataConfig>
(3) managed-schema.xml 增加
<field name="product_name" type="string" indexed="true" stored="true" />
<field name="product_catalog_name" type="string" indexed="true" stored="true" />
<field name="product_price" type="string" indexed="true" stored="true" />
<field name="product_descrition" type="string" indexed="true" stored="true" />
<field name="product_picture" type="string" indexed="true" stored="true" />
4. 重新啟動solr執行匯入資料
bin/solr stop
bin/solr/start
6.測試執行檢索
7. 瀏覽器直接檢索也可以: http://centos7:8983/solr/hdfscore/select?q=*:*
9. 在hadoop hdfs目錄可以看到建立的索引檔案