Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 从卡夫卡读入spark的数据在注册为表格后消失?_Scala_Apache Spark_Apache Kafka - Fatal编程技术网

Scala 从卡夫卡读入spark的数据在注册为表格后消失?

Scala 从卡夫卡读入spark的数据在注册为表格后消失?,scala,apache-spark,apache-kafka,Scala,Apache Spark,Apache Kafka,考虑从dataframe写入kafka的数据,然后从kafka读回新的dataframe: // Write from df to kafka val wdf = airj.write .format("kafka") .option("kafka.bootstrap.servers", "localhost:9092") .option("topic", "air2008") .save 现在读回数据 // Read from kafka into spark df imp

考虑从
dataframe
写入
kafka
的数据,然后从
kafka
读回新的
dataframe

// Write from df to kafka
val wdf  = airj.write
  .format("kafka")
  .option("kafka.bootstrap.servers", "localhost:9092")
  .option("topic", "air2008")
  .save
现在读回数据

// Read from kafka into spark df
import org.apache.spark.sql.functions._
val flights = (spark.read
  .format("kafka")
  .option("kafka.bootstrap.servers", "localhost:9092")
  .option("subscribe", "air2008")
  .load())
有多少张唱片

scala> flights.count
res36: Long = 5824436
让我们将其注册为表:

flights.createOrReplaceTempView("flights_raw")
让我们换一种方式来问:有多少记录

让我们以第一种方式再次提问:

scala> flights.count
res40: Long = 0

这里发生了什么事?

根据@giorgosmyriantous的评论,我在里面放了一个
\u缓存。只有在
createOrReplaceTempView
之前完成,它才有帮助:如下所示

不起作用:

作品:

现在它起作用了

scala> flights.count
res47: Long = 5824436

createOrReplaceTempView
被延迟计算,这意味着它不会持久化到内存中。为此,您必须
缓存
数据

flights.cache
flights.createOrReplaceTempView("flights_raw")


我们应该做到这一点

createOrReplaceTempView
返回
单元
。。(那个建议没有用)我的错!创建视图
flights.createOrReplaceTempView(“flights_raw”)
后,尝试
spark.table(“flights_raw”).cache
然后
spark.table(“flights_raw”).count
我沿着这些行创建了一个答案。请随意复制、粘贴并将其置于您的名下,我就可以奖励您了。简单地说问题的原因并不是createOrReplaceTempView被懒散地评估。这不应该导致“现在你看到了,现在你不知道”的行为。那里有一只虫子。。但是,由于您的评论使我再次尝试缓存,因此我将奖励您(我尝试了“坚持”,但由于某些原因,这并没有解决问题)
import org.apache.spark.sql.functions._
val flights = spark.readStream
  .format("kafka")
  .option("kafka.bootstrap.servers", "localhost:9092")
  .option("subscribe", "air2008")
  .load()
flights.cache
flights.createOrReplaceTempView("flights_raw")
scala> flights.count
res47: Long = 5824436
flights.cache
flights.createOrReplaceTempView("flights_raw")
flights.createOrReplaceTempView("flights_raw")
spark.table("flights_raw")
spark.table("flights_raw").cache
spark.table("flights_raw").count