2017-06-05 32 views
0

來了我有一個amount值,我們正在從文件中讀取並嘗試在控制檯上打印。火花SQL長雙值與指數

當我們嘗試打印時,它顯示爲Exponential,另一個問題是當我們試圖將其轉換爲null

輸入值:

{"amt":987654321111.0} 

示例代碼:

df.show(); 
df.registerTempTable("input"); 
DataFrame outputDF = sqlContext.sql("select cast(amt as decimal) from input"); 
outputDF.show(); 

輸出:

+----------------+ 
|    amt| 
+----------------+ 
|9.87654321111E11| 
+----------------+ 

root 
|-- amt: double (nullable = true) 

root 
|-- amt: decimal(10,0) (nullable = true) 

+----+ 
| amt| 
+----+ 
|null| 
+----+ 
+0

@ zero323:你可以看看這個嗎? – Shankar

回答

0

解決使用StructType createDecimalType方法的問題。

StructType schema = new StructType().add("amt", DataTypes.createDecimalType(38,10)); 

DataFrame df = sqlContext.read().schema(schema).json("test.json");