2016-07-28 8 views
-1

デフォルトでは、データをロードするときに、すべてのカラムがストリング型と見なされます。スパークRDDのカラムのデータ型を変更し、それに照会します

temp.printSchema 
|-- firstName: string (nullable = true) 
|-- lastName: string (nullable = true) 
|-- age: string (nullable = true) 
|-- doj: date (nullable = true) 

は一時テーブルを登録し、それ

temp.registerTempTable("temptable"); 
val temp1 = sqlContext.sql("select * from temptable") 
temp1.show() 
+---------+--------+---+----------+ 
|firstName|lastName|age|  doj| 
+---------+--------+---+----------+ 
| dileep|  gog| 21|2016-01-01| 
| avishek| ganguly| 21|2016-01-02| 
| shreyas|  t| 20|2016-01-03| 
+---------+--------+---+----------+ 
val temp2 = sqlContext.sql("select * from temptable where doj > cast('2016-01-02' as date)") 

に照会しかし、私がしようとしているときのように見えるRDDのスキーマを更新した後

firstName,lastName,age,doj 
dileep,gog,21,2016-01-01 
avishek,ganguly,21,2016-01-02 
shreyas,t,20,2016-01-03 

:データは次のようになりますそれが私に与えている結果を見てください:

temp2: org.apache.spark.sql.DataFrame = [firstName: string, lastName: string, age: string, doj: date] 

私は

temp2.show() 
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer 
+0

「スキーマを更新する」正確な方法は? –

+0

var x = tempSchema.toArray; val y = StructField( "dob"、DateType、true); x.update(3、y) \t tempSchema = StructType(x); val temp = sqlContext.applySchema(tempSchemaRDD、tempSchema) ; – Dileep

答えて

0

を行うときに、私はあなたのコードを試してみました、それは私のために動作します。私は問題があなたが最初にスキーマをどのように変更するのか疑問に思っています(コメントに投稿すると読みにくいです - コードで質問を更新する必要があります)。予想通り

import org.apache.spark.sql.functions._ 

val temp = df.withColumn("doj", to_date('doj)) 
temp.registerTempTable("temptable"); 
val temp2 = sqlContext.sql("select * from temptable where doj > cast('2016-01-02' as date)") 

temp2.show()を行う明らかにする:次に

val df = sc.parallelize(List(("dileep","gog","21","2016-01-01"), ("avishek","ganguly","21","2016-01-02"), ("shreyas","t","20","2016-01-03"))).toDF("firstName", "lastName", "age", "doj") 

まず、あなたの入力をシミュレート:

とにかく、私はそれをこのように行っている

+---------+--------+---+----------+ 
|firstName|lastName|age|  doj| 
+---------+--------+---+----------+ 
| shreyas|  t| 20|2016-01-03| 
+---------+--------+---+----------+ 
+0

提案のおかげで、完全なコードと説明で私の質問が更新されます – Dileep

関連する問題