This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 267
/
SourceBuilder.scala
225 lines (196 loc) · 8.24 KB
/
SourceBuilder.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
/*
Copyright 2013 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.twitter.summingbird.builder
import com.twitter.algebird.{ Monoid, Semigroup }
import com.twitter.bijection.Codec
import com.twitter.chill.IKryoRegistrar
import com.twitter.chill.java.IterableRegistrar
import com.twitter.storehaus.algebra.MergeableStore
import com.twitter.storehaus.ReadableStore
import com.twitter.summingbird._
import com.twitter.summingbird.batch.{ BatchID, Batcher }
import com.twitter.summingbird.scalding.{ Scalding, ScaldingEnv }
import com.twitter.summingbird.scalding.batch.BatchedStore
import com.twitter.summingbird.service.CompoundService
import com.twitter.summingbird.sink.{ CompoundSink, BatchedSinkFromOffline }
import com.twitter.summingbird.source.EventSource
import com.twitter.summingbird.store.CompoundStore
import com.twitter.summingbird.online._
import com.twitter.summingbird.storm.{
Storm,
StormEnv,
StormSink
}
import java.io.Serializable
import java.util.Date
/**
* The (deprecated) Summingbird builder API builds up a single
* MapReduce job using a SourceBuilder. After any number of calls to
* flatMap, leftJoin, filter, merge, etc, the user calls
* "groupAndSumTo", equivalent to "sumByKey" in the Producer
* API. This call converts the SourceBuilder into a CompletedBuilder
* and prevents and future flatMap operations.
*
* @author Oscar Boykin
* @author Sam Ritchie
* @author Ashu Singhal
*/
object SourceBuilder {
type PlatformPair = OptionalPlatform2[Scalding, Storm]
type Node[T] = Producer[PlatformPair, T]
private val nextId = new java.util.concurrent.atomic.AtomicLong(0)
def adjust[T](m: Map[T, Options], k: T)(f: Options => Options) =
m.updated(k, f(m.getOrElse(k, Options())))
implicit def sg[T]: Semigroup[SourceBuilder[T]] =
Semigroup.from(_ ++ _)
def nextName[T: Manifest]: String =
"%s_%d".format(manifest[T], nextId.getAndIncrement)
def apply[T](eventSource: EventSource[T], timeOf: T => Date)(implicit mf: Manifest[T], eventCodec: Codec[T]) = {
implicit val te = TimeExtractor[T](timeOf(_).getTime)
val newID = nextName[T]
val scaldingSource =
eventSource.offline.map(s => Scalding.pipeFactory(s.scaldingSource(_)))
val stormSource = eventSource.spout.map(Storm.toStormSource(_))
new SourceBuilder[T](
Source[PlatformPair, T]((scaldingSource, stormSource)),
CompletedBuilder.injectionRegistrar[T](eventCodec),
newID
)
}
}
case class SourceBuilder[T: Manifest] private (
@transient node: SourceBuilder.Node[T],
@transient registrar: IKryoRegistrar,
id: String,
@transient opts: Map[String, Options] = Map.empty) extends Serializable {
import SourceBuilder.{ adjust, Node, nextName }
def map[U: Manifest](fn: T => U): SourceBuilder[U] = copy(node = node.map(fn))
def filter(fn: T => Boolean): SourceBuilder[T] = copy(node = node.filter(fn))
def flatMap[U: Manifest](fn: T => TraversableOnce[U]): SourceBuilder[U] =
copy(node = node.flatMap(fn))
/**
* This may be more efficient if you know you are not changing the values in
* you flatMap.
*/
def flatMapKeys[K1, K2, V](fn: K1 => TraversableOnce[K2])(implicit ev: T <:< (K1, V),
key1Mf: Manifest[K1], key2Mf: Manifest[K2], valMf: Manifest[V]): SourceBuilder[(K2, V)] =
copy(node = node.asInstanceOf[Node[(K1, V)]].flatMapKeys(fn))
def flatMapBuilder[U: Manifest](newFlatMapper: FlatMapper[T, U]): SourceBuilder[U] =
flatMap(newFlatMapper(_))
def write[U](sink: CompoundSink[U])(conversion: T => TraversableOnce[U])(implicit batcher: Batcher, mf: Manifest[U]): SourceBuilder[T] = {
val newNode =
node.flatMap(conversion).write((
sink.offline.map(new BatchedSinkFromOffline[U](batcher, _)),
sink.online.map { supplier => new StormSink[U] { lazy val toFn = supplier() } }
))
copy(
node = node.either(newNode).flatMap[T] {
case Left(t) => Some(t)
case Right(u) => None
}
)
}
def write(sink: CompoundSink[T])(implicit batcher: Batcher): SourceBuilder[T] =
copy(
node = node.write((
sink.offline.map(new BatchedSinkFromOffline[T](batcher, _)),
sink.online.map { supplier => new StormSink[T] { lazy val toFn = supplier() } }
))
)
def leftJoin[K, V, JoinedValue](service: CompoundService[K, JoinedValue])(implicit ev: T <:< (K, V), keyMf: Manifest[K], valMf: Manifest[V], joinedMf: Manifest[JoinedValue]): SourceBuilder[(K, (V, Option[JoinedValue]))] =
copy(
node = node.asInstanceOf[Node[(K, V)]].leftJoin((
service.offline,
service.online.map { fn: Function0[ReadableStore[K, JoinedValue]] =>
ReadableServiceFactory(fn)
}
))
)
/** Set's an Option on all nodes ABOVE this point */
def set(opt: Any): SourceBuilder[T] = copy(opts = adjust(opts, id)(_.set(opt)))
/**
* Complete this builder instance with a BatchStore. At this point,
* the Summingbird job can be executed on Hadoop.
*/
def groupAndSumTo[K, V](store: BatchedStore[K, V])(
implicit ev: T <:< (K, V),
env: Env,
keyMf: Manifest[K],
valMf: Manifest[V],
keyCodec: Codec[K],
valCodec: Codec[V],
batcher: Batcher,
monoid: Monoid[V]): CompletedBuilder[_, K, V] =
groupAndSumTo(CompoundStore.fromOffline(store))
/**
* Complete this builder instance with a MergeableStore. At this point,
* the Summingbird job can be executed on Storm.
*/
def groupAndSumTo[K, V](store: => MergeableStore[(K, BatchID), V])(
implicit ev: T <:< (K, V),
env: Env,
keyMf: Manifest[K],
valMf: Manifest[V],
keyCodec: Codec[K],
valCodec: Codec[V],
batcher: Batcher,
monoid: Monoid[V]): CompletedBuilder[_, K, V] =
groupAndSumTo(CompoundStore.fromOnline(store))
/**
* Complete this builder instance with a CompoundStore. At this
* point, the Summingbird job can be executed on Storm or Hadoop.
*/
def groupAndSumTo[K, V](store: CompoundStore[K, V])(
implicit ev: T <:< (K, V),
env: Env,
keyMf: Manifest[K],
valMf: Manifest[V],
keyCodec: Codec[K],
valCodec: Codec[V],
batcher: Batcher,
monoid: Monoid[V]): CompletedBuilder[_, K, V] = {
val cb = env match {
case scalding: ScaldingEnv =>
val givenStore = store.offlineStore.getOrElse(sys.error("No offline store given in Scalding mode"))
// Set the store to reset if needed
val batchSetStore = scalding
.initialBatch(batcher)
.map { givenStore.withInitialBatch(_) }
.getOrElse(givenStore)
val newNode = OptionalUnzip2[Scalding, Storm]()(node)._1.map { p =>
Producer.evToKeyed(p.name(id))
.sumByKey(batchSetStore)
}.getOrElse(sys.error("Scalding mode specified alongside some online-only Source, Service or Sink."))
CompletedBuilder(newNode, registrar, batcher, keyCodec, valCodec, nextName[(K, V)], opts)
case storm: StormEnv =>
val supplier = store.onlineSupplier.getOrElse(sys.error("No online store given in Storm mode"))
val givenStore = MergeableStoreFactory.from(supplier())
val newNode = OptionalUnzip2[Scalding, Storm]()(node)._2.map { p =>
Producer.evToKeyed(p.name(id))
.sumByKey(givenStore)
}.getOrElse(sys.error("Storm mode specified alongside some offline-only Source, Service or Sink."))
CompletedBuilder(newNode, registrar, batcher, keyCodec, valCodec, nextName[(K, V)], opts)
case _ => sys.error("Unknown environment: " + env)
}
env.builder = cb
cb
}
// useful when you need to merge two different Event sources
def ++(other: SourceBuilder[T]): SourceBuilder[T] =
copy(
node = node.name(id).merge(other.node.name(other.id)),
registrar = new IterableRegistrar(registrar, other.registrar),
id = "merge_" + nextName[T],
opts = opts ++ other.opts
)
}