Github user zsxwing commented on a diff in the pull request:

    https://github.com/apache/spark/pull/15504#discussion_r84187287
  
    --- Diff: 
external/kafka-0-10-sql/src/main/scala/org/apache/spark/sql/kafka010/JsonUtils.scala
 ---
    @@ -0,0 +1,115 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.kafka010
    +
    +import java.io.Writer
    +
    +import scala.collection.mutable.{ ArrayBuffer, HashMap }
    +import scala.util.control.NonFatal
    +
    +import com.fasterxml.jackson.databind.ObjectMapper
    +import com.fasterxml.jackson.databind.node._
    +import org.apache.kafka.common.TopicPartition
    +
    +/**
    + * Utilities for converting Kafka related objects to and from json.
    + */
    +private object JsonUtils {
    --- End diff --
    
    We can just define a case class and then use `json4s` to convert a json 
string to a Scala instance. E.g.,
    
    ```Scala
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
        import org.json4s.NoTypeHints
        import org.json4s.jackson.Serialization
        import org.json4s.jackson.Serialization.read
    
        implicit val formats = Serialization.formats(NoTypeHints)
    
        val assign = """{"topicfoo": [0, 1],"topicbar": [0, 1]}"""
        val offsets = """{"topicFoo": {"0": 1234, "1": -2}, "topicBar":{"0": 
-1}}"""
        println(read[Map[String, Seq[Int]]](assign))
        println(read[Map[String, Map[Int, Long]]](offsets))
    
    // Exiting paste mode, now interpreting.
    
    Map(topicfoo -> List(0, 1), topicbar -> List(0, 1))
    Map(topicFoo -> Map(0 -> 1234, 1 -> -2), topicBar -> Map(0 -> -1))
    import org.json4s.NoTypeHints
    import org.json4s.jackson.Serialization
    import org.json4s.jackson.Serialization.read
    formats: org.json4s.Formats{val dateFormat: org.json4s.DateFormat; val 
typeHints: org.json4s.TypeHints} = org.json4s.Serialization$$anon$1@7d4d65f5
    assign: String = {"topicfoo": [0, 1],"topicbar": [0, 1]}
    offsets: String = {"topicFoo": {"0": 1234, "1": -2}, "topicBar":{"0": -1}}
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to