java连接ElasticSearch集群操作
作者:java界的守门员 时间:2023-11-28 04:06:24
我就废话不多说了,大家还是直接看代码吧~
/*
*es配置类
*
*/
@Configuration
public class ElasticSearchDataSourceConfigurer {
private static final Logger LOG = LogManager.getLogger(ElasticSearchDataSourceConfigurer.class);
@Bean
public TransportClient getESClient() {
//设置集群名称
Settings settings = Settings.builder().put("cluster.name", "bigData-cluster").put("client.transport.sniff", true).build();
//创建client
TransportClient client = null;
try {
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300));//集群ip
LOG.info("ESClient连接建立成功");
} catch (UnknownHostException e) {
LOG.info("ESClient连接建立失败");
e.printStackTrace();
}
return client;
}
}
/**
* Simple to Introduction
*
* @Description: [添加类]
*/
@Repository
public class UserDaoImpl implements userDao {
private static final String INDEXNAME = "user";//小写
private static final String TYPENAME = "info";
@Resource
TransportClient transportClient;
@Override
public int addUser(User[] user) {
IndexResponse indexResponse = null;
int successNum = 0;
for (int i = 0; i < user.length; i++) {
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
String jsonValue = null;
try {
jsonValue = JsonUtil.object2JsonString(user[i]);
if (jsonValue != null) {
indexResponse = transportClient.prepareIndex(INDEXNAME, TYPENAME, str).setSource(jsonValue)
.execute().actionGet();
successNum++;
}
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return successNum;
}
}
/**
*批量插入
*/
public static void bathAddUser(TransportClient client, List<User> users) {
BulkRequestBuilder bulkRequest = transportClient.prepareBulk();
for (int i = 0; i < users.size(); i++) {
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
String jsonValue = null;
try {
jsonValue = JsonUtil.object2JsonString(users.get(i));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
bulkRequest.add(client.prepareIndex("user", "info", str).setSource(jsonValue));
// 一万条插入一次
if (i % 10000 == 0) {
bulkRequest.execute().actionGet();
}
System.out.println("已经插入第" + i + "多少条");
}
}
补充知识:使用java创建ES(ElasticSearch)连接池
1.首先要有一个创建连接的工厂类
package com.aly.util;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
/**
* EliasticSearch连接池工厂对象
* @author 00000
*
*/
public class EsClientPoolFactory implements PooledObjectFactory<RestHighLevelClient>{
@Override
public void activateObject(PooledObject<RestHighLevelClient> arg0) throws Exception {
System.out.println("activateObject");
}
/**
* 销毁对象
*/
@Override
public void destroyObject(PooledObject<RestHighLevelClient> pooledObject) throws Exception {
RestHighLevelClient highLevelClient = pooledObject.getObject();
highLevelClient.close();
}
/**
* 生产对象
*/
//@SuppressWarnings({ "resource" })
@Override
public PooledObject<RestHighLevelClient> makeObject() throws Exception {
//Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
RestHighLevelClient client = null;
try {
/*client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));*/
client = new RestHighLevelClient(RestClient.builder(
new HttpHost("192.168.1.121", 9200, "http"), new HttpHost("192.168.1.122", 9200, "http"),
new HttpHost("192.168.1.123", 9200, "http"), new HttpHost("192.168.1.125", 9200, "http"),
new HttpHost("192.168.1.126", 9200, "http"), new HttpHost("192.168.1.127", 9200, "http")));
} catch (Exception e) {
e.printStackTrace();
}
return new DefaultPooledObject<RestHighLevelClient>(client);
}
@Override
public void passivateObject(PooledObject<RestHighLevelClient> arg0) throws Exception {
System.out.println("passivateObject");
}
@Override
public boolean validateObject(PooledObject<RestHighLevelClient> arg0) {
return true;
}
}
2.然后再写我们的连接池工具类
package com.aly.util;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.elasticsearch.client.RestHighLevelClient;
/**
* ElasticSearch 连接池工具类
*
* @author 00000
*
*/
public class ElasticSearchPoolUtil {
// 对象池配置类,不写也可以,采用默认配置
private static GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
// 采用默认配置maxTotal是8,池中有8个client
static {
poolConfig.setMaxTotal(8);
}
// 要池化的对象的工厂类,这个是我们要实现的类
private static EsClientPoolFactory esClientPoolFactory = new EsClientPoolFactory();
// 利用对象工厂类和配置类生成对象池
private static GenericObjectPool<RestHighLevelClient> clientPool = new GenericObjectPool<>(esClientPoolFactory,
poolConfig);
/**
* 获得对象
*
* @return
* @throws Exception
*/
public static RestHighLevelClient getClient() throws Exception {
// 从池中取一个对象
RestHighLevelClient client = clientPool.borrowObject();
return client;
}
/**
* 归还对象
*
* @param client
*/
public static void returnClient(RestHighLevelClient client) {
// 使用完毕之后,归还对象
clientPool.returnObject(client);
}
}
来源:https://blog.csdn.net/baidu_16217779/article/details/71633284
标签:java,ElasticSearch,集群
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现线程安全单例模式的五种方式的示例代码
2023-09-26 16:41:23
普通对象使用spring容器中的对象的实现方法
2023-06-17 12:27:20
Android 8.0系统中应用图标的适配微技巧
2022-09-29 00:22:26
![](https://img.aspxhome.com/file/2023/0/138200_0s.jpg)
C#异步使用需要注意的几个问题
2023-03-05 04:33:08
SpringBoot 自定义+动态切换数据源教程
2021-10-13 21:30:39
![](https://img.aspxhome.com/file/2023/9/92649_0s.png)
Android10 分区存储的适配规则
2022-11-25 12:22:53
使用SpringBoot+EasyExcel+Vue实现excel表格的导入和导出详解
2023-07-18 18:15:14
SpringSecurity整合jwt权限认证的全流程讲解
2022-02-20 09:58:36
![](https://img.aspxhome.com/file/2023/0/113200_0s.png)
RandomAccessFile简介_动力节点Java学院整理
2021-12-18 09:02:15
![](https://img.aspxhome.com/file/2023/3/85403_0s.png)
mybatis sum(参数) 列名作为参数的问题
2022-06-16 01:45:44
JavaWeb中上传和下载文件实例代码
2023-11-04 10:24:00
![](https://img.aspxhome.com/file/2023/5/78855_0s.png)
C#导出pdf的实现方法(浏览器不预览直接下载)
2023-11-04 05:48:10
C# Websocket连接实现wss协议
2022-09-02 08:20:41
MyBatis图文并茂讲解注解开发一对多查询
2023-02-18 08:18:40
![](https://img.aspxhome.com/file/2023/0/67990_0s.png)
原来Java中有两个ArrayList
2023-06-27 11:49:40
Java swing读取txt文件实现学生考试系统
2021-06-13 17:41:02
![](https://img.aspxhome.com/file/2023/6/79906_0s.jpg)
普通类注入不进spring bean的解决方法
2021-06-07 19:22:41
![](https://img.aspxhome.com/file/2023/9/125659_0s.png)
springboot aop配合反射统一签名验证实践
2023-08-04 08:14:07
![](https://img.aspxhome.com/file/2023/7/76257_0s.png)
spring boot线上日志级别动态调整的配置步骤
2022-09-19 01:57:45
![](https://img.aspxhome.com/file/2023/2/94312_0s.png)
C#实现二叉排序树代码实例
2021-10-10 06:26:12
![](https://img.aspxhome.com/file/2023/3/102223_0s.png)