使用Elasticsearch-jdbc为MySQL数据库建立索引

elasticsearch-jdbc

环境

  • Ubuntu 14.04
  • JDK 1.8.0_66
  • Elasticsearch 2.3.1
  • Elasticsearch-jdbc 2.3.1.0
  • Elasticsearch单节点环境

进入es目录~/cluster/elasticsearch-2.3.1

下载elasticsearch-jdbc包,并解压

$ wget http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/2.3.1.0/elasticsearch-jdbc-2.3.1.0-dist.zip
$ unzip elasticsearch-jdbc-2.3.1.0-dist.zip

数据库中的数据

数据位于10.110.1.47:3306下的ispider_data数据库,表名为es_test
共三条数据如下:

id   name
4   zhangsan
2   lisi
3   wangwu

编辑数据导入脚本import.sh

vi import.sh
输入:
bin=/home/es/cluster/elasticsearch-2.3.1/elasticsearch-jdbc-2.3.1.0/bin
lib=/home/es/cluster/elasticsearch-2.3.1/elasticsearch-jdbc-2.3.1.0/lib
echo '{
"type" : "jdbc",
"jdbc": {
"url":"jdbc:mysql://10.110.1.47:3306/ispider_data",
"user":"root",
"password":"xxx",
"sql":"select * from es_test",
"index" : "customer",
"type" : "external"
}}' | java \
    -cp "${lib}/*" \
    -Dlog4j.configurationFile=${bin}/log4j2.xml \
    org.xbib.tools.Runner \
    org.xbib.tools.JDBCImporter

上述,将MySQL中的数据建立为es中索引为customer,类型为external中。

查询索引

es@search1:~/cluster/elasticsearch-2.3.1$ curl 'localhost:9200/customer/external/_search?pretty&q=*'

结果显示:

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "AVQ8--xAkvh0m5n1OUEo",
      "_score" : 1.0,
      "_source" : {
        "id" : "4",
        "name" : "zhangsan"
      }
    }, {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "AVQ8--xBkvh0m5n1OUEq",
      "_score" : 1.0,
      "_source" : {
        "id" : "3",
        "name" : "wangwu"
      }
    }, {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "AVQ8--xBkvh0m5n1OUEp",
      "_score" : 1.0,
      "_source" : {
        "id" : "2",
        "name" : "lisi"
      }
    } ]
  }
}

可见,_source中的各个字段field,与数据库中的各列对应。
注意,_source中的id是es_test表中的id列,索引中document的id是自动生成的,二者并不一样。
至此,从数据库MySQL导入ES建立索引的过程就完成了。

参考资料

http://www.voidcn.com/blog/kdchxue/article/p-5778237.html

转载于:https://www.cnblogs.com/myitroad/p/5434372.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值