Guest User

Untitled

a guest
Nov 15th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.OutputStream;
  13. import java.text.ParseException;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Date;
  16. import org.json.JSONArray;
  17. import org.json.JSONObject;
  18.  
  19. /**
  20. *
  21. * @author riza
  22. */
  23. public class ElasticJsonArrayConverter {
  24.  
  25. public static String loadJsonFile(String location) throws IOException{
  26. InputStream is = new FileInputStream(location);
  27. StringBuilder builder = new StringBuilder();
  28. int data = is.read();
  29. while (data != -1) {
  30. builder.append((char) data);
  31. data = is.read();
  32. }
  33. is.close();
  34. return builder.toString();
  35. }
  36.  
  37. private static void writeJsontoFile(String path, String json) throws FileNotFoundException, IOException{
  38. OutputStream os = new FileOutputStream(path);
  39. for (int i = 0; i < json.length(); i++) {
  40. os.write((byte) json.charAt(i));
  41. }
  42. os.close();
  43. }
  44.  
  45. public static String formatDate(String date) {
  46.  
  47. if(date==null||date.isEmpty()){
  48. return "";
  49. }
  50.  
  51. String formattedDate = "";
  52.  
  53. SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  54. Date d = null;
  55.  
  56. try {
  57. d = sdf.parse(date);
  58. } catch (ParseException e) {
  59. e.printStackTrace();
  60. }
  61.  
  62. sdf.applyPattern("yyyy-MM-dd");
  63. formattedDate = sdf.format(d);
  64.  
  65. return formattedDate;
  66. }
  67.  
  68. /**
  69. * @param args the command line arguments
  70. * {
  71. "date": {
  72. "type": "date"
  73. },
  74. "kerugian": {
  75. "type": "text"
  76. },
  77. "keterangan": {
  78. "type": "text"
  79. },
  80. "koordinat": {
  81. "type": "geo_point"
  82. },
  83. "korban": {
  84. "type": "text"
  85. },
  86. "lokasi": {
  87. "type": "text"
  88. },
  89. "no": {
  90. "type": "integer"
  91. },
  92. "tanggal": {
  93. "type": "date"
  94. }
  95. }
  96. *
  97. */
  98. public static void main(String[] args) throws IOException {
  99. // TODO code application logic here
  100. String loc_src = "/home/riza/Dev/CSV-to-ElasticSearch-master/csvjson.json";
  101. String loc_res = "/home/riza/Dev/CSV-to-ElasticSearch-master/elasticjson.json";
  102. JSONArray jSONArray = new JSONArray(loadJsonFile(loc_src));
  103. StringBuilder builder = new StringBuilder();
  104. JSONObject jsonSrc;
  105. for (int i = 0; i < jSONArray.length(); i++) {
  106. // source
  107. jsonSrc = jSONArray.getJSONObject(i);
  108.  
  109. // index
  110. JSONObject id = new JSONObject();
  111. id.accumulate("_id", i+1);
  112. JSONObject index = new JSONObject();
  113. index.accumulate("index", id);
  114.  
  115. // result
  116. JSONObject resultJson = new JSONObject();
  117. resultJson.accumulate("tanggal", formatDate(jsonSrc.getString("Tanggal")));
  118. resultJson.accumulate("jam", jsonSrc.getString("Jam"));
  119. resultJson.accumulate("kerugian", jsonSrc.getString("Kerugian"));
  120. resultJson.accumulate("keterangan", jsonSrc.getString("Keterangan"));
  121. resultJson.accumulate("koordinat", jsonSrc.getInt("Lintang")+","+jsonSrc.getInt("Bujur"));
  122. resultJson.accumulate("korban", jsonSrc.getString("Korban"));
  123. resultJson.accumulate("lokasi", jsonSrc.getString("Lokasi"));
  124. resultJson.accumulate("no", jsonSrc.getInt("No"));
  125.  
  126. builder.append(index.toString()+"\n");
  127. builder.append(resultJson.toString()+"\n");
  128. }
  129. System.out.println(builder);
  130. writeJsontoFile(loc_res, builder.toString());
  131.  
  132. }
  133.  
  134. }
Add Comment
Please, Sign In to add comment