安装之前,请参考https://github.com/richardwilly98/elasticsearch-river-mongodb根据你的MongoDB版本号决定需要的elasticsearch版本号和插件号。

1)安装ES

下载ElasticSearch_版本号.tar.gz,官网上有,下载好之后。

1
2
tar -zvxf elasticsearch-1.1.0.tar.gz
cd elasticsearch-1.1.0

安装一下插件,也可以不安装,这个插件用来监控用的

1
./bin/plugin -i elasticsearch/marvel/latest

想了解这个插件可以参考官方文档

1
http://www.elasticsearch.org/guide/en/marvel/current/index.html

2)执行程序

1
./elasticsearch

看到以下的就表示成功了

1
2
3
4
5
6
7
8
9
10
11
[2014-04-09 10:12:41,414][INFO ][node                     ] [Lorna Dane] version[1.1.0], pid[839], build[2181e11/2014-03-25T15:59:51Z]
[2014-04-09 10:12:41,415][INFO ][node                     ] [Lorna Dane] initializing ...
[2014-04-09 10:12:41,431][INFO ][plugins                  ] [Lorna Dane] loaded [], sites []
[2014-04-09 10:12:44,383][INFO ][node                     ] [Lorna Dane] initialized
[2014-04-09 10:12:44,384][INFO ][node                     ] [Lorna Dane] starting ...
[2014-04-09 10:12:44,495][INFO ][transport                ] [Lorna Dane] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/XXXXXX:9300]}
[2014-04-09 10:12:47,522][INFO ][cluster.service          ] [Lorna Dane] new_master [Lorna Dane][Ml-gTu_ZTniHR2mkpbMQ_A][XXXXX][inet[/XXXXXX:9300]], reason: zen-disco-join (elected_as_master)
[2014-04-09 10:12:47,545][INFO ][discovery                ] [Lorna Dane] elasticsearch/Ml-gTu_ZTniHR2mkpbMQ_A
[2014-04-09 10:12:47,572][INFO ][http                     ] [Lorna Dane] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/XXXXX:9200]}
[2014-04-09 10:12:47,607][INFO ][gateway                  ] [Lorna Dane] recovered [0] indices into cluster_state
[2014-04-09 10:12:47,607][INFO ][node                     ] [Lorna Dane] started

如果想后台运行,则执行

1
./elasticsearch -d

想确认程序是否运行,则运行

1
2
3
lsof -i:9200
lsof -i:9300
一个是节点对外服务端口,一个是节点间交互端口(如果有集群的话)。

3)建立集群

配置文件路径是:

1
.....(你的实际路径)/config/elasticsearch.yml

默认是全部配置项都屏蔽的,

我修改后配置项如下:

1
2
cluster.name: ctoes   ---配置集群的名字
node.name: "QiangZiGeGe"---配置节点的名字,注意有双引号
1
bootstrap.mlockall: true

没有提到的配置项都采用默认值,具体参数如何设置,还需要具体情况具体分析。

修改好后,启动es,可以看到打印的消息里有别的节点名字,就表示建立集群成功。

注意:es是自动探测局域网内的同名集群节点的。

查看集群的状态,可以通过:

1
curl 'http://localhost:9200/_cluster/health?pretty'
1
<span></span>响应如下:
1
2
3
4
5
6
7
8
9
10
11
12
{
  "cluster_name" : "ctoes",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 5,
  "active_shards" : 10,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0
}

接下来来使用一下来得到直观感受

4)使用数据库感受一下

创建索引(相当于创建数据库)

示例如下:

1
2
3
4
5
6
7
8
9
10
11
[deployer@XXXXXXX0013 ~]$ curl -XPUT 'http://localhost:9200/test1?pretty' -d'
> {
"settings":{
> "number_of_shards":2,
> "number_of_replicas":1
> }
> }
> '
{
  "acknowledged" : true
}

