Elasticsearch导出导入数据
1.概念回顾
2.基础操作
展示详细信息
GET:http://127.0.0.1:9200/_cat/indices?v
Java代码将文件导入到ES
package com.Graph.medicalgraph;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
//导入es
public class JSONDataImporter {
public static void main(String[] args) {
String jsonFilePath = "D:\\xx.json";
String indexName = "test01";
try (RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")))) {
BulkRequest bulkRequest = new BulkRequest();
BufferedReader reader = new BufferedReader(new FileReader(jsonFilePath));
String line;
int id = 0;
while ((line = reader.readLine()) != null) {
IndexRequest indexRequest = new IndexRequest(indexName);
indexRequest.source(line, XContentType.JSON);
indexRequest.id(Integer.toString(id++)); // 使用递增ID
bulkRequest.add(indexRequest);
}
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
if (bulkResponse.hasFailures()) {
System.out.println("Some documents failed to import.");
} else {
System.out.println("All documents imported successfully.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
全查询:http://127.0.0.1:9200/indexName_test01/_search