将mysql中的数据向es中批量添加数据

(只写我的做法)

1、读取数据库中或者文件中的数据(本次是mysql中有相应的数据)并且格式化到一个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;

}

获得的文件为(内容片段):

数据库中:

2、将data.json上传到服务器

3、在json文件所在的目录下运行语句:

curl -XPOST ip:9200/_bulk --data-binary @data3.json

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容