日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java动态json入库_从JSon File动态生成模式

發(fā)布時間:2023/12/20 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java动态json入库_从JSon File动态生成模式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一些興趣點:

1)您不需要數(shù)據(jù)幀來加載您的json架構 . 模式在驅動程序上加載和執(zhí)行,因為不需要分發(fā)那些不必要的開銷

2)我構造了一個JColumn對象的List,并將它傳遞給StructType以動態(tài)構造模式

3)inferSchema應該是false,因為我們明確定義了schema

4)我假設您的數(shù)據(jù)庫表使用“null”表示空值

5)調整映射修改typeMapping

import org.json4s._

import org.json4s.native.JsonMethods

case class JColumn(trim: Boolean, name: String, nullable: Boolean, id: Option[String], position: BigInt, table: String, _type: String, primaryKey: Boolean)

val path = """your_path\schema.json"""

val input = scala.io.Source.fromFile(path)

val json = JsonMethods.parse(input.reader())

val typeMapping = Map(

"double" -> DoubleType,

"integer" -> IntegerType,

"string" -> StringType,

"date" -> DateType,

"bool" -> BooleanType)

var rddSchema = ListBuffer[StructField]()

implicit val formats = DefaultFormats

val schema = json.extract[Array[JColumn]]

//schema.foreach(c => println(s"name:${c.name} type:${c._type} isnullable:${c.nullable}"))

schema.foreach { c =>

rddSchema += StructField(c.name, typeMapping(c._type), c.nullable, Metadata.empty)

}

val in_emp = spark.read

.format("com.databricks.spark.csv")

.schema(StructType(rddSchema.toList))

.option("inferSchema", "false")

.option("dateFormat", "yyyy.MM.dd")

.option("header", "false")

.option("delimiter", ",")

.option("nullValue", "null")

.option("treatEmptyValuesAsNulls", "true")

.csv("""your_path\employee.csv""")

in_emp.printSchema()

in_emp.collect()

in_emp.show()

我使用以下模式進行測試:

[

{

"trim": true,

"name": "id",

"nullable": true,

"id": null,

"position": 0,

"table": "employee",

"_type": "integer",

"primaryKey": true

},

{

"trim": true,

"name": "salary",

"nullable": true,

"id": null,

"position": 1,

"table": "employee",

"_type": "double",

"primaryKey": false

},

{

"trim": true,

"name": "dob",

"nullable": true,

"id": null,

"position": 2,

"table": "employee",

"_type": "date",

"primaryKey": false

},

{

"trim": true,

"name": "department",

"nullable": true,

"id": null,

"position": 3,

"table": "employee",

"_type": "string",

"primaryKey": false

}

]

以及下一個數(shù)據(jù)(employee.csv):

1211,3500.0,null,marketing

1212,3000.0,2016.12.08,IT

1213,4000.0,2017.10.20,HR

1214,3000.0,2017.10.20,finance

總結

以上是生活随笔為你收集整理的java动态json入库_从JSon File动态生成模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。