0

I am trying to create a function to parse my records and I am getting two different behaviors when I call a function vs hard coding it:

I am using:

import org.json4s.JsonAST.{JString, JField, JObject, JArray}
import org.json4s.jackson.JsonMethods._

val parsed = parse("""{"timestamp":"2016-06-02 13:40:16,772","tableName":"stg_mde_campaign_master","dbName":"stg_bankrtl_mde","owner":"hive","location":"null"}""")
      val output = for {
        JObject(child) <- parsed
        JField("timestamp", JString(subject1)) <- child
        JField("tableName", JString(obj1)) <- child 
        } yield (subject1,obj1)

Will output (what I want):

output: List[(String, String)] = List((2016-06-02 13:40:16,772,stg_mde_campaign_master)

but when I transfer it to a function I am getting:

    def getSubOb(record: String, subject:String, obj:String): List[(String, String)] = {
      val parsed = parse(record)
      val output: List[(String, String)] = for {
        JObject(child) <- parsed
        JField(subject, JString(subject1)) <- child
        JField(obj, JString(obj1)) <- child
    } yield (subject1, obj1)
      output
  }
val something = getSubOb("""{"timestamp":"2016-06-02 13:40:16,772","tableName":"stg_mde_campaign_master","dbName":"stg_bankrtl_mde","owner":"hive","location":"null"}""", "timestamp", "tableName")

The output acts very weird:

something: List[(String, String)] = List((2016-06-02 13:40:16,772,2016-06-02 13:40:16,772), (2016-06-02 13:40:16,772,stg_mde_campaign_master), (2016-06-02 13:40:16,772,stg_bankrtl_mde), (2016-06-02 13:40:16,772,hive), (2016-06-02 13:40:16,772,null), (stg_mde_campaign_master,2016-06-02 13:40:16,772), (stg_mde_campaign_master,stg_mde_campaign_master), (stg_mde_campaign_master,stg_bankrtl_mde), (stg_mde_campaign_master,hive), (stg_mde_campaign_master,null), (stg_bankrtl_mde,2016-06-02 13:40:16,772), (stg_bankrtl_mde,stg_mde_campaign_master), (stg_bankrtl_mde,stg_bankrtl_mde), (stg_bankrtl_mde,hive), (stg_bankrtl_mde,null), (hive,2016-06-02 13:40:16,772), (hive,stg_mde_campaign_master), (hive,stg_bankrtl_mde), (hive,hive), (hive,null), (null,2016-06-02 13:40:16,772), (null,stg_mde_campaign_...
theMadKing
  • 2,064
  • 7
  • 32
  • 59

1 Answers1

1

You have a subtele error on the unapply.

Lowercased terms on the left side of a pattern match are treated as variables. So everything matches and is bound there.

You can use backticks like in `variable name` to tell Scala that it is not a variable to be bound but a value to be matched against on the left side of the pattern matched.

see: lowercased variables in pattern matching

This should work as intended:

def getSubOb(record: String, subject:String, obj:String): List[(String, String)] = for {
    JObject(child) <-  parse(record)
    JField(`subject`, JString(subject1)) <- child
    JField(`obj`, JString(obj1)) <- child
} yield (subject1, obj1)
Community
  • 1
  • 1
Andreas Neumann
  • 10,734
  • 1
  • 32
  • 52