gpt4 book ai didi

java - Apache flink 维基百科使用 Scala 编辑分析

转载 作者:行者123 更新时间:2023-12-01 09:04:39 26 4
gpt4 key购买 nike

我正在尝试将 Apache Flink 教程中的维基百科编辑流分析重写为 Scala https://ci.apache.org/projects/flink/flink-docs-release-1.2/quickstart/run_example_quickstart.html

教程中的代码是

import org.apache.flink.api.common.functions.FoldFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.wikiedits.WikipediaEditEvent;
import org.apache.flink.streaming.connectors.wikiedits.WikipediaEditsSource;

public class WikipediaAnalysis {

public static void main(String[] args) throws Exception {

StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();

DataStream<WikipediaEditEvent> edits = see.addSource(new WikipediaEditsSource());

KeyedStream<WikipediaEditEvent, String> keyedEdits = edits
.keyBy(new KeySelector<WikipediaEditEvent, String>() {
@Override
public String getKey(WikipediaEditEvent event) {
return event.getUser();
}
});

DataStream<Tuple2<String, Long>> result = keyedEdits
.timeWindow(Time.seconds(5))
.fold(new Tuple2<>("", 0L), new FoldFunction<WikipediaEditEvent, Tuple2<String, Long>>() {
@Override
public Tuple2<String, Long> fold(Tuple2<String, Long> acc, WikipediaEditEvent event) {
acc.f0 = event.getUser();
acc.f1 += event.getByteDiff();
return acc;
}
});

result.print();

see.execute();
}
}

下面是我在 scala 中的尝试

import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
import org.apache.flink.streaming.connectors.wikiedits.{WikipediaEditEvent, WikipediaEditsSource}
import org.apache.flink.streaming.api.windowing.time.Time


object WikipediaAnalytics extends App{

val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

val edits = env.addSource(new WikipediaEditsSource());

val keyedEdits = edits.keyBy(event => event.getUser)

val result = keyedEdits.timeWindow(Time.seconds(5)).fold(("", 0L), (we: WikipediaEditEvent, t: (String, Long)) =>
(we.getUser, t._2 + we.getByteDiff))

}

这或多或少是到 scala 的字对字转换,基于此 val 结果 的类型应该是 DataStream[(String, Long)] 但是fold() 之后推断出的实际类型相差甚远。

请帮助确定 scala 代码有什么问题

EDIT1:使用 fold[R] 的柯里化(Currying)原理图进行了以下更改,现在类型确认为预期类型,但还是没找到原因

  val result_1: (((String, Long), WikipediaEditEvent) => (String, Long)) => DataStream[(String, Long)] =
keyedEdits.timeWindow(Time.seconds(5)).fold(("", 0L))

val result_2: DataStream[(String, Long)] = result_1((t: (String, Long), we: WikipediaEditEvent ) =>
(we.getUser, t._2 + we.getByteDiff))

最佳答案

问题似乎出在折叠上,您必须在累加器初始值之后有一个右括号。当您修复该问题时,代码将无法编译,因为它没有可用于 WikipediaEditEvent 的 TypeInformation。解决这个问题最简单的方法是导入更多的 flink scala API。请参阅下面的完整示例:

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.wikiedits.WikipediaEditsSource
import org.apache.flink.streaming.api.windowing.time.Time

object WikipediaAnalytics extends App {
val see: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
val edits = see.addSource(new WikipediaEditsSource())
val userEditsVolume: DataStream[(String, Int)] = edits
.keyBy(_.getUser)
.timeWindow(Time.seconds(5))
.fold(("", 0))((acc, event) => (event.getUser, acc._2 + event.getByteDiff))
userEditsVolume.print()
see.execute("Wikipedia User Edit Volume")
}

关于java - Apache flink 维基百科使用 Scala 编辑分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41383878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com