This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
TwitterTopicBinner.scala
216 lines (186 loc) · 8.89 KB
/
TwitterTopicBinner.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
/*
* Copyright (c) 2013 Oculus Info Inc.
* http://www.oculusinfo.com/
*
* Released under the MIT License.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oculusinfo.twitter.tilegen
import java.text.SimpleDateFormat
import java.util.{Date, List => JavaList}
import com.oculusinfo.binning.TileData
import com.oculusinfo.binning.impl.WebMercatorTilePyramid
import com.oculusinfo.tilegen.tiling.analytics.{AnalysisDescription, CompositeAnalysisDescription}
import com.oculusinfo.tilegen.tiling.{CartesianIndexScheme, RDDBinner, TileIO}
import com.oculusinfo.tilegen.util.ArgumentParser
import com.oculusinfo.twitter.binning.{TwitterTopicAvroSerializer, TwitterDemoTopicRecord}
import org.apache.avro.file.CodecFactory
import org.apache.spark.rdd.RDD
import scala.reflect.ClassTag
import scala.util.Try
object TwitterTopicBinner {
def main (args: Array[String]) {
val argParser = new ArgumentParser(args)
argParser.debug
val sc = argParser.getSparkConnector.createContext(Some("Twitter demo data tiling"))
val source = argParser.getString("source", "The source location at which to find twitter data")
val dateParser = new SimpleDateFormat("yyyy/MM/dd.HH:mm:ss.zzzz")
// Note: don't need a start time for this binning project. Start time is assumed to be 31 days prior to end time.
val endTime = dateParser.parse(argParser.getString("end", "The end time for binning. Format is yyyy/MM/dd.HH:mm:ss.+zzzz"))
val levelSets = argParser.getString("levels",
"The level sets (;-separated) of ,-separated levels to bin.")
.split(";").map(_.split(",").map(_.toInt))
val pyramidId = argParser.getString("id", "An ID by which to identify the finished pyramid.")
val pyramidName = argParser.getString("name", "A name with which to label the finished pyramid").replace("_", " ")
val pyramidDescription = argParser.getString("description", "A description with which to present the finished pyramid").replace("_", " ")
val partitions = argParser.getInt("partitions", "The number of partitions into which to read the raw data", Some(0))
val topicList = argParser.getString("topicList", "Path and filename of list of extracted topics and English translations")
val tileIO = TileIO.fromArguments(argParser)
val files = source.split(",")
// For each file, attempt create an RDD, then immediately force an
// exception in the case it does not exist. Union all RDDs together.
val rawData = files.map { file =>
Try({
val tmp = if (0 == partitions) {
sc.textFile( file )
} else {
sc.textFile( file , partitions)
}
tmp.partitions // force exception if file does not exist
tmp
}).getOrElse( sc.emptyRDD )
}.reduce(_ union _)
val minAnalysis:
AnalysisDescription[TileData[JavaList[TwitterDemoTopicRecord]],
List[TwitterDemoTopicRecord]] =
new TwitterTopicListAnalysis(new TwitterMinRecordAnalytic)
val maxAnalysis:
AnalysisDescription[TileData[JavaList[TwitterDemoTopicRecord]],
List[TwitterDemoTopicRecord]] =
new TwitterTopicListAnalysis(new TwitterMaxRecordAnalytic)
val tileAnalytics: Option[AnalysisDescription[TileData[JavaList[TwitterDemoTopicRecord]],
(List[TwitterDemoTopicRecord],
List[TwitterDemoTopicRecord])]] =
Some(new CompositeAnalysisDescription(minAnalysis, maxAnalysis))
val dataAnalytics: Option[AnalysisDescription[((Double, Double),
Map[String, TwitterDemoTopicRecord]),
Int]] =
None
genericProcessData(rawData, levelSets, tileIO, tileAnalytics, dataAnalytics,
endTime, pyramidId, pyramidName, pyramidDescription, topicList)
}
private def genericProcessData[AT, DT]
(rawData: RDD[String],
levelSets: Array[Array[Int]],
tileIO: TileIO,
tileAnalytics: Option[AnalysisDescription[TileData[JavaList[TwitterDemoTopicRecord]], AT]],
dataAnalytics: Option[AnalysisDescription[((Double, Double),
Map[String, TwitterDemoTopicRecord]), DT]],
endTime: Date,
pyramidId: String,
pyramidName: String,
pyramidDescription: String,
topicList: String) =
{
val tileAnalyticsTag: ClassTag[AT] = tileAnalytics.map(_.analysisTypeTag).getOrElse(ClassTag.apply(classOf[Int]))
val dataAnalyticsTag: ClassTag[DT] = dataAnalytics.map(_.analysisTypeTag).getOrElse(ClassTag.apply(classOf[Int]))
processData(rawData, levelSets, tileIO, tileAnalytics, dataAnalytics,
endTime, pyramidId, pyramidName, pyramidDescription, topicList)(tileAnalyticsTag, dataAnalyticsTag)
}
private def processData[AT: ClassTag, DT: ClassTag]
(rawData: RDD[String],
levelSets: Array[Array[Int]],
tileIO: TileIO,
tileAnalytics: Option[AnalysisDescription[TileData[JavaList[TwitterDemoTopicRecord]], AT]],
dataAnalytics: Option[AnalysisDescription[((Double, Double),
Map[String, TwitterDemoTopicRecord]), DT]],
endTime: Date,
pyramidId: String,
pyramidName: String,
pyramidDescription: String,
topicList: String) =
{
val endTimeSecs = endTime.getTime()/1000; // convert time from msec to sec
val topicMatcher = new TopicMatcher
val topicsMap = topicMatcher.getKeywordList(topicList) // get pre-extracted topics
// append topics to end of data entries
val rawDataWithTopics = topicMatcher.appendTopicsToData(rawData.sparkContext, rawData, topicsMap, endTimeSecs)
val data = rawDataWithTopics.mapPartitions(i =>
{
val recordParser = new TwitterTopicRecordParser(endTimeSecs)
i.flatMap(line =>
{
try {
recordParser.getRecordsByTopic(line)
} catch {
// Just ignore bad records, there aren't many
case _: Throwable => Seq[((Double, Double), Map[String, TwitterDemoTopicRecord])]()
}
}
)
}
).map(record => (record._1, record._2, dataAnalytics.map(_.convert(record))))
data.cache
val binner = new RDDBinner
binner.debug = true
val tilePyramid = new WebMercatorTilePyramid
// Add global analytic accumulators
val sc = rawData.context
tileAnalytics.map(_.addGlobalAccumulator(sc))
dataAnalytics.map(_.addGlobalAccumulator(sc))
levelSets.foreach(levelSet =>
{
println()
println()
println()
println("Starting binning levels "+levelSet.mkString("[", ",", "]")+" at "+new Date())
// Add whole-level analytic accumulators for these levels
tileAnalytics.map(analytic =>
levelSet.map(level => analytic.addLevelAccumulator(sc, level))
)
dataAnalytics.map(analytic =>
levelSet.map(level => analytic.addLevelAccumulator(sc, level))
)
// Do actual binning
val taskStart = System.currentTimeMillis
val tiles = binner.processDataByLevel(data,
new CartesianIndexScheme,
new TwitterTopicBinningAnalytic,
tileAnalytics,
dataAnalytics,
tilePyramid,
levelSet,
xBins=1,
yBins=1)
tileIO.writeTileSet(tilePyramid,
pyramidId,
tiles,
new TwitterTopicAvroSerializer(CodecFactory.bzip2Codec()),
tileAnalytics,
dataAnalytics,
pyramidName,
pyramidDescription)
val taskEnd = System.currentTimeMillis()
val elapsedMinutes = (taskEnd - taskStart)/60000.0
println("Finished binning levels "+levelSet.mkString("[", ",", "]")+" at "+new Date())
println("\telapsed time: "+elapsedMinutes+" minutes")
println()
}
)
}
}