ElasticSearch(四) Elasticsearch-jdbc實(shí)現(xiàn)MySQL同步到ElasticSearch

按照之前的四篇文章,走到現(xiàn)在,接下來就是需要安裝jdbc了。這里是用的是jdbc的2.3.4.0版本,雖然按照官方給出的對應(yīng)版本對應(yīng)不上,https://github.com/jprante/elasticsearch-jdbc,但是實(shí)際測試,是可以支持的。

下載解壓并移動(dòng)到/usr/local/jdbc目錄

#wget http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/2.3.4.0/elasticsearch-jdbc-2.3.4.0-dist.zip   //wget下載地址
# unzip elasticsearch-jdbc-2.3.4.0-dist.zip
# mv elasticsearch-jdbc-2.3.4.0-dist.zip /usr/local/jdbc/

配置環(huán)境變量

# vim /etc/profile //把下面兩行代碼放入底部
export JDBC_HOME=/usr/local/jdbc
export ES_HOME=/usr/local/elasticsearch
# source /etc/profile  //生效

接下來創(chuàng)建導(dǎo)入腳本

# cd /usr/local/
# mkdir shell shell/logs
# vim mysql-elasticsearch.sh
#腳本內(nèi)容
#!/bin/sh
bin=$JDBC_HOME/bin
lib=$JDBC_HOME/lib
echo '{
"type" : "jdbc",
"jdbc": {
      "elasticsearch.autodiscover":true, 
      "elasticsearch.cluster":"my-application",         #簇名,對應(yīng)好配置
      "url":"jdbc:mysql://192.168.9.155:3306/hb",     #mysql地址/端口/庫名
      "user":"root",   #Mysql賬號
      "password":"wt000000",  #Mysql密碼
      "sql":"select product_name,id as _id from hp_product where id >10 limit 10 ",  #SQL語句 .........注意的是,這里只是測試,在實(shí)際配置中需要對應(yīng)好要存入文檔的字段,同樣表的主見id對應(yīng)文檔id
      "elasticsearch" : {
                "cluster":"my-application",
          "host" : "192.168.9.155",
          "port" : 9300
       },
      "index" : "myindex",     #新的index,這里測試的是商品表,用的index是myindex,其實(shí)可以規(guī)范命名~
      "type" : "mytype"        #新的type,類型也可以規(guī)范命名
      }
}'| java \
  -cp "${lib}/*" \
  -Dlog4j.configurationFile=${bin}/log4j2.xml \
  org.xbib.tools.Runner \
  org.xbib.tools.JDBCImporter
# chmod a+x mysql-elasticsearch.sh   //賦予權(quán)限
# ./mysql-elasticsearch.sh   //執(zhí)行腳本
41.png

執(zhí)行完成之后,查看一下日志文件,[INFO]成功信息~

cat /usr/local/shell/logs/jdbc.log
42.png
curl -XGET 'http://192.168.9.155:9200/myindex/mytype/_search?pretty'  
{
  "took" : 14,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 10,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "myindex",
      "_type" : "mytype",
      "_id" : "14",
      "_score" : 1.0,
      "_source" : {
        "product_name" : "兌點(diǎn)茶 剪刀石頭布原花混合茶 10包"
      }
    }, {
      "_index" : "myindex",
      "_type" : "mytype",
      "_id" : "19",
      "_score" : 1.0,
      "_source" : {
        "product_name" : "L'Occitane 歐舒丹 蠟菊賦顏精華液 30ml"
      }
    }, {
      "_index" : "myindex",
      "_type" : "mytype",
      "_id" : "12",
      "_score" : 1.0,
      "_source" : {
        "product_name" : "兌點(diǎn)茶 烏克麗麗原花混合茶 10包"
      }
    }, {
      "_index" : "myindex",
      "_type" : "mytype",
      "_id" : "15",
      "_score" : 1.0,
      "_source" : {
        "product_name" : "兌點(diǎn)茶 魔法精靈原花混合茶12包/盒"
      }
    }, {
      "_index" : "myindex",
      "_type" : "mytype",
      "_id" : "20",
      "_score" : 1.0,
      "_source" : {
        "product_name" : "Bobbi Brown波比布朗 彈力保濕精華液 30ml"
      }
    }, {
      "_index" : "myindex",
      "_type" : "mytype",
      "_id" : "13",
      "_score" : 1.0,
      "_source" : {
        "product_name" : "兌點(diǎn)茶 挪威森林原花混合茶 10包"
      }
    }, {
      "_index" : "myindex",
      "_type" : "mytype",
      "_id" : "16",
      "_score" : 1.0,
      "_source" : {
        "product_name" : "兌點(diǎn)茶 涌雪原花混合茶12包/盒"
      }
    }, {
      "_index" : "myindex",
      "_type" : "mytype",
      "_id" : "18",
      "_score" : 1.0,
      "_source" : {
        "product_name" : "L’Occitane 歐舒丹蠟菊亮白水凝精華液 30ml"
      }
    }, {
      "_index" : "myindex",
      "_type" : "mytype",
      "_id" : "11",
      "_score" : 1.0,
      "_source" : {
        "product_name" : "兌點(diǎn)茶 艾麗莎原花混合茶12包"
      }
    }, {
      "_index" : "myindex",
      "_type" : "mytype",
      "_id" : "17",
      "_score" : 1.0,
      "_source" : {
        "product_name" : "兌點(diǎn)茶 菩蘭原花混合茶12包"
      }
    } ]
  }
}
43.png

附加----

#!/bin/sh
bin=$JDBC_HOME/bin
lib=$JDBC_HOME/lib
echo '{
    "type" : "jdbc",
    "jdbc" : {
        "url" : "jdbc:mysql://172.16.10.XXX:3306/db_car",
        "user" : "write",
        "password" : "write",
        "sql" : "select id,title,fnStripTags(content) as content,click_num,keywords,UNIX_TIMESTAMP(updated) as update_time from t_knowledgenew",
    "elasticsearch" : {
        "cluster": "my-application",
        "host" : "172.16.170.XXX",
        "port" : 9300
    },
    "index" : "knowledge_index",
    "type" : "knowledge"
    }
}' | java \
       -cp "${lib}/*" \
       -Dlog4j.configurationFile=${bin}/log4j2.xml \
       org.xbib.tools.Runner \
       org.xbib.tools.JDBCImporter

通過命令查看es中數(shù)據(jù)

curl -XGET 'http://XXX.XXX.XXX.XXX:9200/knowledge_index/knowledge/_search?pretty'   
44.png

刪除指定索引

# curl -XDELETE 'http://172.16.170.122:9200/index_name'
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容