Apply filter condition on dataframe created from JSON

后端 未结 2 757
鱼传尺愫
鱼传尺愫 2021-01-23 14:02

I am working on the dataframe created by JSON and then I want to apply the filter condition over the dataframe.

val jsonStr = \"\"\"{ \"metadata\": [{ \"key\": 8         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-23 14:28

    First you should use explode to get an easy-to-work-with dataFrame. Then you can select both key and value of you given input:

    val explodedDF = df.withColumn("metadata", explode($"metadata"))
      .select("metadata.key", "metadata.value")
    

    Output:

    +-----+-----+
    |  key|value|
    +-----+-----+
    |84896|   54|
    | 1234|   12|
    +-----+-----+
    

    This way you'll be able to perform your filtering logic as usual:

    scala> explodedDF.where("key == 84896").show
    +-----+-----+
    |  key|value|
    +-----+-----+
    |84896|   54|
    +-----+-----+
    

    You can concatenate your filtering requirements, some examples below:

    explodedDF.where("key == 84896 AND value == 54")
    explodedDF.where("(key == 84896 AND value == 54) OR key = 1234")
    

提交回复
热议问题