scala - Cast a map column of dataframe to struct column -


i have dataframe, , 1 of column's type map. map comes udf , existing columns of dataframe.
question is there way transform column struct type?
way, i'm using scala 2.10, , column of map has more 50 fields. don't want use case class.
ahead.

since there not enough information how data looks , kind of output want, i'm writing answer based on understanding of question.

val df = sqlcontext.sql(""" select map(1,"a" ,2,"b" ,3,"c" ,4,"d" ,5,"e" ,6,"f" , 7,"g" ,8,"h" ,9,"i" ,10,"j" ,11,"k" ,12,"l" ,13,"m" , 14,"n" ,15,"o" ,16,"p" ,17,"q" ,18,"r" ,19,"s" ,20,"t" , 21,"u" ,22,"v" ,23,"w" ,24,"x" ,25,"y" ,26,"z" )as mapcol  """) 

you can write udf convert map seq type read struct in dataframe. each element of struct key-value pair separated comma (or whatever want).

val tostruct = udf( (c1: map[int, string]) => c1.map {     case (k,v) => k+","+v }.toseq) 

now, calling udf on dataframe.

val structdf = df.withcolumn("structcol", tostruct('mapcol) ) structdf.show +--------------------+--------------------+ |              mapcol|           structcol| +--------------------+--------------------+ |map(5 -> e, 10 ->...|[5,e, 10,j, 24,x,...| +--------------------+--------------------+ 

to fetch single element:

structdf.selectexpr("structcol[0]").show  +------------+ |structcol[0]| +------------+ |         5,e| +------------+ 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -