Elasticsearch2.3入门

来源:互联网 发布:数控铣床倒圆角编程 编辑:程序博客网 时间:2024/06/07 00:02

如转载请声明来源:http://blog.csdn.net/LG772EF/article/details/51925784

由于项目需求,近期学习es,入门学习信息进行记录

    • 基础环境说明
    • 一es环境搭建
      • es单机环境搭建
        • 1 es安装
        • 2 插件安装
        • 3 基础参数说明
      • 基础实践
        • 1 Restful API
        • 2 Java API
      • es集群搭建
    • 二问题记录
      • 启动后不能通过IP访问
      • linux下不能通过root启动
      • 集群配置时需要明确指明需要扫描的地址信息
    • 三学习资源

基础环境说明

本人在win7上搭建的环境:jdk64位1.7.0_55, es用的是2.3.4。后来由于要用es-river-jdbc插件,切换到es2.3.3版本
linux上(Red Hat Enterprise Linux Server release 5.4, 32位, jdk1.7)也搭建了一套,操作流程一致。
因两套环境操作一致,后续操作不做特殊说明,如果差异之处,会单独说明

一、es环境搭建

1. es单机环境搭建

tip:jdk版本要求1.7以上,推荐1.8
这里写图片描述

1.1 es安装

a) es官网直接下载安装包安装 (Download and unzip the latest Elasticsearch distribution)
官网:http://www.elasticsearch.org
下载地址:https://www.elastic.co/downloads/elasticsearch
windows下载zip,linux下载tar或rpm
这里写图片描述

b) 运行elasticsearch (Run bin/elasticsearch on Unix or bin/elasticsearch.bat on Windows)
注意:linux下用root用户启动会报错,不允许用root用户启动,请新建其他用户,或者启动命令后增加参数
./bin/elasticsearch -Des.insecure.allow.root=true
这里写图片描述

c) 访问 http://localhost:9200 (Run curl -X GET http://localhost:9200/)
name、cluster_name已经自己设置过了, cluster_name默认是elasticsearch, name默认会随机创建
tip:默认只能通过localhost或127.0.0.1访问,不能通过ip访问,原因请看后面的基础参数配置说明
这里写图片描述

至此,es安装完毕.

官网上的安装步骤说明

官网详细安装说明:https://www.elastic.co/guide/en/elasticsearch/reference/current/_installation.html

1.2 插件安装

head插件安装
a)在elasticsearch/bin/目录下通过plugin命令安装
命令
windows下:plugin install mobz/elasticsearch-head
linux下:./plugin install mobz/elasticsearch-head
b).运行es
c).打开http://localhost:9200/_plugin/head/
方法1已做过自测,网上1.*版本的入门说明中,install前可能要加-

插件安装方法2:
a.https://github.com/mobz/elasticsearch-head下载zip 解压
b.建立elasticsearch-1.0.0\plugins\head_site文件
c.将解压后的elasticsearch-head-master文件夹下的文件copy到_site
d.运行es
e.打开http://localhost:9200/_plugin/head/
tip:方法2从网上获取,未自测验证

访问界面:
这里写图片描述

1.3 基础参数说明

配置文件位置:elasticsearch-2.3.4/config/elasticsearch.yml
cluster.name: 集群名字,默认为elasticsearch
node.name: 节点名称,默认会从es的jar包names.txt里面,随机取动漫人物名称
node.master:默认为true,是否可以被选举为主节点
network.bind_host: 绑定的ip地址
network.publish_host: 对外发布的ip地址, 其他节点连接此节点的地址
network.host: 一起设置bind_host和publish_host的值,默认是localhost或127.0.0.1,如果不设置,访问时只能通过localhost或127.0.0.1访问。设置为0.0.0.0时,localhost或者IP均可访问
transport.tcp.port:默认9300,用于java客户端或者内部交换数据使用
http.port: 默认9200, restful调用访问的端口地址

2. 基础实践

入门增删改查、搜索 (java + restful两种方式)
官网API地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html

2.1 Restful API

