ElasticsearchImportExport { public void bulkify(Path input, Path output) throws IOException { List<String> outRecs = new ArrayList<String>(); try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(input.toFile()), StandardCharsets.UTF_8))) { String line; while ((line = br.readLine()) != null) { Map<String, Object> inDoc = JSONUtils.INSTANCE .load(line, JSONUtils.MAP_SUPPLIER); Object id = inDoc.get("_id"); Object type = inDoc.get("_type"); String createRaw = String .format("{ \"create\" : { \"_id\": \"%s\", \"_type\": \"%s\" } }", id, type); String outData = JSONUtils.INSTANCE.toJSON(inDoc.get("_source"), false); outRecs.add(createRaw); outRecs.add(outData); } } try (BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output.toFile()), StandardCharsets.UTF_8))) { for (String line : outRecs) { br.write(line); br.write(System.lineSeparator()); } } }  static void main(String[] args); void bulkify(Path input, Path output);  }
@Test public void bulk_exporter_writes_elasticsearch_records_in_bulk_import_format() throws Exception { Path recordsFile = Paths.get(tempDir.getPath(), "inputfile.json"); Path outputFile = Paths.get(tempDir.getPath(), "outputfile.json"); TestUtils.write(recordsFile.toFile(), records); ElasticsearchImportExport tool = new ElasticsearchImportExport(); tool.bulkify(recordsFile, outputFile); String actual = TestUtils.read(outputFile.toFile()); assertThat(actual, equalTo(expected)); }