gpt4 book ai didi

apache-flink - 泛型参数的 Flink Scala API 函数

转载 作者:行者123 更新时间:2023-12-04 04:58:33 25 4
gpt4 key购买 nike

这是关于 Flink Scala API "not enough arguments" 的后续问题.

我希望能够传递 Flink 的 DataSet 并用它做一些事情,但是数据集的参数是通用的。

这是我现在遇到的问题:

import org.apache.flink.api.scala.ExecutionEnvironment
import org.apache.flink.api.scala._
import scala.reflect.ClassTag

object TestFlink {

def main(args: Array[String]) {
val env = ExecutionEnvironment.getExecutionEnvironment
val text = env.fromElements(
"Who's there?",
"I think I hear them. Stand, ho! Who's there?")

val split = text.flatMap { _.toLowerCase.split("\\W+") filter { _.nonEmpty } }
id(split).print()

env.execute()
}

def id[K: ClassTag](ds: DataSet[K]): DataSet[K] = ds.map(r => r)
}

ds.map(r => r) 有这个错误:

Multiple markers at this line
- not enough arguments for method map: (implicit evidence$256: org.apache.flink.api.common.typeinfo.TypeInformation[K], implicit
evidence$257: scala.reflect.ClassTag[K])org.apache.flink.api.scala.DataSet[K]. Unspecified value parameters evidence$256, evidence$257.
- not enough arguments for method map: (implicit evidence$4: org.apache.flink.api.common.typeinfo.TypeInformation[K], implicit evidence
$5: scala.reflect.ClassTag[K])org.apache.flink.api.scala.DataSet[K]. Unspecified value parameters evidence$4, evidence$5.
- could not find implicit value for evidence parameter of type org.apache.flink.api.common.typeinfo.TypeInformation[K]

当然,这里的id函数只是一个例子,我希望能够用它做一些更复杂的事情。

如何解决?

最佳答案

您还需要将 TypeInformation 作为上下文绑定(bind)在 K 参数上,因此:

def id[K: ClassTag: TypeInformation](ds: DataSet[K]): DataSet[K] = ds.map(r => r)

原因是,Flink 会分析您在程序中使用的类型,并为您使用的每种类型创建一个 TypeInformation 实例。如果要创建通用操作,则需要通过添加上下文绑定(bind)来确保该类型的 TypeInformation 可用。这样,Scala 编译器将确保实例在泛型函数的调用点可用。

关于apache-flink - 泛型参数的 Flink Scala API 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29542214/

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