添加/修改数据:
$ curl -XPUT ‘http://IP:9200/twitter/tweet/1’ -d ‘{“user” : “kimchy”,”post_date” : “2009-11-15T14:12:12”,”message” : “trying out Elasticsearch”}’
添加数据的信息:index=twitter, type=tweet, index=1, 如果本身已有twitter/tweet/1这条数据信息,则会更新已有数据
curl是linux redhat下命令,没有linux环境的同学,可以手工在浏览器上输入 http://ip:port/twitter/tweet/1 -d ‘参数信息’,或者找模拟工具,如httpie

head插件查看数据添加结果
这里写图片描述

b) 删除数据
$ curl -XDELETE ‘http://IP:9200/twitter/tweet/1’
删除index=twitter, type=tweet, id=1的这条数据
tip: 指向delete操作时,如果index或type不存在,会创建index和type
$ curl -XDELETE ‘http://IP:9200/twitter’
删除索引,索引名为twitter, 下面所有的type、document均会删除

c) 查看数据
curl -XGET ‘http://IP:9200/twitter/tweet/1’
查看index=twitter, type=tweet, id=1的这条数据
curl -XHEAD -i ‘http://localhost:9200/twitter/tweet/1’
查看index=twitter, type=tweet, id=1的这条数据是否存在

2.2 Java API

ElasticsearchStu.rar
入门学习搭建的maven工程,增删改查列子请查看CRUDTest.java这个类

3. es集群搭建

本人是在同一台机器上安装了2个es。安装步骤和单个安装一致,不在赘述,有几个参数需要修改(config/elasticsearch.yml)
a). 节点信息
cluster.name: elasticCluster — 集群名,需要制定为一致,默认是elasticsearch
node.name: node-t2 — 节点名,需不一致
a). 占用的端口 – 需不一致
transport.tcp.port: 9301 (node-t1配置的是9300,node-t2配置的是9301)
http.port: 9201 (node-t1配置的是9200,node-t2配置的是9201)
b). 节点扫描
节点启动时会去扫描配置的节点列表,多个用英文逗号隔开
discovery.zen.ping.unicast.hosts: [“127.0.0.1:9300”] (node-t1配置的是”127.0.0.1:9301”,node-t2配置的是9301)
2.3.4版本不会主动扫描局域网同名的节点信息,需要手工指定
这里写图片描述

启动后,可以看到有两个节点
这里写图片描述

二、问题记录

记录es学习过程中碰到的各种坑

1. 启动后不能通过IP访问

es2.3.4默认绑定的ip为127.0.0.1,需手工设置绑定ip,方可通过ip访问
network.host: localhost或127.0.0.1,如果不设置,访问时只能通过localhost或127.0.0.1访问。设置为0.0.0.0时,localhost或者IP均可访问,或者设置为

2. linux下不能通过root启动

./bin/elasticsearch -Des.insecure.allow.root=true

3. 集群配置时,需要明确指明需要扫描的地址信息

discovery.zen.ping.unicast.hosts: [“127.0.0.1:9300”]
“discovery.zen.ping.unicast.hosts”的值可以是单值也可以是多值,在不同的服务器之间部署es节点可以不指明ip端口,但是在同一服务器中部署,ip需要加上检测的端口号,否则检测不到要加入的节点

三、学习资源

  1. 官网
    https://www.elastic.co/
    https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

  2. elasticsearch权威指南
    http://es.xiaoleilu.com/ (中文版-未翻译完)
    https://github.com/elastic/elasticsearch-definitive-guide (英文原版)

参考帖子:
http://mageedu.blog.51cto.com/4265610/1714522?utm_source=tuicool&utm_medium=referral — 搜索、es的一些概念说明,不了解es的童鞋可以先看这篇
http://www.cnblogs.com/muzhiye/p/elasticsearch_set_cluster.html
http://blog.csdn.net/cnweike/article/details/33736429
http://www.jianshu.com/p/05cff717563c

建议:
学习一门新技术,建议找一份靠谱的资料很重要(官方文档或者入门书籍)。
es学习时baidu上搜索到的都是老的入门说明文档,在2.3搭建过程中碰到不少坑。、
后来google+官方说明才搞定

本人水平有限,如果不妥之处,请指正,谢谢

1 0