AnsweredAssumed Answered

hi, I have a json file. I want to append current date time to that. How do i do that in Java ?

Question asked by shivaraj160 on Mar 21, 2018
Latest reply on Apr 1, 2018 by shivaraj160

Sample JSON :

{"device_uuid":"ec981a8ee78cf608f4b71f3a33cfa336e1980425","os_ver":"7.0","install_uuid":"6e5ba0c6d82d7359d09b143c8c9191f417887edc","customer_ids":{"customer_id":"31990574881"}}

 

Required Output: add current timestamp

{"device_uuid":"ec981a8ee78cf608f4b71f3a33cfa336e1980425","os_ver":"7.0","install_uuid":"6e5ba0c6d82d7359d09b143c8c9191f417887edc","customer_ids":{"customer_id":"31990574881"},"load time" :"2018-03-24 00:00:00"}

 

Current Java Code working without load time in mapr sand box:

import com.mapr.db.spark.MapRDBSpark;
import com.mapr.db.spark.api.java.MapRDBJavaContext;
import com.mapr.db.spark.impl.OJAIDocument;
import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.io.File;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.text.SimpleDateFormat;

public class SparkOjaiApp {

public static void main(String[] args) {



    System.out.println( "Hello World!" );
    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    System.out.println(timestamp);
    Timestamp s1=timestamp;
    System.out.println(s1);
    final SparkConf session = new SparkConf().setAppName("json app").setMaster("local[*]");
    SparkContext sc = new SparkContext(session);
    JavaSparkContext jsc = new JavaSparkContext(sc);
    SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS");
    System.out.println( "START ##########" +format.format(System.currentTimeMillis()));


    JavaRDD<String> rd = jsc.textFile("testJson.json");
    System.out.println("Count :     "+rd.count());
    JavaRDD<OJAIDocument> rdd= rd.map(new Function<String, OJAIDocument>() {


        public OJAIDocument call(String s) throws Exception {
   
            System.out.println(s);
          
            return MapRDBSpark.newDocument(s) ;

        }

    });

    MapRDBJavaContext.maprDBSparkContext(rdd).saveToMapRDB("/tmp/testData3",true, false, "uuid");
    System.out.println( "END ##########" +format.format(System.currentTimeMillis()));
}

}

Outcomes