ElasticSearch使用
安装之前,请参考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:9200lsof -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 .............................................................................................DONEInstalled 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://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
https://github.com/jprante/elasticsearch-river-jdbc/issues/186
官方文档:
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使用的更多相关文章
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- Elasticsearch 5.0 中term 查询和match 查询的认识
Elasticsearch 5.0 关于term query和match query的认识 一.基本情况 前言:term query和match query牵扯的东西比较多,例如分词器.mapping ...
- 以bank account 数据为例,认识elasticsearch query 和 filter
Elasticsearch 查询语言(Query DSL)认识(一) 一.基本认识 查询子句的行为取决于 query context filter context 也就是执行的是查询(query)还是 ...
- Ubuntu 14.04中Elasticsearch集群配置
Ubuntu 14.04中Elasticsearch集群配置 前言:本文可用于elasticsearch集群搭建参考.细分为elasticsearch.yml配置和系统配置 达到的目的:各台机器配置成 ...
- ElasticSearch 5学习(10)——结构化查询(包括新特性)
之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- .net Elasticsearch 学习入门笔记
一. es安装相关1.elasticsearch安装 运行http://localhost:9200/2.head插件3.bigdesk插件安装(安装细节百度:windows elasticsear ...
- 自己写的数据交换工具——从Oracle到Elasticsearch
先说说需求的背景,由于业务数据都在Oracle数据库中,想要对它进行数据的分析会非常非常慢,用传统的数据仓库-->数据集市这种方式,集市层表会非常大,查询的时候如果再做一些group的操作,一个 ...
- 如何在Elasticsearch中安装中文分词器(IK+pinyin)
如果直接使用Elasticsearch的朋友在处理中文内容的搜索时,肯定会遇到很尴尬的问题--中文词语被分成了一个一个的汉字,当用Kibana作图的时候,按照term来分组,结果一个汉字被分成了一组. ...
- 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 ...
随机推荐
- Kafka 设计与原理详解
一.Kafka简介 本文综合了我之前写的kafka相关文章,可作为一个全面了解学习kafka的培训学习资料. 转载请注明出处 : 本文链接 1.1 背景历史 当今社会各种应用系统诸如商业.社交.搜索. ...
- Minimax Triangulation
题意: 按顺序给定一些点,把这些点分割为n - 2个三角形,花费为最大三角形面积,求最小花费 分析: 区间dp,dp[i][j]表示完成区间[i,j]最小花费,dp[i][j]=min(dp[i][j ...
- hdu 2087-剪花布条(KMP)
题意: 求文本串最多可以分成几个模式串. 分析: KMP #include <map> #include <set> #include <list> #includ ...
- ASIHTTPREQUEST 文档
http://blog.csdn.net/ysysbaobei/article/details/17026577 Please note that I am no longer working on ...
- IOCP模型
IOCP http://blog.csdn.net/zhongguoren666/article/details/7386592 Winsock IO模型之IOCP模型 http://blog.csd ...
- 《Python核心编程》 第六章 序列 - 课后习题
课后习题 6–1.字符串.string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? 答:成员关系操作符(in.not in) import string ...
- ACM2025
/* 查找最大元素 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- URAL-1982 Electrification Plan 最小生成树
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1982 题意:无向图,给n个点,n^2条边,每条边有个一权值,其中有k个点有发电站,给出这 ...
- IIS中使用PUT方法錯誤記錄
在IIS7.5中使用PUT,DELETE方法時會遇到404,405錯誤,特記錄解決辦法:404: 405: 在web.config的system.webServer節點中加入 <modules ...
- 利用red5搭建一个简单的流媒体直播系统
http://blog.sina.com.cn/s/blog_51396f890102exmz.html 一.red5安装.设置. 这个过程就不多说了,参见http://blog.csdn.net/l ...