java向es中写入数据报错org.elasticsearch.action.ActionRequestValidationException

在尝试使用Java操作Elasticsearch时遇到批量写入数据报错,原因是使用的ES客户端版本(6.8.6)与实际ES服务器版本(7.6.1)不匹配,导致`type is missing`的问题。解决方法是将ES客户端版本更新到与服务器相同的7.6.1版本。更新后重新启动并测试,问题得到解决。
部署运行你感兴趣的模型镜像

java操作es写入数据报错如下:

org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: type is missing;2: type is missing;3: type is missing;4: type is missing;5: type is missing;6: type is missing;7: type is missing;8: type is missing;9: type is missing;10: type is missing;11: type is missing;12: type is missing;13: type is missing;14: type is missing;15: type is missing;16: type is missing;17: type is missing;18: type is missing;19: type is missing;20: type is missing;21: type is missing;22: type is missing;23: type is missing;24: type is missing;25: type is missing;26: type is missing;27: type is missing;28: type is missing;29: type is missing;30: type is missing;
at org.elasticsearch.action.bulk.BulkRequest.validate(BulkRequest.java:614) ~[elasticsearch-6.8.6.jar:6.8.6]
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1730) ~[elasticsearch-rest-high-level-client-6.8.6.jar:6.8.6]

后分析查看报错信息发现,打印日志中使用的es版本为6.8.6(2.2.5.RELEASE版本默认使用的es版本),而我们使用的7.6.1中的一些调用方式,所以导致报错。

解决:修改es版本为7.6.1

再次启动,测试,ok问题解决。

您可能感兴趣的与本文相关的镜像

Yolo-v8.3

Yolo-v8.3

Yolo

YOLO(You Only Look Once)是一种流行的物体检测和图像分割模型,由华盛顿大学的Joseph Redmon 和Ali Farhadi 开发。 YOLO 于2015 年推出,因其高速和高精度而广受欢迎

Java 业务代码中同时向 MySQL 和 Elasticsearch 写入数据有多种实现方法,下面结合常见思路与代码示例进行说明: ### 应用层双写(双写模式) 这是一种较为直接的方法,在业务代码里依次完成向 MySQL 和 Elasticsearch 写入数据的操作。以下是示例代码: ```java import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class DualWriteExample { private static final String MYSQL_URL = "jdbc:mysql://localhost:3306/your_database"; private static final String MYSQL_USER = "your_username"; private static final String MYSQL_PASSWORD = "your_password"; private static final String ES_INDEX = "your_index"; private RestHighLevelClient esClient; public DualWriteExample(RestHighLevelClient esClient) { this.esClient = esClient; } public boolean saveData(String data) { Connection mysqlConnection = null; PreparedStatement preparedStatement = null; try { // 写入 MySQL mysqlConnection = DriverManager.getConnection(MYSQL_URL, MYSQL_USER, MYSQL_PASSWORD); String mysqlQuery = "INSERT INTO your_table (data_column) VALUES (?)"; preparedStatement = mysqlConnection.prepareStatement(mysqlQuery); preparedStatement.setString(1, data); preparedStatement.executeUpdate(); // 写入 Elasticsearch IndexRequest indexRequest = new IndexRequest(ES_INDEX); indexRequest.source("{\"data\": \"" + data + "\"}", XContentType.JSON); IndexResponse indexResponse = esClient.index(indexRequest, RequestOptions.DEFAULT); return true; } catch (SQLException | java.io.IOException e) { // 出现异常时进行相应处理,如 ES 数据回滚等 e.printStackTrace(); return false; } finally { try { if (preparedStatement != null) preparedStatement.close(); if (mysqlConnection != null) mysqlConnection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在上述代码中,`saveData` 方法先将数据写入 MySQL,接着再将相同的数据写入 Elasticsearch。若在写入过程中出现异常,可依据具体情况进行数据回滚等操作。 ### 使用消息队列(MQ)实现数据同步 这种方法的思路是在业务代码里将增、删、改操作的消息发送到消息队列,再由消费者监听消息并更新 Elasticsearch 中的数据。以下是简单示例: ```java import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; @Service public class DataService { private static final String MYSQL_URL = "jdbc:mysql://localhost:3306/your_database"; private static final String MYSQL_USER = "your_username"; private static final String MYSQL_PASSWORD = "your_password"; @Autowired private RabbitTemplate rabbitTemplate; public boolean saveData(String data) { Connection mysqlConnection = null; PreparedStatement preparedStatement = null; try { // 写入 MySQL mysqlConnection = DriverManager.getConnection(MYSQL_URL, MYSQL_USER, MYSQL_PASSWORD); String mysqlQuery = "INSERT INTO your_table (data_column) VALUES (?)"; preparedStatement = mysqlConnection.prepareStatement(mysqlQuery); preparedStatement.setString(1, data); preparedStatement.executeUpdate(); // 发送消息到 MQ rabbitTemplate.convertAndSend("your_exchange", "your_routing_key", data); return true; } catch (SQLException e) { e.printStackTrace(); return false; } finally { try { if (preparedStatement != null) preparedStatement.close(); if (mysqlConnection != null) mysqlConnection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在上述代码中,`saveData` 方法先把数据写入 MySQL,然后将数据作为消息发送到消息队列。之后,消费者监听消息队列并更新 Elasticsearch 中的数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值