注意,这里的number_of_shards参数是一次性设置,设置之后永远不可以再修改的,但是number_of_replicas是可以随后可以修改的。

上面的url里的test1其实就是建立的索引(数据库)的名字,根据需要自己修改即可。

创建文档

1
2
3
4
5
6
7
8
9
10
curl -XPUT 'http://localhost:9200/test1/table1/1' -d '
{ "first":"dewmobile",
"last":"technology",
"age":3000,
"about":"hello,world",
"interest":["basketball","music"]
}
'
响应如下:
{"_index":"test1","_type":"table1","_id":"1","_version":1,"created":true}

表明创建文档成功

test1:建立的数据库名字

table1:建立的type名字,type与关系数据库的table对应

1:自己制定的文档的主键,也可以不指定主键由数据库自己分配。

5)安装数据库同步插件

由于我们的数据源是放在MongoDB中的,所以这里只讲MongoDB数据源的数据同步。

插件源码:https://github.com/richardwilly98/elasticsearch-river-mongodb/

1
2
3
MongoDB River Plugin (作者 Richard Louapre)
 
简介:mongodb同步插件,mongodb必须搭成副本集的模式,因为这个插件的原理是通过定期读取mongodb中的oplog来同步数据。

如何安装使用呢?需要安装2个插件

1)插件1

1
./plugin -install elasticsearch/elasticsearch-mapper-attachments/2.0.0

2)插件2

1
./bin/plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.0

安装过程如下:

1
2
3
4
5
6
7
./bin/plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.0
-> Installing com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.0...
Trying http://download.elasticsearch.org/com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/elasticsearch-river-mongodb-2.0.0.zip...
Trying http://search.maven.org/remotecontent?filepath=com/github/richardwilly98/elasticsearch/elasticsearch-river-mongodb/2.0.0/elasticsearch-river-mongodb-2.0.0.zip...
Trying https://oss.sonatype.org/service/local/repositories/releases/content/com/github/richardwilly98/elasticsearch/elasticsearch-river-mongodb/2.0.0/elasticsearch-river-mongodb-2.0.0.zip...
Downloading .............................................................................................DONE
Installed com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.0 into /usr/local/elasticsearch_1.1.0/elasticsearch/elasticsearch-1.1.0/plugins/river-mongodb

3)安装elasticsearch-MySql插件

具体请参考:

https://github.com/jprante/elasticsearch-river-jdbc可以直接下载二进制jar包。

1
https://github.com/jprante/elasticsearch-river-jdbc

4)安装mysql驱动jar包(必须!)

这样,插件就装好了。

6)使用插件告知ES添加监听数据库任务

模板如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
curl -XPUT localhost:9200/_river/mongo_resource/_meta -d '
{
"type":"mongodb",
"mongodb":{
"servers":
[{"host":"10.XX.XX.XX","port":"60004"}
],
"db":"zapya_api",
"collection":"resources"
},
"index":{
"name":"mongotest",
"type":"resources"
}}'

如果看到下面的内容表示创建成功

1
{"_index":"_river","_type":"mongodb","_id":"_meta","_version":1,"created":true}

然后,数据就导入到了es中了,索引建立成功。

~~~~~~~~~~~~~~~~

如果是导入mysql,模板如下:

1
2
3
4
5
6
7
8
9
10
[deployer@XXX0014 ~]$ curl -XPUT 'localhost:9200/_river/my_jdbc_river/_meta' -d '{
> "type":"jdbc",
> "jdbc":{
> "url":"jdbc:mysql://localhost:3306/fastooth",
> "user":"XXX",
> "password":"XXX",
> "sql":"select *,base62Decode(display_name) as name from users"
> }
> }
> '

