![](/img/trans.png)
[英]How to execute BULK UpdateByQuery using java api - Elasticsearch 5.x
[英]loading data to Elasticsearch v7.3 using Bulk API
我需要將數據加載到 elasticsearch 索引。 我正在使用 elasticsearch 的 BULK API 將 JSON 加載到索引。
private String FOLDER_PATH = "src/main/resources/allJsons";
private String index = "test1";
private static final String TYPE = "test_type";
@Autowired
private RestHighLevelClient restHighLevelClient;
public String loadBulkData() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
AtomicInteger counter = new AtomicInteger();
try (Stream<Path> filePathStream = Files.walk(Paths.get(FOLDER_PATH))) {
filePathStream.forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
counter.getAndIncrement();
try {
String content = Files.readString(filePath);
JSONObject jsonObject1 = new JSONObject(content);
HashMap yourHashMap1 = new Gson().fromJson(jsonObject1.toString(), HashMap.class);
IndexRequest indexRequest = new IndexRequest(index, TYPE).source(yourHashMap1);
bulkRequest.add(indexRequest);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
try {
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return "Bulk data loaded to index " + index + "";
}
}
我有多個基於以下格式的 JSON
[
{
"Nutrient" : "Calories",
"Amount" : " 289.00",
"Unit" : " kcal"
}, {
"Nutrient" : "Fat",
"Amount" : " 17.35",
"Unit" : " g"
}
]
在運行代碼時它給了我錯誤,
org.springframework.web.util.NestedServletException:請求處理失敗; 嵌套異常是 org.json.JSONException: A JSONObject 文本必須以 '{' 在 1 [字符 2 第 1 行] 開頭
我認為數據在JSONArray
中,對於代碼,我們需要JSONObject
。 任何人都可以請指導如何做到這一點
您可以通過將 json 對象的 hashmap 傳遞給 Elasticsearch Bulk ZDB974238714CA8DE634A7CE1D03 來進行批量插入。 您可以通過 JSONParser 解析 JSON 文件來創建 Hashmap。
這是相同的代碼:
Integer id= 1;
//You need to call this method for inserting bulk documents which
// internally calls `createBulkRequest` and `parseObjectList` methods.
//This method uses JSONParser to parse your file and convert into JSONArray.
public String insertBulkDocuments() throws Exception {
Object obj = new JSONParser().parse(new FileReader(<path-of-file>));
JSONArray objList= (JSONArray) obj;
BulkRequest request = createBulkRequest(objList);
BulkResponse bulkresp=restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
return bulkresp.status().toString();
}
// Each JSONArray element that was obtained through first method
//is parsed individually through Gson and converted into you defined Object.
//This object is then converted to Map and passed to IndexRequest object.
private BulkRequest createBulkRequest(JSONArray objList) {
BulkRequest request = new BulkRequest();
objList.forEach( obj -> parseObjectList((JSONObject) obj, request,id++));
return request;
}
private void parseObjectList(JSONObject obj, BulkRequest request, int id) {
Gson gson = new GsonBuilder().create();
NutrientDocument doc = gson.fromJson(obj.toJSONString(), NutrientDocument .class);
Map<String, Object> documentMapper = objectMapper.convertValue(doc, Map.class);
IndexRequest indexRequest = new IndexRequest(<your-index-name>).id(Integer.toString(id)).source(documentMapper);
request.add(indexRequest);
}
您需要創建與 json 具有相同字段的自定義 object。 我創建了用於測試的 NutrientDocument,它與您的 JSON 具有相同的字段,並且我在parseObjectList
方法中使用了它。
public class NutrientDocument {
private String Nutrient;
private Float Amount;
private String Unit;
public String getNutrient() {
return Nutrient;
}
public void setNutrient(String nutrient) {
Nutrient = nutrient;
}
public Float getAmount() {
return Amount;
}
public void setAmount(Float amount) {
Amount = amount;
}
public String getUnit() {
return Unit;
}
public void setUnit(String unit) {
Unit = unit;
}
}
筆記:
對於每個文檔 elasticserach 生成唯一的id
。
為了創建我們自己的id
值而不是 Elasticsearch 自動生成的值,我們使用id
變量。 但是,如果你想 go 和 Elasticsearch 自動生成的數字,你可以在parseObjectList
方法中創建IndexRequest
object 並在我們傳遞的任何地方刪除id
變量。
IndexRequest indexRequest = new IndexRequest().source(documentMapper);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.