elasticsearch的单节点和分布式的安装及其操作(使用命令和使用代码)

关于安装是在另一个博客上面写的 不知道怎么同步 有兴趣的可以点击链接去看下 博客地址:elasticsearch单节点和分布式的安装 在文章开始之前、先明确三个概念 1、索引 2、类型 3、文档 对比于数据库中,索引就是一个数据库、类型就是数据库中的某张表、文档也就是表中具体的记录。抽象点来看,索引抽象成一个人、人又分为男人和女人(就是类型)、然后男人有姓名、年龄、身高等(就是文档)。 使用postman进行操作 向es(elasticsearch,下面全部用es简称)发送操作的请求是一个RestFui风格的,类似下面这种: http://:/<索引>/<类型>/<文档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的依赖 org.elasticsearch.client transport 5.5.2 org.apache.logging.log4j log4j-core 2.7 新建一个配置文件的类,初始化一个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://:/<索引>/<类型>/<文档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的依赖 org.elasticsearch.client transport 5.5.2 org.apache.logging.log4j log4j-core 2.7 新建一个配置文件的类,初始化一个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); } } 最后为方法配置路由去访问就可以,其它的操作同理的。 代码的地址:代码下载连接 evildoerDbhttps://www.cnblogs.com/evildoerdb/p/9675714.html
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信