2015-06-30 18 views
5

Sto utilizzando le librerie di scintille in Scala. Ho creato un dataframe utilizzandoScala DataFrame: esplode un array

val searchArr = Array(
    StructField("log",IntegerType,true), 
    StructField("user", StructType(Array(
    StructField("date",StringType,true), 
    StructField("ua",StringType,true), 
    StructField("ui",LongType,true))),true), 
    StructField("what",StructType(Array(
    StructField("q1",ArrayType(IntegerType, true),true), 
    StructField("q2",ArrayType(IntegerType, true),true), 
    StructField("sid",StringType,true), 
    StructField("url",StringType,true))),true), 
    StructField("where",StructType(Array(
    StructField("o1",IntegerType,true), 
    StructField("o2",IntegerType,true))),true) 
) 

val searchSt = new StructType(searchArr)  

val searchData = sqlContext.jsonFile(searchPath, searchSt) 

io sono ormai cosa esplodere il what.q1 campo, che dovrebbe contenere un array di interi, ma la documentazione è limitata: http://spark.apache.org/docs/1.4.0/api/java/org/apache/spark/sql/DataFrame.html#explode(java.lang.String,%20java.lang.String,%20scala.Function1,%20scala.reflect.api.TypeTags.TypeTag)

Finora ho provato un poche cose senza troppa fortuna

val searchSplit = searchData.explode("q1", "rb")(q1 => q1.getList[Int](0).toArray()) 

Eventuali idee/esempi su come utilizzare esplodere su un array?

risposta

0

Hai provato con una UDF sul campo "cosa"? Qualcosa di simile potrebbe essere utile:

val explode = udf { 
(aStr: GenericRowWithSchema) => 
    aStr match { 
     case null => "" 
     case _ => aStr.getList(0).get(0).toString() 
    } 
} 


val newDF = df.withColumn("newColumn", explode(col("what"))) 

dove:

  • getList (0) restituisce campo "Q1"
  • get (0) restituisce il primo elemento del "Q1"

Non sono sicuro ma si potrebbe provare a utilizzare getAs [T] (fieldName: String) anziché getList (indice: Int).