title: Parse Json Object with an Array and Map to Multiple Pairs with Apache Spark in Java
body: <p>I've googled it all day long and couldn't find straight answer, so ended up posting a question here.</p>
  
  <p>I have a file containing line-delimited json objects:</p>
  
  <pre><code>{"device_id": "103b", "timestamp": 1436941050, "rooms": ["Office", "Foyer"]}
  {"device_id": "103b", "timestamp": 1435677490, "rooms": ["Office", "Lab"]}
  {"device_id": "103b", "timestamp": 1436673850, "rooms": ["Office", "Foyer"]}
  </code></pre>
  
  <p>My goal is to parse this file with Apache Spark in Java. I referenced <a href="https://stackoverflow.com/questions/25362942/how-to-parsing-csv-or-json-file-with-apache-spark">How to Parsing CSV or JSON File with Apache Spark</a> and so far I could successfully parse each line of json to JavaRDD using <a href="https://github.com/google/gson" rel="nofollow noreferrer">Gson</a>.</p>
  
  <pre><code>JavaSparkContext sc = new JavaSparkContext(conf);
  JavaRDD&lt;String&gt; data = sc.textFile("fileName");
  JavaRDD&lt;JsonObject&gt; records = data.map(new Function&lt;String, JsonObject&gt;() {
      public JsonObject call(String line) throws Exception {
          Gson gson = new Gson();
          JsonObject json = gson.fromJson(line, JsonObject.class);
          return json;
      }
  });
  </code></pre>
  
  <p>Where I'm really stuck is I want to deserialize the "rooms" array so that it can fit to my class <em>Event</em>.</p>
  
  <pre><code>public class Event implements Serializable {
      public static final long serialVersionUID = 42L;
      private String deviceId;
      private int timestamp;
      private String room;
      // constructor , getters and setters 
  }
  </code></pre>
  
  <p>In other words, from this line:</p>
  
  <pre><code>{"device_id": "103b", "timestamp": 1436941050, "rooms": ["Office", "Foyer"]}
  </code></pre>
  
  <p>I want to create two Event objects in Spark:</p>
  
  <pre><code>obj1: deviceId = "103b", timestamp = 1436941050, room = "Office"
  obj2: deviceId = "103b", timestamp = 1436941050, room = "Foyer"
  </code></pre>
  
  <p>I did my little search and tried <strong>flatMapVlue</strong>, but no luck... It threw me an error...</p>
  
  <pre><code>JavaRDD&lt;Event&gt; events = records.flatMapValue(new Function&lt;JsonObject, Iterable&lt;Event&gt;&gt;() {
      public Iterable&lt;Event&gt; call(JsonObject json) throws Exception {
          JsonArray rooms = json.get("rooms").getAsJsonArray();
          List&lt;Event&gt; data = new LinkedList&lt;Event&gt;();
          for (JsonElement room : rooms) {
              data.add(new Event(json.get("device_id").getAsString(), json.get("timestamp").getAsInt(), room.toString()));
          }
          return data;
      }
  });
  </code></pre>
  
  <p>I'm very new to Spark and Map/Reduce. I would be grateful if you can help me out. Thanks in advance!</p>