-
Notifications
You must be signed in to change notification settings - Fork 6
/
TwitterStreamingSource.scala
284 lines (221 loc) · 9.18 KB
/
TwitterStreamingSource.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package org.twitterstreaming.receiver
import java.io.IOException
import java.util.Optional
import java.util.concurrent.{ArrayBlockingQueue, BlockingQueue, TimeUnit}
import java.sql.Timestamp
import scala.collection.JavaConverters._
import twitter4j.auth.OAuthAuthorization
import twitter4j.conf.ConfigurationBuilder
import twitter4j.Status
import twitter4j.StatusListener
import twitter4j.StatusDeletionNotice
import twitter4j.StallWarning
import twitter4j.TwitterStream
import twitter4j.TwitterStreamFactory
import org.apache.spark.internal.Logging
import org.apache.spark.sql.Row
import org.apache.spark.sql.sources.DataSourceRegister
import org.apache.spark.sql.sources.v2.reader.{DataReader, DataReaderFactory}
import org.apache.spark.sql.sources.v2.reader.streaming.{MicroBatchReader, Offset}
import org.apache.spark.sql.sources.v2.{DataSourceOptions, DataSourceV2, MicroBatchReadSupport}
import org.apache.spark.sql.types._
import scala.collection.mutable.ListBuffer
/**
* Created by hluu on 3/10/18.
*/
class TwitterStreamingSource extends DataSourceV2 with MicroBatchReadSupport with DataSourceRegister with Logging {
override def createMicroBatchReader(
schema: Optional[StructType],
checkpointLocation: String,
options: DataSourceOptions): MicroBatchReader = {
new TwitterStreamMicroBatchReader(options)
}
override def shortName(): String = "twitter"
}
class TwitterStreamMicroBatchReader(options: DataSourceOptions) extends MicroBatchReader with Logging {
private val consumerKey = options.get(TwitterStreamingSource.CONSUMER_KEY).orElse("")
private val consumerSecret = options.get(TwitterStreamingSource.CONSUMER_SECRET).orElse("")
private val accessToken = options.get(TwitterStreamingSource.ACCESS_TOKEN).orElse("")
private val accessTokenSecret = options.get(TwitterStreamingSource.ACCESS_TOKEN_SECRET).orElse("")
private val numPartitions = options.get(TwitterStreamingSource.NUM_PARTITIONS).orElse("5").toInt
private val queueSize = options.get(TwitterStreamingSource.QUEUE_SIZE).orElse("512").toInt
private val debugLevel = options.get(TwitterStreamingSource.DEBUG_LEVEL).orElse("debug").toLowerCase
private val NO_DATA_OFFSET = TwitterOffset(-1)
private var startOffset: TwitterOffset = new TwitterOffset(-1)
private var endOffset: TwitterOffset = new TwitterOffset(-1)
private var currentOffset: TwitterOffset = new TwitterOffset(-1)
private var lastReturnedOffset: TwitterOffset = new TwitterOffset(-2)
private var lastOffsetCommitted : TwitterOffset = new TwitterOffset(-1)
private var incomingEventCounter = 0;
private var stopped:Boolean = false
private var twitterStream:TwitterStream = null
private var worker:Thread = null
private val tweetList:ListBuffer[Status] = new ListBuffer[Status]()
private var tweetQueue:BlockingQueue[Status] = null
initialize()
private def initialize(): Unit = synchronized {
if (consumerKey == "" || consumerSecret == "" || accessToken == "" || accessTokenSecret == "") {
throw new IllegalStateException("One or more pieces of required OAuth info. is missing." +
s" Make sure the following are provided ${TwitterStreamingSource.CONSUMER_KEY}," +
s"${TwitterStreamingSource.CONSUMER_SECRET} ${TwitterStreamingSource.ACCESS_TOKEN} " +
s"${TwitterStreamingSource.ACCESS_TOKEN_SECRET}")
}
tweetQueue = new ArrayBlockingQueue(queueSize)
val configBuilder:ConfigurationBuilder = new ConfigurationBuilder()
configBuilder.setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret)
configBuilder.setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret)
val twitterAuth = new OAuthAuthorization(configBuilder.build())
twitterStream = new TwitterStreamFactory().getInstance(twitterAuth)
twitterStream.addListener(new StatusListener {
def onStatus(status: Status): Unit = {
tweetQueue.add(status)
}
// Unimplemented
def onDeletionNotice(statusDeletionNotice: StatusDeletionNotice) {}
def onTrackLimitationNotice(i: Int) {}
def onScrubGeo(l: Long, l1: Long) {}
def onStallWarning(stallWarning: StallWarning) {}
def onException(e: Exception) {
/*if (!stopped) {
restart("Error receiving tweets", e)
}*/
}
})
worker = new Thread("Tweet Worker") {
setDaemon(true)
override def run() {
receive()
}
}
worker.start()
// start receiving tweets
twitterStream.sample()
}
private def receive(): Unit = {
while(!stopped) {
// poll tweets from queue
val tweet:Status = tweetQueue.poll(100, TimeUnit.MILLISECONDS)
if (tweet != null) {
tweetList.append(tweet);
currentOffset = currentOffset + 1
incomingEventCounter = incomingEventCounter + 1;
}
}
}
override def createDataReaderFactories(): java.util.List[DataReaderFactory[Row]] = {
synchronized {
val startOrdinal = startOffset.offset.toInt + 1
val endOrdinal = endOffset.offset.toInt + 1
internalLog(s"createDataReaderFactories: sOrd: $startOrdinal, eOrd: $endOrdinal, " +
s"lastOffsetCommitted: $lastOffsetCommitted")
val newBlocks = synchronized {
val sliceStart = startOrdinal - lastOffsetCommitted.offset.toInt - 1
val sliceEnd = endOrdinal - lastOffsetCommitted.offset.toInt - 1
assert(sliceStart <= sliceEnd, s"sliceStart: $sliceStart sliceEnd: $sliceEnd")
tweetList.slice(sliceStart, sliceEnd)
}
newBlocks.grouped(numPartitions).map { block =>
new TweetStreamBatchTask(block).asInstanceOf[DataReaderFactory[Row]]
}.toList.asJava
}
}
override def setOffsetRange(start: Optional[Offset],
end: Optional[Offset]): Unit = {
if (start.isPresent && start.get().asInstanceOf[TwitterOffset].offset != currentOffset.offset) {
internalLog(s"setOffsetRange: start: $start, end: $end currentOffset: $currentOffset")
}
this.startOffset = start.orElse(NO_DATA_OFFSET).asInstanceOf[TwitterOffset]
this.endOffset = end.orElse(currentOffset).asInstanceOf[TwitterOffset]
}
override def getStartOffset(): Offset = {
internalLog("getStartOffset was called")
if (startOffset.offset == -1) {
throw new IllegalStateException("startOffset is -1")
}
startOffset
}
override def getEndOffset(): Offset = {
if (endOffset.offset == -1) {
currentOffset
} else {
if (lastReturnedOffset.offset < endOffset.offset) {
internalLog(s"** getEndOffset => $endOffset)")
lastReturnedOffset = endOffset
}
endOffset
}
}
override def commit(end: Offset): Unit = {
internalLog(s"** commit($end) lastOffsetCommitted: $lastOffsetCommitted")
val newOffset = TwitterOffset.convert(end).getOrElse(
sys.error(s"TwitterStreamMicroBatchReader.commit() received an offset ($end) that did not " +
s"originate with an instance of this class")
)
val offsetDiff = (newOffset.offset - lastOffsetCommitted.offset).toInt
if (offsetDiff < 0) {
sys.error(s"Offsets committed out of order: $lastOffsetCommitted followed by $end")
}
tweetList.trimStart(offsetDiff)
lastOffsetCommitted = newOffset
}
override def stop(): Unit = {
log.warn(s"There is a total of $incomingEventCounter events that came in")
stopped = true
if (twitterStream != null) {
try {
twitterStream.shutdown()
} catch {
case e: IOException =>
}
}
}
override def deserializeOffset(json: String): Offset = {
TwitterOffset(json.toLong)
}
override def readSchema(): StructType = {
TwitterStreamingSource.SCHEMA
}
private def internalLog(msg:String): Unit = {
debugLevel match {
case "warn" => log.warn(msg)
case "info" => log.info(msg)
case "debug" => log.debug(msg)
case _ =>
}
}
}
object TwitterStreamingSource {
val CONSUMER_KEY = "consumerKey"
val CONSUMER_SECRET = "consumerSecret"
val ACCESS_TOKEN = "accessToken"
val ACCESS_TOKEN_SECRET = "accessTokenSecret"
val DEBUG_LEVEL = "debugLevel"
val NUM_PARTITIONS = "numPartitions"
val QUEUE_SIZE = "queueSize"
val SCHEMA =
StructType(
StructField("text", StringType) ::
StructField("user", StringType) ::
StructField("userLang", StringType) ::
StructField("createdDate", TimestampType) ::
StructField("isRetweeted", BooleanType) ::
Nil)
}
class TweetStreamBatchTask(tweetList:ListBuffer[Status])
extends DataReaderFactory[Row] {
override def createDataReader(): DataReader[Row] = new TweetStreamBatchReader(tweetList)
}
class TweetStreamBatchReader(tweetList:ListBuffer[Status]) extends DataReader[Row] {
private var currentIdx = -1
override def next(): Boolean = {
// Return true as long as the new index is in the seq.
currentIdx += 1
currentIdx < tweetList.size
}
override def get(): Row = {
val tweet = tweetList(currentIdx)
Row(tweet.getText, tweet.getUser.getScreenName, tweet.getUser.getLang, new Timestamp(tweet.getCreatedAt.getTime),
tweet.isRetweeted)
}
override def close(): Unit = {}
}