配置 JDBC 数据源
1. 安装 openjdk(本人用的ubuntu)
1
|
sudo
apt-get
install
openjdk-7-jre
|
2.下载安装 elasticsearch
1
2
3
|
wget https:
//download
.elasticsearch.org
/elasticsearch/elasticsearch/elasticsearch-1
.4.4.deb
sudo
dpkg -i elasticsearch-1.4.4.deb
/etc/init
.d
/elasticsearch
start
|
3.安装JDBC 插件
1
|
.
/bin/plugin
--
install
jdbc --url http:
//xbib
.org
/repository/org/xbib/elasticsearch/plugin/elasticsearch-river-jdbc/1
.4.0.10
/elasticsearch-river-jdbc-1
.4.0.10.zip
|
4.安装Mysql JDBC 驱动
1
2
3
4
5
|
curl -o mysql-connector-java-5.1.33.zip -L
'http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.33.zip/from/http://cdn.mysql.com/'
unzip mysql-connector-java-5.1.33.zip
cp
mysql-connector-java-5.1.33-bin.jar $ES_HOME
/plugins/jdbc/
chmod
644 $ES_HOME
/plugins/jdbc/
*
/etc/init
.d
/elasticsearch
restart
|
http://localhost:9200/_nodes?settings=true&pretty=true 配置信息(安装路径、插件...)
更多参数见:https://github.com/jprante/elasticsearch-river-jdbc
5.测试mysql (用户root 密码passwd 库jdbctest 表名posts)
1
2
3
4
5
6
7
8
9
10
|
curl -XPUT
'localhost:9200/_river/my_jdbc_river/_meta'
-d '{
"type"
:
"jdbc"
,
"jdbc"
: {
"url"
:
"jdbc:mysql://localhost:3306/jdbctest"
,
"user"
:
"root"
,
"password"
:
"passwd"
,
"sql"
:
"select * from posts"
}
}'
curl
'localhost:9200/jdbc/_search'
|
IK 中文分词插件
1. elasticsearch 默认的分词功能:(简单的把每个汉字分开)
1
|
root@blog-mreald-com:
/usr/share/elasticsearch
# curl -XGET 'http://localhost:9200/posts/_analyze?analyzer=standard&pretty=true&text=Mreald致力于全栈工程师'
|
结果如下:
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
27
|
{
"tokens"
: [ {
"token"
:
"mreald"
,
"start_offset"
: 1,
"end_offset"
: 7,
"type"
:
"<ALPHANUM>"
,
"position"
: 1
}, {
"token"
:
"致"
,
"start_offset"
: 8,
"end_offset"
: 9,
"type"
:
"<IDEOGRAPHIC>"
,
"position"
: 2
}, {
"token"
:
"力"
,
"start_offset"
: 9,
"end_offset"
: 10,
"type"
:
"<IDEOGRAPHIC>"
,
"position"
: 3
}, {
"token"
:
"于"
,
"start_offset"
: 10,
"end_offset"
: 11,
"type"
:
"<IDEOGRAPHIC>"
,
"position"
: 4
}, {
...............................
|
2.安装IK插件:(1.2.9没有jar包,要自己打)
要与ES版本配套:
Version
-—————
master | 1.4.0 → master
1.2.9 | 1.4.0
1.2.8 | 1.3.2
1.2.7 | 1.2.1
下载elasticsearch-analysis-ik 源代码 https://github.com/medcl/elasticsearch-analysis-ik
1
2
3
4
5
|
cd
elasticsearch-analysis-ik
mvn package
cd
target
/releases/
|
就会看到 elasticsearch-analysis-ik-1.2.9.zip
把elasticsearch-analysis-ik-1.2.9.zip 解压到 ES/plugins/analysis-ik/
1
2
3
|
root@blog-mreald-com:
/usr/share/elasticsearch/plugins/analysis-ik
# ls
commons-codec-1.6.jar elasticsearch-analysis-ik-1.2.9.jar httpclient-4.3.5.jar
commons-logging-1.1.3.jar elasticsearch-analysis-ik-1.2.9.zip httpcore-4.3.2.jar
|
将ik的配置和字典都复制到ES_HOME/config下
1
|
sudo
cp
-R ik
/etc/elasticsearch
|
elasticsearch.yml 增加下面一行,然后重启下ES
1
|
index.analysis.analyzer.ik.type : 'ik'
|
3.IK 安装之后:
1
|
root@blog-mreald-com:
/usr/share/elasticsearch
# curl -XGET 'http://localhost:9200/posts/_analyze?analyzer=ik&pretty=true&text=Mreald致力于全栈工程师'
|
查看结果:
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
27
|
{
"tokens"
: [ {
"token"
:
"mreald"
,
"start_offset"
: 1,
"end_offset"
: 7,
"type"
:
"ENGLISH"
,
"position"
: 1
}, {
"token"
:
"致力于"
,
"start_offset"
: 8,
"end_offset"
: 11,
"type"
:
"CN_WORD"
,
"position"
: 2
}, {
"token"
:
"致力"
,
"start_offset"
: 8,
"end_offset"
: 10,
"type"
:
"CN_WORD"
,
"position"
: 3
}, {
"token"
:
"致"
,
"start_offset"
: 8,
"end_offset"
: 9,
"type"
:
"CN_WORD"
,
"position"
: 4
}, {
...................
|
注意事件:
mvn 需要额外安装,centos 方法参考
ubuntu 使用 sudo aptitude install maven
Errors:
1.
1
2
3
4
5
6
7
8
9
10
|
[ERROR] Unable to locate the Javac Compiler in:
/usr/lib/java/jre1.7.0_55/../lib/tools.jar
Please ensure you are using JDK 1.4 or above and
not a JRE (the com.sun.tools.javac.Main class is required).
In most cases you can change the location of your Java
installation by setting the JAVA_HOME environment variable.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project elasticsearch-analysis-ik: Compilation failure
[ERROR] Unable to locate the Javac Compiler in:
[ERROR] /usr/lib/java/jre1.7.0_55/../lib/tools.jar
|
原因:ubuntu 自带的JAVA openjdk包不全,没有tools.jar包。解决方法(find 下你刚才安装的openjdk在哪儿):
1
2
|
sudo
apt-get
install
openjdk-7-jre;
export
export
JAVA_HOME=
/usr/lib/jvm/java-7-openjdk-amd64/
|
2.错误:
{ "error" : "NoShardAvailableActionException[[_na][_na] No shard available for [org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest@7c9c15]]", "status" : 503 }
cp 过去的是: target/releases/elasticsearch-analysis-ik-1.2.9.zip
不是: target/elasticsearch-analysis-ik-1.2.9.jar