安装之前,请参考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. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  2. CABasicAnimation

    几个可以用来实现热门APP应用PATH中menu效果的几个方法 +(CABasicAnimation *)opacityForever_Animation:(float)time //永久闪烁的动画 ...

  3. [selenium webdriver Java]元素定位——findElement/findElements

    策略 语法 语法 描述 By id driver.findElement(By.id()) driver.findElements(By.id()) 通过id属性定位元素 By name driver ...

  4. C 实现的算法篇

    算法的定义:算法是解决实际问题的一种精确的描述方法,目前,广泛认同的定义是:算法的模型分析的一组可行的确定的和有穷的规则 算法的五个特性:有穷性,确切性,输入,输出,可行性.目前算法的可执行的步骤非常 ...

  5. ASP.NET MVC之Html.RenderAction

    WEB窗体模式开发惯了,切入MVC模式,好多东西都不懂,每一步都要查资料. 初步得来的一些知识点体会是: _Layout.cshtml就相当于母版页 然后partical视图(部分视图)就是用户控件. ...

  6. Write a program to convert decimal to 32-bit unsigned binary.

    Write a program to convert decimal to 32-bit unsigned binary. Write a program to convert a 32-bit un ...

  7. Aggregation(1):Blending、Bagging、Random Forest

    假设我们有很多机器学习算法(可以是前面学过的任何一个),我们能不能同时使用它们来提高算法的性能?也即:三个臭皮匠赛过诸葛亮. 有这么几种aggregation的方式: 一些性能不太好的机器学习算法(弱 ...

  8. Ubuntu中、英文环境设置

    改变ubuntu的中英文显示需要修改文件/etc/default/locale,具体设置过程为: 1.打开/etc/default/locale文件 #sudo vim /etc/default/lo ...

  9. L0、L1与L2范数、核范数(转)

    L0.L1与L2范数.核范数 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化.我们先简单的来理解下常用的L0.L1.L2和核范数规则化.最后聊下规则化项参数的选择问题.这里因为篇幅比较庞大 ...

  10. 【Hadoop学习】Super用户以其他用户的名义执行操作

    Hadoop版本:2.6.0 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4146410.html 简介 ...