我遇到的情况是我必须根据涉及外部数据引用的某些条件来过滤流中的数据点。我已经在Dataframe中加载了外部数据(以便我可以使用SQL接口对其进行查询)。但是当我尝试查询Dataframe时,我发现我们无法在transform(filter)函数中访问它。 (以下示例代码)
// DStream is created and temp table called 'locations' is registered
dStream.filter(dp => {
val responseDf = sqlContext.sql("select location from locations where id='001'")
responseDf.show() //nothing is displayed
// some condition evaluation using responseDf
true
})
我做错了吗?如果是,那么在流转换阶段加载外部数据并在流转换阶段查询它会是一种更好的方法。
答案 0 :(得分:0)
使用SparkSession而不是SQLContext解决了这个问题。代码如下,
val sparkSession = SparkSession.builder().appName("APP").getOrCreate()
val df = sparkSession.createDataFrame(locationRepo.getLocationInfo, classOf[LocationVO])
df.createOrReplaceTempView("locations")
val dStream: DStream[StreamDataPoint] = getdStream()
dStream.filter(dp => {
val sparkAppSession = SparkSession.builder().appName("APP").getOrCreate()
val responseDf = sparkAppSession.sql("select location from locations where id='001'")
responseDf.show() // this prints the results
// some condition evaluation using responseDf
true
})