本文整理汇总了Scala中twitter4j.auth.Authorization类的典型用法代码示例。如果您正苦于以下问题:Scala Authorization类的具体用法?Scala Authorization怎么用?Scala Authorization使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Authorization类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: TwitterInputDStream
//设置package包名称以及导入依赖的类
package com.aluxian.tweeather.streaming
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.dstream.ReceiverInputDStream
import org.apache.spark.streaming.receiver.Receiver
import twitter4j.auth.{Authorization, OAuthAuthorization}
import twitter4j.conf.ConfigurationBuilder
import twitter4j.{FilterQuery, Status}
class TwitterInputDStream(@transient ssc: StreamingContext,
twitterAuth: Option[Authorization],
filterQuery: Option[FilterQuery],
storageLevel: StorageLevel
) extends ReceiverInputDStream[Status](ssc) {
private val authorization = twitterAuth.getOrElse(createOAuthAuthorization())
private def createOAuthAuthorization(): Authorization = {
new OAuthAuthorization(new ConfigurationBuilder().build())
}
override def getReceiver(): Receiver[Status] = {
new TwitterReceiver(authorization, filterQuery, storageLevel)
}
}
示例2: TwitterUtils
//设置package包名称以及导入依赖的类
package com.aluxian.tweeather.streaming
import java.util.Properties
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.dstream.DStream
import twitter4j.auth.{AccessToken, Authorization}
import twitter4j.{FilterQuery, Status, TwitterFactory}
object TwitterUtils {
def createMultiStream(ssc: StreamingContext,
queryBuilder: () => FilterQuery = () => null,
credentials: Seq[Authorization] = loadDefaultCredentials(),
storageLevel: StorageLevel = StorageLevel.MEMORY_AND_DISK_SER
): DStream[Status] = {
credentials
.map(auth => createStream(ssc, Some(queryBuilder()), Some(auth)))
.reduce { (accStream, stream) => accStream.union(stream) }
}
private def loadDefaultCredentials(): Seq[Authorization] = {
val props = loadTwitterProperties()
val num = props.getProperty("twitter.credentials").toInt
1.to(num).map(i => {
val twitter = new TwitterFactory().getInstance()
twitter.setOAuthConsumer(
props.getProperty(s"twitter.credentials.$i.consumerKey"),
props.getProperty(s"twitter.credentials.$i.consumerSecret")
)
twitter.setOAuthAccessToken(new AccessToken(
props.getProperty(s"twitter.credentials.$i.token"),
props.getProperty(s"twitter.credentials.$i.tokenSecret")
))
twitter.getAuthorization
})
}
private def loadTwitterProperties(): Properties = {
val properties = new Properties()
val stream = getClass.getResourceAsStream("/com/aluxian/tweeather/res/twitter.properties")
properties.load(stream)
stream.close()
properties
}
}