使用scala case类创建json对象

du7egjpx  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(536)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

5个月前关门了。
改进这个问题
我有如下所示的json文件,我的scala程序必须将这个json文件作为 args(0) 以及 args(1) 我会把你的车作为论据。基于 args(1) 参数传递,程序必须获取相应的stagindir路径, rawLayerTbl(table name) ,filedelimiter作为三个独立变量。
创建了一个case类,并尝试创建与case类匹配的json对象,但是运气不好,有没有一种简单的方法可以使用scala实现这一点。我需要在databricks运行时版本6.3(包括apachespark 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:
      - "|"
am46iovg

am46iovg1#

检查以下代码。

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":["|"]}}}

输出

scala> val (staginDir, rawLayerTbl, fileDelimiter) = getConfig("CUSTOMER_VEHICLE")(yaml)
staginDir: String = ADLS BLOB LOCATION
rawLayerTbl: String = TABLE_NAME
fileDelimiter: String = |

相关问题