(只寫我的做法)
1、讀取數(shù)據(jù)庫(kù)中或者文件中的數(shù)據(jù)(本次是mysql中有相應(yīng)的數(shù)據(jù))并且格式化到一個(gè)json文件中
代碼如下:
@GetMapping("/toJson")
public ResultModel toJson(@RequestParam String sql){
? Map sqlmap = new HashMap<>();
? sqlmap.put("selectSQL",sql);
? List> ll = caserecordService.executeSelect(sqlmap);
? FileOutputStream o = null;
? File f = new File("C:\\Users\\Administrator\\Desktop\\data3.json");
? try {
? ? o = new FileOutputStream(f);
? for (Map map :ll){
? ? ? o.write(("{ \"index\" : { \"_index\" : \"infos\", \"_type\" : \"court_info\", \"_id\" : \""+String.valueOf(map.get("id"))+"\" } }"+"\n").getBytes("utf-8"));
? ? ? o.write((JSON.toJSONString(map)+"\n").getBytes("utf-8"));
? }
? } catch (IOException e) {
? ? e.printStackTrace();
? }finally {
? ? try {
? ? ? o.close();
? ? } catch (IOException e) {
? ? ? e.printStackTrace();
? ? }
? }
? return null;
}
獲得的文件為(內(nèi)容片段):

數(shù)據(jù)庫(kù)中:

2、將data.json上傳到服務(wù)器
3、在json文件所在的目錄下運(yùn)行語(yǔ)句:
curl -XPOST ip:9200/_bulk --data-binary @data3.json