Twitter流媒体错误

时间:2015-07-29 07:00:15

标签: twitter apache-spark streaming twitter4j spark-streaming

我正在使用spark streaming编写一个twitter连接器。
我正面临着这样的例外

  

ERROR ReceiverTracker:流0的注销接收器:重新启动   延迟2000ms的接收器:启动Twitter流时出错 -   显示java.lang.NullPointerException        在org.apache.spark.streaming.twitter.TwitterReceiver.onStart(TwitterInputDStream.scala:89)        在org.apache.spark.streaming.receiver.ReceiverSupervisor.startReceiver(ReceiverSupervisor.scala:121)        在org.apache.spark.streaming.receiver.ReceiverSupervisor $$ anonfun $ restartReceiver $ 1.apply $ mcV $ sp(ReceiverSupervisor.scala:159)        在org.apache.spark.streaming.receiver.ReceiverSupervisor $$ anonfun $ restartReceiver $ 1.apply(ReceiverSupervisor.scala:152)        在org.apache.spark.streaming.receiver.ReceiverSupervisor $$ anonfun $ restartReceiver $ 1.apply(ReceiverSupervisor.scala:152)        在scala.concurrent.impl.Future $ PromiseCompletingRunnable.liftedTree1 $ 1(Future.scala:24)        在scala.concurrent.impl.Future $ PromiseCompletingRunnable.run(Future.scala:24)        at scala.concurrent.impl.ExecutionContextImpl $$ anon $ 3.exec(ExecutionContextImpl.scala:107)        在scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)        在scala.concurrent.forkjoin.ForkJoinPool $ WorkQueue.runTask(ForkJoinPool.java:1339)        在scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)        在scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

以下是相关代码段。

val config = new twitter4j.conf.ConfigurationBuilder()
    .setOAuthConsumerKey("*********************")
               .setOAuthConsumerSecret("**********************************************")
    .setOAuthAccessToken("****************************************************")
    .setOAuthAccessTokenSecret("**********************************************************")
    .build

val twitter_auth = new TwitterFactory(config)
val a = new twitter4j.auth.OAuthAuthorization(config)
val atwitter : Option[twitter4j.auth.Authorization] =  Some(twitter_auth.getInstance(a).getAuthorization())

val sparkConf = new SparkConf().setAppName("TwitterPopularTags").setMaster("local[*]")
val ssc = new StreamingContext(sparkConf, Seconds(2))
// ssc.checkpoint("D:/test")
val stream = TwitterUtils.createStream(ssc, atwitter, null, StorageLevel.MEMORY_AND_DISK_2)

val hashTags = stream.map(status => status.getUser().getName())
hashTags.foreachRDD(rdd => {
  rdd.foreach(println)
})

ssc.start()
ssc.awaitTermination()

任何人都可以帮我解决这个问题吗? 谢谢:)

1 个答案:

答案 0 :(得分:1)

转到抛出异常的行,我们可以看到:

if (filters.size > 0) {

对于抛出NPE的那一行,过滤器必须为null,这正是TwitterStream实例化时发生的事情:

  

val stream = TwitterUtils.createStream(ssc,atwitter, null ,StorageLevel.MEMORY_AND_DISK_2)

成为filter序列,请使用Seq()而不是null对其进行初始化。