Skip to content

Instantly share code, notes, and snippets.

@fpopic
Created March 18, 2018 22:47
Show Gist options
  • Select an option

  • Save fpopic/0723042f28ed667279b3e3ae2b37ccc5 to your computer and use it in GitHub Desktop.

Select an option

Save fpopic/0723042f28ed667279b3e3ae2b37ccc5 to your computer and use it in GitHub Desktop.
Reduce and ReduceByKey in BEAM 2.3.0
import org.apache.beam.sdk.Pipeline
import org.apache.beam.sdk.transforms.Combine.BinaryCombineFn
import org.apache.beam.sdk.transforms.{Combine, Create, DoFn, MapElements}
import org.apache.beam.sdk.values.{KV, TypeDescriptors}
case class MyClass(int: Int)
class MyBinaryFn extends BinaryCombineFn[MyClass] {
def apply(left: MyClass, right: MyClass): MyClass = {
println(left, right)
if (left.int > right.int) left else right
}
}
object BinaryCombineFnMain {
import collection.JavaConverters._
def main(args: Array[String]): Unit = {
val pipeline = Pipeline.create()
pipeline
.apply("create elems", Create.of(Seq(MyClass(1), MyClass(3), MyClass(0), MyClass(2)).asJava))
.apply("Reduce", Combine.globally(new MyBinaryFn))
.apply(MapElements
.into(TypeDescriptors.strings())
.via { (x: MyClass) =>
println(x)
x.toString
}
)
pipeline.run()
}
}
object BinaryCombineFnPerKeyMain {
import collection.JavaConverters._
def main(args: Array[String]): Unit = {
val pipeline = Pipeline.create()
pipeline
.apply("create kvs", Create.of(Seq(
KV.of(1, MyClass(1)), KV.of(1, MyClass(3)),
KV.of(2, MyClass(2)), KV.of(2, MyClass(0))).asJava))
.apply("ReduceByKey", Combine.perKey[Int, MyClass, MyClass](new MyBinaryFn))
.apply(MapElements
.into(TypeDescriptors.strings())
.via { (x: KV[Int, MyClass]) =>
println(x)
x.toString
}
)
pipeline.run()
}
}
object BinaryCombineFnPerKeyWithHotKeyFanoutMain {
import collection.JavaConverters._
def main(args: Array[String]): Unit = {
val pipeline = Pipeline.create()
pipeline
.apply("create kvs", Create.of(Seq(
KV.of(1, MyClass(1)), KV.of(1, MyClass(3)),
KV.of(2, MyClass(2)), KV.of(2, MyClass(0))).asJava))
.apply("ReduceByKey", Combine
.perKey[Int, MyClass, MyClass](new MyBinaryFn)
.withHotKeyFanout(10))
.apply(MapElements
.into(TypeDescriptors.strings())
.via { (x: KV[Int, MyClass]) =>
println(x)
x.toString
}
)
pipeline.run()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment