-3

I've the JSON file as shown below, my scala program has to read this JSON file as an args(0) and for args(1) i will be passing CUSTOMER_VEHICLE as an argument. Based on the args(1) argument passed, program has to fetch the corresponding staginDir path, rawLayerTbl(table name), fileDelimiter as a three separate variables.

Created a case class and tried creating JSON object matching the case class but no luck, is there a simple way to achieve this using scala. I will need to execute this on the Databricks runtime version 6.3(includes Apache Spark 2.4.4, Scala 2.11).

    rawSource: 
   DEALER_MASTER :
    staginDir :
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter :
      - "|"
   CUSTOMER_VEHICLE :
    staginDir :
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter :
      - "|" 
   EMPLOYEE_MASTER :
    staginDir :
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter :
      - "|"
   CUSTOMERCREDIT_MASTER :
    staginDir :
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter :
      - "|"
   CONTINUOUS_CAN_MASTER :
    staginDir :
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter :
      - "|" 
   LOGIN_TABLE:
    staginDir:
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter:
      - "|"  
SCouto
  • 7,808
  • 5
  • 32
  • 49
chaitra k
  • 371
  • 1
  • 4
  • 18

1 Answers1

1

Check below code.

scala> "cat /tmp/sample.yaml".!
 rawSource:
   DEALER_MASTER :
    staginDir :
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter :
      - "|"
   CUSTOMER_VEHICLE :
    staginDir :
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter :
      - "|"
   EMPLOYEE_MASTER :
    staginDir :
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter :
      - "|"
   CUSTOMERCREDIT_MASTER :
    staginDir :
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter :
      - "|"
   CONTINUOUS_CAN_MASTER :
    staginDir :
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter :
      - "|"
   LOGIN_TABLE:
    staginDir:
      - "ADLS BLOB LOCATION"
    rawLayerTbl:
      - "TABLE_NAME"
    fileDelimiter:
      - "|"
scala> def loadYAML(path: String): String = {
     |     import com.fasterxml.jackson.databind.ObjectMapper
     |     import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
     |
     |     val yaml = scala.io.Source.fromFile(path).getLines().mkString(" \n ")
     |     val yamlReader = new ObjectMapper(new YAMLFactory)
     |     val obj = yamlReader.readValue(yaml, classOf[Any])
     |     val jsonWriter = new ObjectMapper
     |     jsonWriter.writeValueAsString(obj)
     | }
loadYAML: (path: String)String
scala> def getConfig(key: String)(yaml: String):(String,String,String) = {
     |   import org.json4s._
     |   import org.json4s.jackson.JsonMethods._
     |   implicit val formats = DefaultFormats
     |
     |   val json = parse(yaml)
     |   val staginDir = ( json \\ key \ "staginDir").extract[List[String]].head
     |   val rawLayerTbl = ( json \\ key \ "rawLayerTbl").extract[List[String]].head
     |   val fileDelimiter = ( json \\ key \ "fileDelimiter").extract[List[String]].head
     |   (staginDir, rawLayerTbl, fileDelimiter)
     | }
getConfig: (key: String)(yaml: String)(String, String, String)
scala> val yaml = loadYAML("/tmp/sample.yaml")
yaml: String = {"rawSource":{"DEALER_MASTER":{"staginDir":["ADLS BLOB LOCATION"],"rawLayerTbl":["TABLE_NAME"],"fileDelimiter":["|"]},"CUSTOMER_VEHICLE":{"staginDir":["ADLS BLOB LOCATION"],"rawLayerTbl":["TABLE_NAME"],"fileDelimiter":["|"]},"EMPLOYEE_MASTER":{"staginDir":["ADLS BLOB LOCATION"],"rawLayerTbl":["TABLE_NAME"],"fileDelimiter":["|"]},"CUSTOMERCREDIT_MASTER":{"staginDir":["ADLS BLOB LOCATION"],"rawLayerTbl":["TABLE_NAME"],"fileDelimiter":["|"]},"CONTINUOUS_CAN_MASTER":{"staginDir":["ADLS BLOB LOCATION"],"rawLayerTbl":["TABLE_NAME"],"fileDelimiter":["|"]},"LOGIN_TABLE":{"staginDir":["ADLS BLOB LOCATION"],"rawLayerTbl":["TABLE_NAME"],"fileDelimiter":["|"]}}}

Output

scala> val (staginDir, rawLayerTbl, fileDelimiter) = getConfig("CUSTOMER_VEHICLE")(yaml)
staginDir: String = ADLS BLOB LOCATION
rawLayerTbl: String = TABLE_NAME
fileDelimiter: String = |
Srinivas
  • 8,957
  • 2
  • 12
  • 26