更详细的是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
    "jdbc" :{
        "strategy" : "simple",
        "url" : null,
        "user" : null,
        "password" : null,
        "sql" : null,
        "schedule" : null,
        "poolsize" : 1,
        "rounding" : null,
        "scale" : 2,
        "autocommit" : false,
        "fetchsize" : 10, /* Integer.MIN for MySQL */
        "max_rows" : 0,
        "max_retries" : 3,
        "max_retries_wait" : "30s",
        "locale" : Locale.getDefault().toLanguageTag(),
        "index" : "jdbc",
        "type" : "jdbc",
        "bulk_size" : 100,
        "max_bulk_requests" : 30,
        "bulk_flush_interval" : "5s",
        "index_settings" : null,
        "type_mapping" : null
    }
}

对于schedule参数:设置调度时刻的

格式参考:http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

http://elasticsearch-users.115913.n3.nabble.com/Ann-JDBC-River-Plugin-for-ElasticSearch-td4019418.html

http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

https://github.com/jprante/elasticsearch-river-jdbc/issues/186

官方文档:

http://elasticsearch-users.115913.n3.nabble.com/Ann-JDBC-River-Plugin-for-ElasticSearch-td4019418.html

https://github.com/jprante/elasticsearch-river-jdbc/wiki/JDBC-River-parameters

https://github.com/jprante/elasticsearch-river-jdbc/wiki/Quickstart(包含如何删除任务)

附录:http://my.oschina.net/wenhaowu/blog/215219#OSC_h2_7

测试过程中,会出现错误:

1
[7]: index [yyyy], type [rrrr], id [1964986], message [RemoteTransportException[[2sdfsdf][inet[/xxxxxxxxxx:9300]][bulk/shard]]; nested: EsRejectedExecutionException[rejected execution (queue capacity 50) on org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1@3e82ee89]; ]

修改配置文件,在最后增加:

1
2
3
4
5
threadpool:
    bulk:
        type: fixed
        size: 60
        queue_size: 1000

至于这几个参数是什么意思,还请读者自己去弄明白。

参考:

http://stackoverflow.com/questions/20683440/elasticsearch-gives-error-about-queue-size

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-threadpool.html

~~~~~~~~~~~~~~~

关于客户端,我们使用了Play框架,正如数据库都需要驱动包一样,我们从官方网站上看到了这个

https://github.com/cleverage/play2-elasticsearch

关于中文分词,可以尝试使用Ansj.

~~~~~~~~~~~~~~~~~~~~~

关于创建索引:

curl -i -XPUT  'XXX:9200/fasth' -d '
{
   "settings" :
   {
      "number_of_shards" : 3 ,
      "number_of_replicas" : 1
   }
   
}
'

~~~~~~~~~~~

创建映射

curl -i -XPUT  'http://localhost:9200/fa/users/_mapping' -d '
{

"properties":
 {
  "_id":
  { 
  "type":"string",
  "index":"not_analyzed"
  },
  "name":
  {
  "type":"string"
  },
  "gender":
  {
  "type":"string",
  "index":"not_analyzed"
  },
  "primary_avatar":
  {
  "type":"string",
  "index":"not_analyzed"
  },
  "signature":
  {
  "type":"string",
  "index":"not_analyzed"
  }
 }

}
'

全量任务:

curl -XPUT  'xxx:9200/_river/mysql_users/_meta' -d ' 

 "type":"jdbc", 
 "jdbc": 
 { 
 "url":"jdbc:mysql://XXX:3306/fastooth", 
 "user":"XXX", 
 "password":"XXX", 
 "sql":"select distinct _id,base62Decode(display_name) as name,gender,primary_avatar,signature from users", 
 "index":"XXX", 
 "type":"XXX" 
 } 

'

http://www.nosqldb.cn/1368777378160.html

