我正在尝试将RDD表单couchbase转换为DataFrame(scala 2.11 - 和spark 2.1)但是得到一个重载错误,我的代码在下面,任何想法?另一个线程没有完全回答这个问题。
我在Databricks笔记本中这样做,我使用沙发连接器来获得纯DataFrames,但如果我想做一个客户N1QL查询,更多定制,这是我能想到的最好的,首先使用RDD?
首先,有一种更好的方法在本机Dataframe中执行此查询吗?我想我需要使用n1qL和RDD,或者我在这里遗漏了什么?
请让我知道下面的RDD转换代码我做错了什么,我也得到:84:错误:重载方法值createDataFrame与替代品:错误....谢谢!
val reconciliationSchema =
new StructType()
.add("numEvents", IntegerType)
.add("eventCategory", StringType)
.add("eventName", StringType)
val orderEventsCouchbaseQuery = """
SELECT
count(*) as numEvents, event.eventCategory, event.eventName
FROM
events
WHERE
STR_TO_UTC(event.eventOccurredTime)
BETWEEN STR_TO_UTC("2017-06-16") AND STR_TO_UTC("2017-06-26")
GROUP BY event.eventCategory, event.eventName
order by event.eventCategory, event.eventName
"""
val queryResultRDD = sc.couchbaseQuery(N1qlQuery.simple(orderEventsCouchbaseQuery),"events").map(_.value)
val queryResultDF: DataFrame = spark.createDataFrame(queryResultRDD,reconciliationSchema)
display(queryResultDF)
答案 0 :(得分:1)
我认为你遇到的问题不是与沙发基础相关的问题,而是火花/ scala类型推断问题。当您使用createDataFrame
时,在这种情况下,spark需要使用Row
,而不是使用该rdd的couchbase查询的返回类型。
所以这里有一些类似的示例代码,您可以看到,当它变成一行时,它可以正常工作:
val query = N1qlQuery.simple("" +
"select country, count(*) as count " +
"from `travel-sample` " +
"where type = 'airport' " +
"group by country " +
"order by count desc")
val schema = StructType(
StructField("count", IntegerType) ::
StructField("country", StringType) :: Nil
)
val rdd = spark.sparkContext.couchbaseQuery(query).map(r => Row(r.value.getInt("count"), r.value.getString("country")))
spark.createDataFrame(rdd, schema).show()