安装之前,请参考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. bjfu1099 度度熊大战僵尸

    这也是2011年百度之星的一道题. 这题我就是乱搞搞过的,打代码之前自己心里也没底,不知道能不能过的. 我的做法很简单,就是按时间顺序依次构造能杀死的僵尸血量,找到第k小的.构造的方法也很暴力:对t时 ...

  2. FILEtoJPG-神秘文件 -更新(软件BUG及建议可以在这里反馈)

    FILEtoJPG-神秘文件(文件神隐助手) 论坛神器!彩虹系列作品之神秘文件(文件神隐助手),帮你隐藏文件的好帮手! 已更新,移除对winRAR的依赖 放张大图镇帖: 此图略丑,但是很有用,文末告诉 ...

  3. C#选择排序详解

    选择排序图解                         选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的 ...

  4. CentOS VPS创建pptpd VPN服务

    原文地址http://www.hi-vps.com/wiki/doku.php?id=xen_vps_centos6_install_pptpd CentOS VPS创建pptpd VPN服务 Xen ...

  5. vi--文本编辑常用快捷键之复制-粘贴-替换-删除

    这几天刚开始接触vi编辑器,慢慢开始熟悉vi,但是还是感觉诸多不便,比如说复制粘贴删除操作不能用鼠标总是感觉不自在,而且我一般习惯用方向键移动光标,更增加了操作的复杂度,今天在网上搜索了一下,vim编 ...

  6. linux socket中的SO_REUSEADDR

    Welcome to the wonderful world of portability... or rather the lack of it. Before we start analyzing ...

  7. 搭建linux下teamspeak3多人语音服务器

    最近项目中新的需求,需要支持多人在线实时通话.就安装测试一下teamspeak.http://www.teamspeak.com/ 主页有服务器版本和客户端版本供下载安装.软硬件环境: melot@m ...

  8. 第二百九十九天 how can I 坚持

    不是傻,就是因为人太好了,我宁愿相信是我人太好了,好吧,我就是对人都挺好,这是病吗. 昨天一起吃的饭一起AA了,挺好,这种事就得AA,玩的挺happy. 还有.感觉自己好傻,老是遇事焦虑,以后试着改变 ...

  9. Java设计模式系列之适配器模式

    适配器模式的定义 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.(就类似于我们充电器的转接头将220V的电压转换成我们的手机端 ...

  10. stdlib.h 头文件

    stdlib 头文件即standard library标准库头文件.stdlib.h里面定义了五种类型.一些宏和通用工具函数. 类型例如size_t.wchar_t.div_t.ldiv_t和lldi ...