Spark Jupyter Notebook不显示Scala控制台输出

时间:2018-07-05 12:08:15

标签: scala apache-spark spark-streaming

1)我正在学习流式传输,并遇到控制台(scala)上未显示任何问题(通过sendEVent的println)。我进一步尝试植入println(“ xyz”)行,发现只有在未将其嵌入在'while'块的块中时,它才会被打印...否则,即使放在while循环之前也不会被打印。我在这些println(“ xyz”)中多放了几行,发现有些行可能被挡住了……而只有最后一行被打印出了。

以前,我还两次在Storm流中遇到了两个不同的代码段:从Jupyter Notebook打印出的任何内容都没有,但是在Scala Shell上完全没问题。

2)我也想知道在那些awaitTermination()中,例如: messages.writeStream.outputMode(“ append”)。format(“ console”)。option(“ truncate”,false).start()。awaitTermination()(我也没有从控制台获得任何输出)

或以下“ infinitive loop”(以下代码): var完成=假 while(!finished){................. ..}

他们在等待停顿或[CTR] C ...这样的艰难休息还是如何正确休息?因此,下一行将被执行。当我编写示例/教程的作者对此一无所知时,我感到非常困惑。

enter code here

import java.util._
import scala.collection.JavaConverters._
import java.util.concurrent._

import org.apache.spark._
import org.apache.spark.streaming._
import org.apache.spark.eventhubs.ConnectionStringBuilder

// Event hub configurations
// Replace values below with yours        
val eventHubName = "<Event hub name>"
val eventHubNSConnStr = "<Event hub namespace connection string>"
val connStr = ConnectionStringBuilder (eventHubNSConnStr)
.setEventHubName(eventHubName).build 

import com.microsoft.azure.eventhubs._
val pool = Executors.newFixedThreadPool(1)
val eventHubClient = EventHubClient.create(connStr.toString(), pool)

def sendEvent(message: String) = {
  val messageData = EventData.create(message.getBytes("UTF-8"))
  eventHubClient.get().send(messageData)
  println("Sent event: " + message + "\n")
}


import twitter4j._
import twitter4j.TwitterFactory
import twitter4j.Twitter
import twitter4j.conf.ConfigurationBuilder

// Twitter application configurations
// Replace values below with yours   
val twitterConsumerKey = "<CONSUMER KEY>"
val twitterConsumerSecret = "<CONSUMER SECRET>"
val twitterOauthAccessToken = "<ACCESS TOKEN>"
val twitterOauthTokenSecret = "<TOKEN SECRET>"

val cb = new ConfigurationBuilder()
cb.setDebugEnabled
(true).setOAuthConsumerKeywitterConsumerKey).setOAuthConsumerSecret
(twitterConsumerSecret).setOAuthAccessToken
(twitterOauthAccessToken).setOAuthAccessTokenSecret(twitterOauthTokenSecret)

val twitterFactory = new TwitterFactory(cb.build())
val twitter = twitterFactory.getInstance()

//Getting tweets with keyword "Azure" and sending them to Event Hub realtime 

val query = new Query(" #Azure ")
query.setCount(100)
query.lang("en")
var finished = false
while (!finished) {
  val result = twitter.search(query)
  val statuses = result.getTweets()
  var lowestStatusId = Long.MaxValue
  for (status <- statuses.asScala) {
  if(!status.isRetweet()){
  sendEvent(status.getText())
}
  lowestStatusId = Math.min(status.getId(), lowestStatusId)
  Thread.sleep(2000)
}
query.setMaxId(lowestStatusId - 1)
}

// Closing connection to the Event Hub
eventHubClient.get().close()

0 个答案:

没有答案