简体   繁体   中英

create data frame with key value pair in scala

As I am new to scala need to convert following code in scala which i written in python

Dict={"test1":"date","test2":"string"}

for x in Dict.keys():
    column= x
    type = Dict[x]
    print (column)
    print (type)

output

test1
date
test2
string

you can use

Map(collection of key/value pairs)

in Scala . See Scala Maps

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

Iterate over Key/Value pairs

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

Conversion to DF

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM