Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.json.JSONArray;
- import org.json.JSONObject;
- /**
- *
- * @author riza
- */
- public class ElasticJsonArrayConverter {
- public static String loadJsonFile(String location) throws IOException{
- InputStream is = new FileInputStream(location);
- StringBuilder builder = new StringBuilder();
- int data = is.read();
- while (data != -1) {
- builder.append((char) data);
- data = is.read();
- }
- is.close();
- return builder.toString();
- }
- private static void writeJsontoFile(String path, String json) throws FileNotFoundException, IOException{
- OutputStream os = new FileOutputStream(path);
- for (int i = 0; i < json.length(); i++) {
- os.write((byte) json.charAt(i));
- }
- os.close();
- }
- public static String formatDate(String date) {
- if(date==null||date.isEmpty()){
- return "";
- }
- String formattedDate = "";
- SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
- Date d = null;
- try {
- d = sdf.parse(date);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- sdf.applyPattern("yyyy-MM-dd");
- formattedDate = sdf.format(d);
- return formattedDate;
- }
- /**
- * @param args the command line arguments
- * {
- "date": {
- "type": "date"
- },
- "kerugian": {
- "type": "text"
- },
- "keterangan": {
- "type": "text"
- },
- "koordinat": {
- "type": "geo_point"
- },
- "korban": {
- "type": "text"
- },
- "lokasi": {
- "type": "text"
- },
- "no": {
- "type": "integer"
- },
- "tanggal": {
- "type": "date"
- }
- }
- *
- */
- public static void main(String[] args) throws IOException {
- // TODO code application logic here
- String loc_src = "/home/riza/Dev/CSV-to-ElasticSearch-master/csvjson.json";
- String loc_res = "/home/riza/Dev/CSV-to-ElasticSearch-master/elasticjson.json";
- JSONArray jSONArray = new JSONArray(loadJsonFile(loc_src));
- StringBuilder builder = new StringBuilder();
- JSONObject jsonSrc;
- for (int i = 0; i < jSONArray.length(); i++) {
- // source
- jsonSrc = jSONArray.getJSONObject(i);
- // index
- JSONObject id = new JSONObject();
- id.accumulate("_id", i+1);
- JSONObject index = new JSONObject();
- index.accumulate("index", id);
- // result
- JSONObject resultJson = new JSONObject();
- resultJson.accumulate("tanggal", formatDate(jsonSrc.getString("Tanggal")));
- resultJson.accumulate("jam", jsonSrc.getString("Jam"));
- resultJson.accumulate("kerugian", jsonSrc.getString("Kerugian"));
- resultJson.accumulate("keterangan", jsonSrc.getString("Keterangan"));
- resultJson.accumulate("koordinat", jsonSrc.getInt("Lintang")+","+jsonSrc.getInt("Bujur"));
- resultJson.accumulate("korban", jsonSrc.getString("Korban"));
- resultJson.accumulate("lokasi", jsonSrc.getString("Lokasi"));
- resultJson.accumulate("no", jsonSrc.getInt("No"));
- builder.append(index.toString()+"\n");
- builder.append(resultJson.toString()+"\n");
- }
- System.out.println(builder);
- writeJsontoFile(loc_res, builder.toString());
- }
- }
Add Comment
Please, Sign In to add comment