在scala中创建具有键值对的数据帧



由于我是scala的新手,需要在scala中转换我用python 编写的以下代码

Dict={"test1":"date","test2":"string"}
for x in Dict.keys():
column= x
type = Dict[x]
print (column)
print (type)

输出

test1
date
test2
string

您可以使用

Map(密钥/值对的集合(

Scala中。参见Scala映射

val testMap  = Map("test1" -> "Date", "test2" -> "String" ) 

在密钥/值对上迭代

for ((key,value) <- testMap) println(s"key: $value, value: $valuen")

转换为DF

val df = testMap.toSeq.toDF("name", "dataType")
df.show()

最新更新