概述
1、pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>Es</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- es的客户端-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.0</version>
</dependency>
<!-- 依赖2.x的log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
2、CRUD
package com.es;
import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CRUD {
private final static String host1="hdp-1";
private final static String host2="hdp-2";
private final static String host3="hdp-3";
public final static int port = 9300;
private TransportClient client = null;
@Before
public void init() throws UnknownHostException {
Settings.Builder builder = Settings.builder();
Settings.Builder cluster = builder.put("cluster.name", "my-Es");
Settings se = cluster.build();
client = new PreBuiltTransportClient(se).addTransportAddresses(
new TransportAddress(InetAddress.getByName(host1),port),
new TransportAddress(InetAddress.getByName(host2),port),
new TransportAddress(InetAddress.getByName(host3),port)
);
}
/**
* closeConnection(关闭连接)
*/
@After
public void closeConnection(){
if (client != null){
client.close();
}
}
/**
* testIndex:(创建索引)
*/
@Test
public void testCreateIndex(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("orderNo",new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "001");
jsonObject.put("orderName","购买元宝");
jsonObject.put("orderTime",new Date());
jsonObject.put("price",10000);
jsonObject.put("ip","192.168.234.129");
IndexResponse response = client.prepareIndex("rxpay","order").setSource(jsonObject.toJSONString(), XContentType.JSON).get();
System.out.println("索引名称:" + response.getIndex());
System.out.println("类型:" + response.getType());
System.out.println("文档ID:" + response.getId());
System.out.println("当前实例状态:" + response.status());
}
/**
* 查询
*/
@Test
public void Query(){
GetRequestBuilder get = client.prepareGet();
get.setIndex("rxpay");
get.setType("order");
get.setId("UBimPm4BfdPlsY3OArZ4");
ActionFuture<GetResponse> execute = get.execute();
GetResponse response = execute.actionGet();
System.out.println(response);
}
/**
* update更新
*/
@Test
public void testUpdate(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("title","class5");
jsonObject.put("num","40");
jsonObject.put("stat","good");
UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate();
updateRequestBuilder.setIndex("student");
updateRequestBuilder.setType("banji");
updateRequestBuilder.setId("3");
UpdateRequestBuilder updateRequest = updateRequestBuilder.setDoc(jsonObject.toJSONString(), XContentType.JSON);
UpdateResponse response = updateRequest.get();
System.out.println(response);
}
/**
* 删除
*/
@Test
public void testdelete(){
DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete();
deleteRequestBuilder.setIndex("student");
deleteRequestBuilder.setType("banji");
deleteRequestBuilder.setId("3");
DeleteResponse deleteResponse = deleteRequestBuilder.get();
System.out.println(deleteResponse);
}
}
最后
以上就是儒雅蜜粉为你收集整理的ElasticSearch6.3.1的java入门程序(CRUD)的全部内容,希望文章能够帮你解决ElasticSearch6.3.1的java入门程序(CRUD)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复