2017-08-12 60 views
0

一個JSON我有一個服務器上運行的SQL星火應用。它從.parquet文件獲取數據,並在每個請求中對這些數據執行SQL查詢。我需要在響應中發送對應於查詢輸出的JSON。獲取從數據集在星火SQL(JAVA)

這是我做的

Dataset<Row> sqlDF = spark.sql(query); 
sqlDF.show(); 

所以我知道的查詢工作。

我試過返回sqlDF.toJSON().collect(),但在另一端我只收到[Ljava.lang.String;@1cd86ff9

我試着寫sqlDF作爲一個JSON文件,但後來不知怎麼的內容添加到響應,並將其保存什麼都沒有做的一個JSON文件的文件結構。

任何想法/建議?

回答

0
You can return JSON String using the below code. 
List<String> stringDataset = sqlDF.toJSON().collectAsList(); 
return stringDataset; 

Jackson will return the JSON string in this case. 

If you want to return proper JSONObject the you can use the below code : 

List<Map<String,Object>> result= new ArrayList<>(); 
List<String> stringDataset = sqlDF.toJSON().collectAsList(); 
for(String s : stringDataset){ 
    Map<String,Object> map = new HashMap<>(); 
    map = mapper.readValue(s, new TypeReference<Map<String, String>>(){}); 
    result.add(map); 
      }