方法1:使用flink DataSet API
points.map(new SelectNearestCenter).withBroadcastSet(currentCentroids, "centroids")//申明对map操作进行广播
import scala.collection.JavaConverters._
final class SelectNearestCenter extends RichMapFunction[DenseVector, (Int, DenseVector)] with Serializable{
private var centroids: Traversable[DenseVector] = null
override def open(parameters: Configuration) {
centroids = getRuntimeContext.getBroadcastVariable[DenseVector]("centroids").asScala
}
def map(p: DenseVector): (Int, DenseVector) = {
//use centroids ...
}
}
方法2:使用Flink ml mapWithBcVariable方法
points.mapWithBcVariable(currentCentroids) {
(point, center) => {
//直接使用广播变量center
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。