elasticsearch学习javaAPI:es的增删改查
来源:互联网 发布:js数组去重系统方法 编辑:程序博客网 时间:2024/06/04 17:51
1.环境要求:
1.jdk1.8以上
2.Maven依赖
<dependencies> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.2.2</version></dependency><dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.7</version></dependency><dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version></dependency> </dependencies>
2.通过TransportClient进行增上改查的业务
package com.roncoo.es.score.first;import java.io.IOException;import java.net.InetAddress;import org.elasticsearch.action.delete.DeleteResponse;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.action.update.UpdateResponse;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.common.xcontent.XContentFactory;import org.elasticsearch.transport.client.PreBuiltTransportClient;public class ProductCURD {@SuppressWarnings({ "unchecked", "resource" })public static void main(String[] args) throws Exception {//1.构建clientSettings settings = Settings.builder().put("cluster.name", "my-application").build();TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300));//2.进行操作//2.1增加,创建一个商品//addProdect(client);//2.2查询,获取商品//getProduct(client);//2.3修改,修改商品//updateProduct(client);//2.4删除,删除商品//deleteProduct(client);//3.关闭连接client.close();}/** * 创建一个商品 * @param client * @throws IOException */private static void addProdect(TransportClient client) throws IOException {IndexResponse response = client.prepareIndex("shop", "product", "1").setSource(XContentFactory.jsonBuilder().startObject().field("name", "Thinking in java").field("price", "99").field("author","jack").field("country", "us").endObject()).get();System.out.println(response.getResult());}/** * 查看商品信息 * @param client */private static void getProduct(TransportClient client) {GetResponse response = client.prepareGet("shop","product","1").get();System.out.println(response.getSourceAsString());}/** * 修改商品 * @param client * @throws IOException */private static void updateProduct(TransportClient client) throws IOException {UpdateResponse response = client.prepareUpdate("shop","product","1").setDoc(XContentFactory.jsonBuilder().startObject().field("price", "9.9").endObject()).get();System.out.println(response.getResult());}/** * 删除 一个商品 * @param client */private static void deleteProduct(TransportClient client) {DeleteResponse response = client.prepareDelete("shop", "product", "1").get();System.out.println(response.getResult());}}
阅读全文
0 0
- elasticsearch学习javaAPI:es的增删改查
- ElasticSearch的增删改查
- Zookeeper JavaApi 增删改查
- es增删改查
- java操作ElasticSearch(es)进行增删查改操作
- ElasticSearch restful api 的客户端PostMan实现对ES的增删改查
- Elasticsearch增删改查
- ElasticSearch 增删改查
- elasticsearch增删改查
- [日志]es的增删改查
- ElasticSearch的增删改查API介绍
- Elasticsearch系列(四)----Elasticsearch索引文档的增删改查
- elasticsearch学习javaAPI:es简单搜索
- ElasticSearch Java 增删改查
- Elasticsearch 5.2 增删改查
- 分布式搜索elasticsearch 索引文档的增删改查 入门
- 分布式搜索elasticsearch 索引文档的增删改查 入门
- Elasticsearch 单模式下API的增删改查操作
- Split Array Largest Sum
- 杂谈
- HDU 2016 数据的交换输出
- 三菱V3菱悦智能遥控匹配详细的(两种)方法 配钥匙
- Netty in action—Netty中的ByteBuf
- elasticsearch学习javaAPI:es的增删改查
- 外部中断
- python网络编程(线程)
- 分布式系统事务一致性
- Eclipse中设置Servlet模板
- 排序算法(六)---- 归并排序
- 【leetcode】219. Contains Duplicate II(Python & C++)
- iBATIS SQL Maps之Inline Parameter Map。
- 统计数字问题(未完待续)