gpt4 book ai didi

scala - 如何使用 ValueMapper 使用 Scala 更改 Kafka Streams 10.2 中的值类型

转载 作者:行者123 更新时间:2023-12-05 00:51:16 25 4
gpt4 key购买 nike

我正在尝试将基于 Scala 的 Kafka Streams 应用程序从 0.10.0.0 升级到 0.10.2.1,但我不知道如何编译该应用程序。

我在 documentation 中找到的示例用途 mapValue但它不会改变值类型。我将 Scala 2.11 与 -Xexperimental 一起使用按照 this 的编译器标志.

代码

class MyStream() {
def startMyStream(): Unit = {
val kStreamBuilder = new KStreamBuilder
val kStream = kStreamBuilder.stream("myTopic")

kStream.mapValues(new ValueMapper[AnyRef, Double]() {
override def apply(value: Any) = 6.3
})

val kafkaStreams = new KafkaStreams(kStreamBuilder, new Properties)
kafkaStreams.start()
}
}

编译器错误
no type parameters for method mapValues: (x$1: org.apache.kafka.streams.kstream.ValueMapper[_, _ <: VR])org.apache.kafka.streams.kstream.KStream[Nothing,VR] exist so that it can be applied to arguments (org.apache.kafka.streams.kstream.ValueMapper[AnyRef,Double]{})
--- because ---
argument expression's type is not compatible with formal parameter type;
found : org.apache.kafka.streams.kstream.ValueMapper[AnyRef,Double]
required: org.apache.kafka.streams.kstream.ValueMapper[_, _ <: ?VR]
Note: AnyRef <: Any, but Java-defined trait ValueMapper is invariant in type V.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
kStream.mapValues(new ValueMapper[AnyRef, Double]() {
^
type mismatch;
found : org.apache.kafka.streams.kstream.ValueMapper[AnyRef,Double]{}
required: org.apache.kafka.streams.kstream.ValueMapper[_, _ <: VR]
kStream.mapValues(new ValueMapper[AnyRef, Double]() {
^
two errors found

表示为一个 java 类,它可以很好地编译。
public class MyStream {
public void startMyStream() {
KStreamBuilder kStreamBuilder = new KStreamBuilder();
KStream kStream = kStreamBuilder.stream("myTopic");

kStream.mapValues(new ValueMapper<Object, Double>() {
@Override
public Double apply(Object value) {
return 6.3;
}
});

KafkaStreams kafkaStreams = new KafkaStreams(kStreamBuilder, new Properties());
kafkaStreams.start();
}
}

我如何获得要编译的 Scala 版本?

基于此的解决方案 answer

三种有效的方法和两种无效的方法。
class MyStream() {
def startMyStream(): Unit = {
val kStreamBuilder = new KStreamBuilder

// Explicit tying here is not required to compile and run.
val kStream: KStream[Array[Byte], String] = kStreamBuilder.stream("myTopic")

// Does not compile
kStream.mapValues(new ValueMapper[AnyRef, Double]() {
override def apply(value: AnyRef) = 6.3
})

// Does not compile
kStream.mapValues(_ => 6.3)

// Works
kStream.mapValues[Double](new ValueMapper[AnyRef, Double]() {
override def apply(value: AnyRef) = 6.3
})

// Works, requires compiler option -Xexperimental
kStream.mapValues[Double](_ => 6.3)

// Works, requires compiler option -Xexperimental
kStream.mapValues[Double](convert)
def convert(string: String): Double = 6.3

val kafkaStreams = new KafkaStreams(kStreamBuilder, new Properties)
kafkaStreams.start()
}
}

更新:无效的解决方案

尝试 1 按照建议( val kStream: KStream[Array[Byte], String] = kStreamBuilder.stream("myTopic") )向 kStream 添加显式类型仍然无法编译并导致此错误。
no type parameters for method mapValues: (x$1: org.apache.kafka.streams.kstream.ValueMapper[_ >: String, _ <: VR])org.apache.kafka.streams.kstream.KStream[Array[Byte],VR] exist so that it can be applied to arguments (org.apache.kafka.streams.kstream.ValueMapper[AnyRef,Double]{})
--- because ---
argument expression's type is not compatible with formal parameter type;
found : org.apache.kafka.streams.kstream.ValueMapper[AnyRef,Double]
required: org.apache.kafka.streams.kstream.ValueMapper[_ >: String, _ <: ?VR]
Note: AnyRef <: Any, but Java-defined trait ValueMapper is invariant in type V.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
kStream.mapValues(new ValueMapper[AnyRef, Double]() {
^
type mismatch;
found : org.apache.kafka.streams.kstream.ValueMapper[AnyRef,Double]{}
required: org.apache.kafka.streams.kstream.ValueMapper[_ >: String, _ <: VR]
kStream.mapValues(new ValueMapper[AnyRef, Double]() {

尝试 2 添加上述内容并使用 SAM 转换来“避免显式写出匿名类的实例化”( kStream.mapValues(_ => 6.3) )导致此编译器错误。
no type parameters for method mapValues: (x$1: org.apache.kafka.streams.kstream.ValueMapper[_ >: String, _ <: VR])org.apache.kafka.streams.kstream.KStream[Array[Byte],VR] exist so that it can be applied to arguments (org.apache.kafka.streams.kstream.ValueMapper[String,Double] with Serializable)
--- because ---
argument expression's type is not compatible with formal parameter type;
found : org.apache.kafka.streams.kstream.ValueMapper[String,Double] with Serializable
required: org.apache.kafka.streams.kstream.ValueMapper[_ >: String, _ <: ?VR]
Note: String <: Any (and org.apache.kafka.streams.kstream.ValueMapper[String,Double] with Serializable <: org.apache.kafka.streams.kstream.ValueMapper[String,Double]), but Java-defined trait ValueMapper is invariant in type V.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
kStream.mapValues(_ => 6.3)
^
type mismatch;
found : org.apache.kafka.streams.kstream.ValueMapper[String,Double] with Serializable
required: org.apache.kafka.streams.kstream.ValueMapper[_ >: String, _ <: VR]
kStream.mapValues(_ => 6.3)
^

最佳答案

这里有两个问题。首先,您不指定流的类型:

val kStream: KStream[Array[Byte], String] = kStreamBuilder.stream("myTopic")

这实际上是您看到的错误的原因 - 由于您没有指定类型,Scala 将它们推断为某些默认值,这几乎总是不是您想要的。

其次,由于您已启用 -Xexperimental ,您可以依靠 SAM 转换来避免显式写出匿名类的实例:
kStream.mapValues(_ => 6.3)

更新:似乎由于某种原因 Scala 编译器无法正确推断匿名函数/SAM 实例的输出类型。我能够通过以下小调整成功编译代码:
kStream.mapValues[Double](_ => 6.3)

关于scala - 如何使用 ValueMapper 使用 Scala 更改 Kafka Streams 10.2 中的值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44731736/

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