elasticsearch的单节点和分布式的安装及其操作(使用命令和使用代码)
2018-09-19 17:24
645 查看
关于安装是在另一个博客上面写的 不知道怎么同步 有兴趣的可以点击链接去看下
博客地址:elasticsearch单节点和分布式的安装
在文章开始之前、先明确三个概念1、索引
2、类型
3、文档
对比于数据库中,索引就是一个数据库、类型就是数据库中的某张表、文档也就是表中具体的记录。抽象点来看,索引抽象成一个人、人又分为男人和女人(就是类型)、然后男人有姓名、年龄、身高等(就是文档)。
使用postman进行操作
向es(elasticsearch,下面全部用es简称)发送操作的请求是一个RestFui风格的,类似下面这种: http://<ip>:<port>/<索引>/<类型>/<文档id> 下面使用post向es发送请求进行索引的创建
请求url:127.0.0.1:9200/people
请求入参:
{ "settings": { "index.number_of_shards":3, "index.number_of_replicas":1 }, "mappings": { "man": { "properties":{ "name":{ "type":"text" }, "height":{ "type":"text" }, "age":{ "type":"integer" } } } } }
上述的过程就是创建了一个索引为people、类型为man、类型中的properties有name、height、age这个字段
响应参数:
{ "acknowledged": true, "shards_acknowledged": true }
插入数据有两种方式:
第一种 指定id来插入:
请求url:127.0.0.1:9200/people/man/1 (1就是我们指定的id)
请求入参json串:
{ "name":"shuaige", "age" : 18, "height": "188cm" }
响应:
{ "_index": "people", "_type": "man", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "created": true }
自动生成id:
请求url:127.0.0.1:9200/people/man
请求入参:
{ "name":"laoshiren", "age" : 30, "height": "166cm" }
响应参数:
{ "_index": "people", "_type": "man", "_id": "AWXxFS1S66ULpPmE4hFv", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "created": true }
此时就创建了一个id为AWXxFS1S66ULpPmE4hFv的数据,其它的操作同理,都是像es发请求,只不过入参改改就可以
使用java代码进行操作
1、首先、构建一个springboot'工程、引入es的依赖
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.5.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency>
新建一个配置文件的类,初始化一个es的客户端
@Configuration public class ElasticSearchClientConfig { @Bean public TransportClient client() throws Exception { InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300);//tcp端口是9300 Settings settings = Settings.builder().put("cluster.name","sanxiongdi").build(); TransportClient client = new PreBuiltTransportClient(settings); client.addTransportAddress(node); return client; } }
最后在一个controller里面对es进行操作
首先注入初始化es客户端:
@Autowired private TransportClient client;
插入数据:
public ResponseEntity addBook(@RequestParam("name")String name, @RequestParam("height")String height,@RequestParam("age")int height){ try { XContentBuilder content = XContentFactory.jsonBuilder().startObject() .field("name",name) .field("height",height) .field("age",age) .endObject(); IndexResponse response = this.client.prepareIndex("person","man").setSource(content).get(); log.info("插入数据成功={}",response); return new ResponseEntity(response.getId(),HttpStatus.OK); }catch (Exception e){ log.error("插入数据异常={}", e); return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); } }
最后为方法配置路由去访问就可以,其它的操作同理的。
代码的地址:代码下载连接
---恢复内容结束---
在文章开始之前、先明确三个概念
1、索引
2、类型
3、文档
对比于数据库中,索引就是一个数据库、类型就是数据库中的某张表、文档也就是表中具体的记录。抽象点来看,索引抽象成一个人、人又分为男人和女人(就是类型)、然后男人有姓名、年龄、身高等(就是文档)。
使用postman进行操作
向es(elasticsearch,下面全部用es简称)发送操作的请求是一个RestFui风格的,类似下面这种: http://<ip>:<port>/<索引>/<类型>/<文档id> 下面使用post向es发送请求进行索引的创建
请求url:127.0.0.1:9200/people
请求入参:
{ "settings": { "index.number_of_shards":3, "index.number_of_replicas":1 }, "mappings": { "man": { "properties":{ "name":{ "type":"text" }, "height":{ "type":"text" }, "age":{ "type":"integer" } } } } }
上述的过程就是创建了一个索引为people、类型为man、类型中的properties有name、height、age这个字段
响应参数:
{ "acknowledged": true, "shards_acknowledged": true }
插入数据有两种方式:
第一种 指定id来插入:
请求url:127.0.0.1:9200/people/man/1 (1就是我们指定的id)
请求入参json串:
{ "name":"shuaige", "age" : 18, "height": "188cm" }
响应:
{ "_index": "people", "_type": "man", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "created": true }
自动生成id:
请求url:127.0.0.1:9200/people/man
请求入参:
{ "name":"laoshiren", "age" : 30, "height": "166cm" }
响应参数:
{ "_index": "people", "_type": "man", "_id": "AWXxFS1S66ULpPmE4hFv", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "created": true }
此时就创建了一个id为AWXxFS1S66ULpPmE4hFv的数据,其它的操作同理,都是像es发请求,只不过入参改改就可以
使用java代码进行操作
1、首先、构建一个springboot'工程、引入es的依赖
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.5.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency>
新建一个配置文件的类,初始化一个es的客户端
@Configuration public class ElasticSearchClientConfig { @Bean public TransportClient client() throws Exception { InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300);//tcp端口是9300 Settings settings = Settings.builder().put("cluster.name","sanxiongdi").build(); TransportClient client = new PreBuiltTransportClient(settings); client.addTransportAddress(node); return client; } }
最后在一个controller里面对es进行操作
首先注入初始化es客户端:
@Autowired private TransportClient client;
插入数据:
public ResponseEntity addBook(@RequestParam("name")String name, @RequestParam("height")String height,@RequestParam("age")int height){ try { XContentBuilder content = XContentFactory.jsonBuilder().startObject() .field("name",name) .field("height",height) .field("age",age) .endObject(); IndexResponse response = this.client.prepareIndex("person","man").setSource(content).get(); log.info("插入数据成功={}",response); return new ResponseEntity(response.getId(),HttpStatus.OK); }catch (Exception e){ log.error("插入数据异常={}", e); return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); } }
最后为方法配置路由去访问就可以,其它的操作同理的。
代码的地址:代码下载连接
相关文章推荐
- Redis常用数据类型介绍、使用场景及其操作命令
- linux 7z命令安装使用及其交叉编译移植到arm linux平台
- Redis常用数据类型介绍、使用场景及其操作命令
- 使用curl命令操作elasticsearch
- Redis常用数据类型介绍、使用场景及其操作命令
- hadoop学习之总目录(1):安装和使用完全分布式hadoop-2.7.2及其家族其他成员
- windows上maven安装与使用(基础操作命令)并与eclipse(galileo版)整合教程
- Redis常用数据类型介绍、使用场景及其操作命令
- [转]使用curl命令操作elasticsearch And 使用http 查询ES
- [大数据]-Elasticsearch5.3.1+Kibana5.3.1从单机到分布式的安装与使用<2>
- asp.net TreeView安装、使用(如何将TreeView打包发布)(带CheckBox选择框的TreeView的初始化,TreeView客户端操作:选择父节点后自动选择所有子节点,子节点选择后自动选择父节点)(TreeView节点精确定位)
- Redis常用数据类型介绍、使用场景及其操作命令
- Linux使用基础一:安装与常用的操作使用命令
- 使用curl命令操作elasticsearch
- 学习用Node.js和Elasticsearch构建搜索引擎(3):使用curl命令操作elasticsearch
- mongodb学习(-)安装及其简单的操作命令
- Git的使用——安装/添加/删除/历史记录/命令记录的操作
- 使用curl命令操作elasticsearch
- asp.net TreeView安装、使用(如何将TreeView打包发布)(带CheckBox选择框的TreeView的初始化,TreeView客户端操作:选择父节点后自动选择所有子节点,子节点选择后自动选择父节点)(TreeView节点精确定位)2005-12-23更新