elasticsearch环境搭建与使用
来源:http://blog.csdn.net/kingice1014/article/details/52860150
1.ES的安装与环境配置
首先请先去了解下ES的一些基本概念:cluster,node,index,shard,replica shard,plugin,river;这里就不一一赘述了。
ES的安装很简单:
– 确保机器上已经安装了JDK7以上版本
– 下载:官网下载地址:https://www.elastic.co/downloads/elasticsearch
– 将下载后的文件解压到/opt/eshome(默认的es home,可更改)
– 进入到bin文件夹下,执行“./elasticsearch -d”(-d表示在后台执行)
安装结束后,访问 http://ip:9200/,出现下面的返回结果。默认的cluster名为elasticsearch
{
"status" : 200,
"name" : "test_node_196",
"cluster_name" : " test_cluster1",
"version" : {
"number" : "1.7.0",
"build_hash" : "929b9739cae115e73c346cb5f9a6f24ba735a743",
"build_timestamp" : "2015-07-16T14:31:07Z",
"build_snapshot" : false,
"lucene_version" : "4.10.4"
},
"tagline" : "You Know, for Search"
}
|
ES环境配置
ES的配置文件在conf/elasticsearch.yml。
master节点、data节点和river节点都在该配置文件里面配备。(river节点在后面介绍)
master节点的配备如下:
cluster.name: test_cluster1 ##cluster名
node.name: " test_node_196" ##节点名称
node.master: true ##是否是master节点
node.data: true ##该节点上是否保存数据
index.number_of_replicas: 1 ##备份的数量,这里设为1
path.data: /opt/esdata ##该节点上数据存储的path
transport.tcp.port: 9300 ##tcp的端口号
http.port: 9200 ##http的端口号
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.timeout: 3s ##节点间自动发现的响应时间
discovery.zen.ping.unicast.hosts: ["localhost"] ##节点间自动发现,master节点为localhost
|
data节点的配备如下
cluster.name: test_cluster1 ##cluster名,注意和master节点保持一致
node.name: " test_node_197"
node.master: false ##不是master节点
node.data: true
index.number_of_replicas: 1
path.data: /opt/esdata
transport.tcp.port: 9300
http.port: 9200
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.timeout: 3s ##节点间自动发现的响应时间
discovery.zen.ping.unicast.hosts: ["10.19.220.196"] ##节点间自动发现,这里要写master节点的IP,多个要用逗号隔开
|
注意:1.为了测试,多个node可以在同一台服务器上启动,但通常一个服务器只放一个node。
2.系统启动时,node会使用广播来发现一个现有的cluster,并且试图加入该cluster
3.配置文件中的冒号后要加空格,否则启动es时会报load配置文件发生错误
4.为了防止脑裂,需要将discovery.zen.minimum_master_nodes设为[master节点数/2+1]。
关于脑裂,请参见http://blog.trifork.com/2013/10/24/how-to-avoid-the-split-brain-problem-in-elasticsearch/
5.当将一个节点的index.number_of_replicas从0变为1时,ES中现有的索引的分片仍然是没有备份的,但修改后新建的索引的分片都有一个备份
使用curl操作es
下面是一些通过curl操作es的例子,_cat用于查看,_update用于更新,_bulk用于批量操作,_search用于查询。
具体的就不一一解释了。可参见官网:https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html
##To check the cluster health
curl 'localhost:9200/_cat/health?v'
##get a list of nodes in our cluster
curl 'localhost:9200/_cat/nodes?v'
##take a peek at our indices
curl 'localhost:9200/_cat/indices?v'
##create an index named "customer"
curl -XPUT 'localhost:9200/customer?pretty'
##index a simple customer document into the customer index, "external" type, with an ID of 1
curl -XPUT 'localhost:9200/customer/external/1?pretty'-d ' { "name": "John Doe" }'
##retrieve that document
curl -XGET 'localhost:9200/customer/external/1?pretty'
##delete the index
curl -XDELETE 'localhost:9200/bankfortest?pretty'
##update our previous document
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty'-d ' { "doc": { "name": "Jane Doe", "age": 20 } }'
##注意:要运行下面的语句,需要在配置文件中加上“script.disable_dynamic: false”
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty'-d ' { "script" : "ctx._source.age += 5" }'
##delete multiple documents that match a query condition
curl -XDELETE 'localhost:9200/customer/external/_query?pretty'-d ' { "query": { "match": { "name": "John" } } }'
##批量操作
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty'-d ' {"index":{"_id":"5"}} {"name": "John Doe" } {"index":{"_id":"6"}} {"name": "Jane Doe" } '
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty'-d ' {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}} '
curl -XPOST 'localhost:9200/bankfortest/account/_bulk?pretty' --data-binary @accounts.json
##查询
curl 'localhost:9200/bank/_search?q=*&pretty'
curl -XPOST 'localhost:9200/bank/_search?pretty'-d ' { "query": { "match_all": {} } }'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "account_number": 20 } }
}'
|
2.常用的插件head,bigdesk,essql
首先请到官网上下载这些插件,安装指令如下:
bin/plugin --url file:///path/to/plugin --install plugin-name
|
head插件相当于es的一个管理控制台,可以看到es中节点、数据等内容
访问地址:http://ip:9600/_plugin/head/
bigdesk插件相当于一个es的监控插件,访问地址http://ip:9200/_plugin/bigdesk/
essql插件是一个sql插件,你可以使用普通sql语句去访问es里面的数据。
访问地址:http://ip:9200/_plugin/essql/
3.从es中读取数据
es中数据读取有两种方法:一种是通过restful API,一种是通过TransportClient,这里主要介绍第二种方式,效率更高,更易编码。
下面是一些简单的增删改查的例子。
(具体的ES的Java API请参照官网https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-api.html)
public class EsIndexTest {
public static Client client;
@Before
public void init() {
TransportClient transportClient = new TransportClient();
Settings settings = ImmutableSettings.settingsBuilder().put( "cluster.name" , " test_cluster1" ).build();
transportClient = new TransportClient(settings);
//这里的端口号要与配置文件中的transport.tcp.port保持一致,否则会出现NoNodeAvailableException
client = transportClient.addTransportAddress( new InetSocketTransportAddress(
"10.19.220.196" , 9300 ));
}
public static DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
"yyyy-MM-dd'T'HH:mm:ss.SSS" ).withZone(
DateTimeZone.forOffsetHours( 8 ));
public static String obj2JsonData() {
String jsonData = null ;
try {
// 使用XContentBuilder创建json数据
Date date = new Date();
//Gson gson = new Gson();
//System.err.println("date-->" + gson.toJson(date));
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
jsonBuilder
.startObject()
.field( "id" , 78 )
.field( "name" , " test" )
.field( "funciton" , date, dateFormatter)
.endObject();
jsonData = jsonBuilder.string();
System.out.println(jsonData);
} catch (IOException e) {
e.printStackTrace();
}
return jsonData;
}
@Test
//创建一个index,并向里面写入json数据
public void createIndexResponse() {
String jsonData = obj2JsonData();
IndexRequestBuilder ib = client.prepareIndex( "bank2" , "account2" ).setSource(
jsonData);
IndexResponse response = ib.execute().actionGet();
// Index name
String _index = response.getIndex();
System.out.println(_index);
// Type name
String _type = response.getType();
System.out.println(_type);
// Document ID (generated or not)
String _id = response.getId();
System.out.println(_id);
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
System.out.println(_version);
// isCreated() is true if the document is a new one, false if it has been updated
boolean created = response.isCreated();
System.out.println(created);
}
@Test
//更新操作,为既有的index添加一列
public void testUpdateIndex() throws IOException, InterruptedException, ExecutionException{
UpdateRequest updateRequest = new UpdateRequest( "bank2" , "account2" , "AU_04YOqvDFJcKoDMaDT" )
.doc(XContentFactory.jsonBuilder()
.startObject()
.field( "gender" , "male" )
.endObject());
client.update(updateRequest).get();
/*IndexRequest indexRequest = new IndexRequest("bank2", "account2", "2")
.source(XContentFactory.jsonBuilder()
.startObject()
.field("name", "Joe Smith")
//.field("gender", "male")
.endObject());
UpdateRequest updateRequest = new UpdateRequest("bank2", "account2", "2")
.doc(XContentFactory.jsonBuilder()
.startObject()
.field("gender", "female")
.endObject())
.upsert(indexRequest);
client.update(updateRequest).get();*/
}
@Test
//查询
public void testSearchIndex(){
SearchResponse response = client.prepareSearch( "bankfortest" )
.setTypes( "account" )
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termQuery( "lastname" , "fox" )) // Query
.setFrom( 0 ).setSize( 10 ).setExplain( true ).execute().actionGet();
SearchHits hits = response.getHits();
System.out.println(hits.getTotalHits());
for ( int i = 0 ; i < hits.getHits().length; i++) {
System.out.println(hits.getHits()[i].getSourceAsString());
}
}
@Test
//删除一条记录
public void testDeleteIndex(){
DeleteResponse response = client.prepareDelete( "bank1" , "account1" , "AU_02Z00vDFJcKoDMaDS" )
.execute()
.actionGet();
}
}
|
elasticsearch环境搭建与使用相关推荐
- Elasticsearch环境搭建和介绍(Windows)
一.Elasticsearch介绍和安装 1.1 介绍 Elastic Elastic官网:https://www.elastic.co/cn/ Elastic有一条完整的产品线:Elasticse ...
- Elasticsearch环境搭建
Elasticsearch简介 Elasticsearch是一个实时的分布式搜索和分析引擎.可以帮助你用前所未有的速度去处理大规模数据. 它可以用于全文检索,结构化搜索以及分析. Elasticsea ...
- ElasticSearch环境搭建 ElasticSearch-Header安装 分词器安装 ElasticSearch7.13.2 集成SpringBoot
ES 学习笔记 致敬kuangshen ES安装 Linux集群安装 1.解压安装包 #以7.13.2为例 tar zxvf elasticsearch-7.13.2-linux-x86_64.tar ...
- ElasticSearch探索之路(六)实战:环境搭建、REST、CRUD、Search
文章目录 环境搭建 搭建Elasticsearch环境 搭建Kibana环境 基本操作 RESTful CRUD 创建索引 插入文档 查询文档 更新文档 删除文档和索引 Search 查询方式 URL ...
- 图数据库JanusGraph实战[6]: JanusGraph+HBase+ElasticSearch的环境搭建
图数据库JanusGraph实战[6]: JanusGraph+HBase+ElasticSearch的环境搭建 作者:胡佳辉(家辉) 日期:2019年01月14日 CSDN博客:https://b ...
- ElasticSearch入门——环境搭建
文章目录 一.ElasticSearch环境搭建 1.解压 2.修改配置 (1)jvm.options (2)elasticsearch.yml 3.启动ES (1)新增用户并授权 (2)启动es ( ...
- Elasticsearch集群环境搭建
一.ES集群管理 ES是如何解决高并发 ES是一个分布式全文检索框架,隐藏了复杂的处理机制,核心内容 分片机制.集群发现.分片负载均衡请求路由. 二.ES基本概念名词 Cluster 代表一个集群,集 ...
- elasticsearch 6.2.4和elasticsearch-head环境搭建 使用docker-compose方式
elasticsearch 6.2.4和elasticsearch-head测试环境搭建 使用docker-compose方式 一 背景说明 对于新手来说搭建一个elasticsearch的测试环境稍 ...
- 环境搭建之日志搜集/采集服务系统对比(logtail/hadoop/elk/splunk/appender)
随着公司日益增长的业务,日志增长较快,各种bug层出不穷.为了快速定位问题以及做告警/数据分析等问题,就针对性的查看/搭建了几种日志服务系统. 可能整理的不是那么正确或者详尽.但也记录一下,方便日后回 ...
最新文章
- python项目实例代码-python开源项目及示例代码
- linux安装jdk1.7
- 石川es6课程---1-2、ES6简介
- NFS服务器是什么?(Network File System 网络文件系统)(远程主机间 mount 挂载目录)(ubuntu:nfs-kernel-server)
- VTK:可视化算法之HeadSlice
- 阿里安全猎户座实验室(Alibaba Orion Security Lab)简介
- Hive高级用法汇总
- css3修改透明png颜色
- windows2003安全设置
- 构建之法第4.17章读书笔记
- 浅谈:Hadoop、spark、SaaS、PaaS、IaaS、云计算
- Alpha冲刺(2/10)——2019.4.24
- Linux系统管理-(12)-网络配置IP命令
- android 创建typeface 对象,java – 检查Android中的Typeface对象的族
- 使用源代码编译安装基于LAMP的网站架构
- 各浏览器下图片垂直居中的方法:
- 15. Bypass 360主机卫士SQL注入防御(多姿势)
- [转载] 心目中的编程高手-----后学者以自励
- WE WANT U ┃中天微 国防科技大学2017届硕士专场校园宣讲会
- y410p linux 网卡,关于Y400、Y500、Y410P、Y510P无线网卡连接在Win8下经常受限的解决方法...