2017-06-26 83 views
0

我試圖將值插入Hive表中,並且如果每列都有值,則沒有問題,但我需要在其中一列中插入NULL值。 我做這種事兒:將NULL值插入Hive with Spark中的dataFrame

val errorsToAlert = List(("source1", "table1","27-01-2002", null)) 
val data = sqlContext.createDataFrame(errorsToAlert).toDF("source", 
"table_name", "open_date", "close_date") 
data.write.mode("append").saveAsTable("management.alerts") 

我試過用NULL和無,但都代表這個錯誤:

17/06/26 11:59:38 ERROR yarn.ApplicationMaster: User class threw exception:
scala.MatchError: scala.None.type (of class scala.reflect.internal.Types$UniqueSingleType)
scala.MatchError: scala.None.type (of class scala.reflect.internal.Types$UniqueSingleType)

+0

那是因爲你只有一排數據與close_date爲null,其處理作爲None類型而不是StringType。您必須顯式定義模式,或者在close_date列中添加另一個非空值的行。 –

回答

2

的問題是完全無關的蜂巢。如果您檢查errorsToAlert類型,你會看到它是:

List[(String, String, String, Null)] 

scala.Null不是Dataset可接受的輸入。

如果需要的類型本身是可空的,你可以明確指定:

sqlContext.createDataFrame(Seq(
    ("source1", "table1","27-01-2002", null: String) 
)) 

以其它方式使用scala.Option

sqlContext.createDataFrame(Seq(
    ("source1", "table1","27-01-2002", None: Option[Int]) 
)) 
+0

我不認爲「close_date」的類型是'Int'(你的最後一行) –

+0

@RaphaelRoth公平點,但是'Strings'和支持的'java.sql'類型都被第一個case覆蓋,所以這主要是爲了完整性。 – zero323