Flink之DataSet 的Join、Cross使用

本文探讨了Flink中的DataSet API如何进行Join和Cross操作。详细解释了内连接(默认)、左连接、右连接及全连接的用法,并举例说明了笛卡尔积的实现,展示了输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Join 默认就是内连接,还有leftOuterJoin,rightOuterJoin,fullOuterJoin
cross 笛卡尔积

package com.ruozedata.flink

import org.apache.flink.api.scala.ExecutionEnvironment
import org.apache.flink.api.scala._
import scala.collection.mutable.ListBuffer

object FlinkJoinApp {
   
   
  def main(args: Array[String]): Unit = {
   
   

    val env=ExecutionEnvironment.getExecutionEnvironment

    val data1=ListBuffer[(Int,String)]()
    data1.append((1,"ruoze"))
    data1.append((2,"jepson"))
    data1.append((3,"xingxing"))

    val data2=ListBuffer[(Int,String)]()
    data2.append((1,"beijing"))
    data2.append((2,"shanghai"))
    data2.append((4,"hangzhou"))

    val a
K-means 是一个经典的聚类算法,Flink 也提供了 K-means 的实现。以下是一个简单的 Flink K-means 的例子: 首先,我们需要定义数据点的数据结构: ```java public class Point implements Serializable { public double x; public double y; public int clusterId; public Point(double x, double y) { this.x = x; this.y = y; this.clusterId = -1; } public Point(double x, double y, int clusterId) { this.x = x; this.y = y; this.clusterId = clusterId; } public void setClusterId(int clusterId) { this.clusterId = clusterId; } public int getClusterId() { return clusterId; } public double distance(Point other) { double dx = this.x - other.x; double dy = this.y - other.y; return Math.sqrt(dx * dx + dy * dy); } public String toString() { return "(" + x + "," + y + ")"; } } ``` 然后,我们可以创建一个 Flink Execution Environment,读取数据集,并使用 KMeans() 方法来执行 K-means 算法: ```java ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); DataSet<Point> points = env.readTextFile("path/to/points.txt").map(line -> { String[] fields = line.split(","); double x = Double.parseDouble(fields[0]); double y = Double.parseDouble(fields[1]); return new Point(x, y); }); int k = 3; int maxIterations = 10; DataSet<Point> centroids = points.sample(true, k).map(point -> new Point(point.x, point.y, 0)); DataSet<Point> clusteredPoints = points.map(point -> new Point(point.x, point.y, -1)) .iterate(maxIterations, loop -> { DataSet<Point> newCentroids = loop .join(centroids).where((KeySelector<Point, Integer>) Point::getClusterId) .equalTo((KeySelector<Point, Integer>) Point::getClusterId) .with((point, centroid) -> new Tuple2<>(point, centroid)) .groupBy(tuple -> tuple.f1) .reduceGroup(new CentroidReducer()) .map(tuple -> new Point(tuple.f0.x, tuple.f0.y, tuple.f1)); DataSet<Point> newClusteredPoints = loop .cross(newCentroids) .map(new CrossFunction<Point, Point, Tuple2<Point, Double>>() { @Override public Tuple2<Point, Double> cross(Point point, Point centroid) { double distance = point.distance(centroid); return new Tuple2<>(point, distance); } }) .groupBy(tuple -> tuple.f0) .minBy(1) .map(tuple -> { Point point = tuple.f0; double distance = tuple.f1; point.setClusterId((int) point.y); return point; }); Tuple2<DataSet<Point>, DataSet<Point>> result = loop.closeWith(newClusteredPoints, newCentroids); return result.f0; }); clusteredPoints.print(); ``` 在上面的代码中,我们使用 readTextFile() 方法从文本文件中读取数据集,并使用 map() 方法将数据解析成数据点。我们使用 sample() 方法从数据集中随机选择 k 个点作为初始的聚类中心,并使用 iterate() 方法来迭代执行 K-means 算法。 在每次迭代中,我们首先将数据点与聚类中心进行关联,并使用 reduceGroup() 方法来计算每个聚类的中心点。然后,我们将数据点与聚类中心进行交叉操作,并使用 minBy() 方法找到每个数据点距离最近的聚类中心,并将数据点分配到该聚类中心。最后,我们使用 closeWith() 方法将新的聚类中心和数据点传递给下一次迭代,并使用 print() 方法打印结果。 以上就是一个简单的 Flink K-means 的例子。在实际应用中,我们可以根据需要定义更复杂的业务逻辑,并使用 Flink 来实现各种复杂的数据处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值