Linux上搭建Elasticsearch服務器並同步數據庫


1.准備工作 
 
     下載Elasticsearch版本號2.3.4 https://www.elastic.co/downloads/past-releases/elasticsearch-2-3-4 ,
     下載同步數據庫所需要的包  https://codeload.github.com/jprante/elasticsearch-jdbc/tar.gz/2.3.4.0 ,
 
2.運行Elasticsearch
 
解壓    
tar vxf elasticsearch-2.3.4.tar
Elasticsearch已經准備就緒,執行以下命令可在前台啟動:
./bin/elasticsearch
如果想在后台以守護進程模式運行,添加 -d  參數
./bin/elasticsearch -d
打開另一個終端進行測試:
curl -XGET "http://127.0.0.1:9200/?pretty"
 
你能看到以下返回信息:
{
   "name": "Nico Minoru",
   "cluster_name": "elasticsearch",
   "version": {
      "number": "2.3.4",
      "build_hash": "e455fd0c13dceca8dbbdbb1665d068ae55dabe3f",
      "build_timestamp": "2016-06-30T11:24:31Z",
      "build_snapshot": false,
      "lucene_version": "5.5.0"
   },
   "tagline": "You Know, for Search"
}

 

這說明你的ELasticsearch集群已經啟動並且正常運行.
 
3.添加ik中文分詞
在elasticsearch/plugins/下創建文件夾ik ,將elasticsearch-analysis-ik.zip 解壓到下面
 
4.創建 索引mapping同步數據庫
 
編寫腳本.sh
 
#刪除索引
curl -XDELETE 'http://127.0.0.1:9200/gsdata'
 
#創建Mapping
curl -XPUT "http://127.0.0.1:9200/gsdata" -d'
{
      "mappings":{
        "egov_basc_jbxx":{
            "_all": {
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word",
                    "term_vector": "no",
                    "store": "false"
                },
          "dynamic":false,
          "properties": {
            "id":{
              "type":"string"
            },
            "entname":{
              "type":"string",
              "analyzer":"ik_smart"
            },
            "lerep":{
              "type":"string",
              "analyzer":"ik_max_word"
            },
            "poscope":{
              "type":"string",
              "analyzer":"ik_smart"
            },
            "econat":{
              "type":"string",
              "analyzer":"ik_smart"
            },
            "esdate":{
              "type":"date"
            },
             "regcap":{
              "type":"integer"
            },
            "state":{
              "type":"string"
            }
          }
        }
      }
    }'
 
#同步數據   
DIR=/home/search/elasticsearch-jdbc-2.3.4.0
bin=${DIR}/bin
lib=${DIR}/lib
JAVA_HOME=/home/search/jdk1.8.0_111
 
echo '
{
    "type" : "jdbc",
    "jdbc" : {
        "url" : "jdbc:oracle:thin:@//127.0.0.1:1521/ORCL",
        "user" : "intgdata",
        "password" : "intgdata",
        "sql" : "SELECT ID as  \"id\" , ENTNAME as \"entname\" , LEREP as \"lerep\", OPSCOPE as \"poscope\", ECONAT as \"econat\", ESTDATE as \"esdate\" ,REGCAP as \"regcap\" ,STATE as \"state\" FROM EGOV_BASC_JBXX ",
        "treat_binary_as_string" : true,
        "index" : "gsdata",
        "type": "egov_basc_jbxx",
        "elasticsearch" : {
            "cluster" : "elasticsearch",
            "host" : "127.0.0.1",
            "port" : 9300
        }
    }
}
' | ${JAVA_HOME}/bin/java \
    -cp "${lib}/*" \
    -Dlog4j.configurationFile=${bin}/log4j2.xml \
    org.xbib.tools.Runner \
    org.xbib.tools.JDBCImporter

備注:因為這個版本elasticsearch-jdbc必須要1.8以上的版本,所以可以指定jdk去運行

5.運行腳本.sh
 
6.查看數據是否錄入
 
curl -XPOST 'http://localhost:9200/_search'

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM