elasticsearch学习javaAPI:es的增删改查

本文介绍如何使用 Java 和 Elasticsearch 的 TransportClient 进行商品信息的创建、读取、更新和删除 (CRUD) 操作。示例展示了如何设置客户端连接、执行基本的 CRUD 功能并关闭连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.构建client
		Settings 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());
	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值