首页 > 代码库 > Linux上搭建Elasticsearch服务器并同步数据库

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 ,
     下载ik中文分词 https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v1.9.4/elasticsearch-analysis-ik-1.9.4.zip
 
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

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Linux上搭建Elasticsearch服务器并同步数据库