Java API 向 Elasticsearch 索引库添加订单数据
以下是使用 Java API 向索引库 order_index 中添加 Order(订单)对象数据的示例代码:
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import java.io.IOException;
public class OrderIndexer {
public static void main(String[] args) throws IOException {
// 创建RestHighLevelClient客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 创建订单数据
Order order1 = new Order('商品1', 99.9, '用户1');
Order order2 = new Order('商品2', 199.9, '用户2');
Order order3 = new Order('商品3', 299.9, '用户3');
Order order4 = new Order('商品4', 399.9, '用户4');
Order order5 = new Order('商品5', 499.9, '用户5');
// 使用XContentBuilder构建JSON数据
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.field('goodName', order1.getGoodName());
builder.field('goodPrice', order1.getGoodPrice());
builder.field('userId', order1.getUserId());
}
builder.endObject();
// 创建IndexRequest对象
IndexRequest request = new IndexRequest('order_index')
.id('1')
.source(builder);
// 使用IndexRequest对象向索引库中添加数据
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println('Add order1: ' + response.toString());
// 使用Map构建JSON数据
request = new IndexRequest('order_index')
.id('2')
.source('goodName', order2.getGoodName(),
'goodPrice', order2.getGoodPrice(),
'userId', order2.getUserId());
// 使用IndexRequest对象向索引库中添加数据
response = client.index(request, RequestOptions.DEFAULT);
System.out.println('Add order2: ' + response.toString());
// 使用Object构建JSON数据
request = new IndexRequest('order_index')
.id('3')
.source(order3);
// 使用IndexRequest对象向索引库中添加数据
response = client.index(request, RequestOptions.DEFAULT);
System.out.println('Add order3: ' + response.toString());
// 使用JSON字符串构建JSON数据
String json = '{"goodName": "' + order4.getGoodName() + '", "goodPrice": ' + order4.getGoodPrice() + ', "userId": "' + order4.getUserId() + '"}';
request = new IndexRequest('order_index')
.id('4')
.source(json, XContentType.JSON);
// 使用IndexRequest对象向索引库中添加数据
response = client.index(request, RequestOptions.DEFAULT);
System.out.println('Add order4: ' + response.toString());
// 使用JSON字节数组构建JSON数据
byte[] bytes = '{"goodName": "'.getBytes();
bytes = Bytes.concat(bytes, order5.getGoodName().getBytes());
bytes = Bytes.concat(bytes, '", "goodPrice": '.getBytes());
bytes = Bytes.concat(bytes, Double.toString(order5.getGoodPrice()).getBytes());
bytes = Bytes.concat(bytes, ', "userId": "'.getBytes());
bytes = Bytes.concat(bytes, order5.getUserId().getBytes());
bytes = Bytes.concat(bytes, '"}'.getBytes());
request = new IndexRequest('order_index')
.id('5')
.source(bytes, XContentType.JSON);
// 使用IndexRequest对象向索引库中添加数据
response = client.index(request, RequestOptions.DEFAULT);
System.out.println('Add order5: ' + response.toString());
// 关闭RestHighLevelClient客户端
client.close();
}
private static class Order {
private String goodName;
private double goodPrice;
private String userId;
public Order(String goodName, double goodPrice, String userId) {
this.goodName = goodName;
this.goodPrice = goodPrice;
this.userId = userId;
}
public String getGoodName() {
return goodName;
}
public double getGoodPrice() {
return goodPrice;
}
public String getUserId() {
return userId;
}
}
}
相关资源:
注意:
- 以上代码示例需要使用 Maven 或 Gradle 引入 Elasticsearch Java API 依赖库。
- 确保 Elasticsearch 服务已启动并正在运行。
- order_index 为索引库名称,可根据需要修改。
- 每个订单对象使用不同的 ID,方便后续查询。
- 为了清晰起见,示例代码中包含了打印响应信息,可根据实际需求进行调整。
原文地址: https://www.cveoy.top/t/topic/ofTQ 著作权归作者所有。请勿转载和采集!