ElasticSearch使用的更多相关文章

  1. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  2. Elasticsearch 5.0 中term 查询和match 查询的认识

    Elasticsearch 5.0 关于term query和match query的认识 一.基本情况 前言:term query和match query牵扯的东西比较多,例如分词器.mapping ...

  3. 以bank account 数据为例,认识elasticsearch query 和 filter

    Elasticsearch 查询语言(Query DSL)认识(一) 一.基本认识 查询子句的行为取决于 query context filter context 也就是执行的是查询(query)还是 ...

  4. Ubuntu 14.04中Elasticsearch集群配置

    Ubuntu 14.04中Elasticsearch集群配置 前言:本文可用于elasticsearch集群搭建参考.细分为elasticsearch.yml配置和系统配置 达到的目的:各台机器配置成 ...

  5. ElasticSearch 5学习(10)——结构化查询(包括新特性)

    之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...

  6. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  7. .net Elasticsearch 学习入门笔记

    一. es安装相关1.elasticsearch安装  运行http://localhost:9200/2.head插件3.bigdesk插件安装(安装细节百度:windows elasticsear ...

  8. 自己写的数据交换工具——从Oracle到Elasticsearch

    先说说需求的背景,由于业务数据都在Oracle数据库中,想要对它进行数据的分析会非常非常慢,用传统的数据仓库-->数据集市这种方式,集市层表会非常大,查询的时候如果再做一些group的操作,一个 ...

  9. 如何在Elasticsearch中安装中文分词器(IK+pinyin)

    如果直接使用Elasticsearch的朋友在处理中文内容的搜索时,肯定会遇到很尴尬的问题--中文词语被分成了一个一个的汉字,当用Kibana作图的时候,按照term来分组,结果一个汉字被分成了一组. ...

  10. jar hell & elasticsearch ik 版本问题

    想给es 安装一个ik 的插件, 我的es 是 2.4.0, 下载了一个版本是 1.9.5, [2016-10-09 16:56:26,248][INFO ][node ] [node-2] init ...

随机推荐

  1. [转] arcgis Engine创建shp图层

    小生 原文 arcgis Engine创建shp图层 以创建点图层为例.首先要得到保存文件的地址. SaveFileDialog saveFileDialog = new SaveFileDialog ...

  2. Listview异步加载之优化篇

    异步加载图片基本思想: 1.      先从内存缓存中获取图片显示(内存缓冲) 2.      获取不到的话从SD卡里获取(SD卡缓冲) 3.      都获取不到的话从网络下载图片并保存到SD卡同时 ...

  3. UI篇---RadioButton(单选按钮)

    单选按钮RadioButton在Android平台上也应用的非常多,比如一些选择项的时候,会用到单选按钮,实现单选按钮由两部分组成,也就是RadioButton和RadioGroup配合使用 Radi ...

  4. How to Calculate difference between two dates in C# z

    Do you need to find the difference in number of days, hours or even minute between the two date rang ...

  5. [Irving]WPF Invalid character in the given encoding. Line xx, position xx.' XML is not valid.

    WPF开发中发现Xaml界面中突然抽风似的提示错误 Invalid character in the given encoding. Line xx, position xx.' XML is not ...

  6. <转>python version 2.7 required,which was not found in the registry

    安装PIL-1.1.7.win32-py2.7的时候,不能再注册表中识别出来python2.7 方法:新建一个register.py 文件,把一下代码贴进去,保存 # # script to regi ...

  7. C++实现离散余弦变换(参数为Eigen矩阵)

    C++实现离散余弦变换(参数为Eigen矩阵) 问题描述 昨天写了一个参数为二维指针为参数的离散余弦变换,虽然改进了参数为二维数组时,当数组大小不确定时声明函数时带来的困难,但使用指针作为参数也存在一 ...

  8. 通过库函数API和C代码中嵌入汇编代码剖析系统调用的工作机制

    作者:吴乐 山东师范大学<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 本次实验的主要内容就是分别采用A ...

  9. DOM笔记(二):Node接口

    所有的节点都使用Node接口来表示,可以使用很多方法去获取节点,如document.getElementsByTagName().document.getElementsByName()等均返回一个N ...

  10. hdfs[命令] fsck

    Usage: DFSck <path> [-list-corruptfileblocks | [-move | -delete | -openforwrite] [-files [-blo ...