scala 找不到 org.apache.flink.api.common.typeinfo.TypeInformation[...] 类型的证据参数的隐式值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37920023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
could not find implicit value for evidence parameter of type org.apache.flink.api.common.typeinfo.TypeInformation[...]
提问by jheyd
I am trying to write some use cases for Apache Flink. One error I run into pretty often is
我正在尝试为 Apache Flink 编写一些用例。我经常遇到的一个错误是
could not find implicit value for evidence parameter of type org.apache.flink.api.common.typeinfo.TypeInformation[SomeType]
My problem is that I cant really nail down when they happen and when they dont.
我的问题是我无法确定它们何时发生和何时不发生。
The most recent example of this would be the following
最近的例子如下
...
val largeJoinDataGen = new LargeJoinDataGen(dataSetSize, dataGen, hitRatio)
val see = StreamExecutionEnvironment.getExecutionEnvironment
val newStreamInput = see.addSource(largeJoinDataGen)
...
where LargeJoinDataGen extends GeneratorSource[(Int, String)]and GeneratorSource[T] extends SourceFunction[T], both defined in separate files.
其中LargeJoinDataGen extends GeneratorSource[(Int, String)]和GeneratorSource[T] extends SourceFunction[T],都在单独的文件中定义。
When trying to build this I get
当我尝试构建这个时,我得到
Error:(22, 39) could not find implicit value for evidence parameter of type org.apache.flink.api.common.typeinfo.TypeInformation[(Int, String)]
val newStreamInput = see.addSource(largeJoinDataGen)
1. Why is there an error in the given example?
1. 为什么给定的例子有错误?
2. What would be a general guideline when these errors happen and how to avoid them in the future?
2. 发生这些错误时的一般准则是什么?将来如何避免它们?
P.S.: first scala project and first flink project so please be patient
PS:第一个scala项目和第一个flink项目所以请耐心等待
采纳答案by aljoscha
This mostly happens when you have user code, i.e. a source or a map function or something of that nature that has a generic parameter. In most cases you can fix that by adding something like
这主要发生在您拥有用户代码时,即源代码或地图函数或具有通用参数的类似性质的东西。在大多数情况下,您可以通过添加类似的东西来解决这个问题
implicit val typeInfo = TypeInformation.of(classOf[(Int, String)])
If your code is inside another method that has a generic parameter you can also try adding a context bound to the generic parameter of the method, as in
如果您的代码位于另一个具有泛型参数的方法中,您还可以尝试添加绑定到该方法的泛型参数的上下文,如
def myMethod[T: TypeInformation](input: DataStream[Int]): DataStream[T] = ...
回答by dmreshet
You may make an import instead of implicits
您可以进行导入而不是隐式
import org.apache.flink.streaming.api.scala._
It will also help.
它也会有所帮助。
回答by Yuval Itzchakov
My problem is that I cant really nail down when they happen and when they dont.
我的问题是我无法确定它们何时发生和何时不发生。
They happen when an implicit parameteris required. If we look at the method definition we see:
它们在需要隐式参数时发生。如果我们查看方法定义,我们会看到:
def addSource[T: TypeInformation](function: SourceFunction[T]): DataStream[T]
But we don't see any implicit parameter defined, where is it?
但是我们没有看到定义了任何隐式参数,它在哪里?
When you see a polymorphic method where the type parameter is of the form
当你看到一个多态方法,其中类型参数的形式是
def foo[T : M](param: T)
Where Tis the type parameter and Mis a context bound. It means that the creator of the method is requesting an implicit parameter of type M[T]. It is equivalent to:
哪里T是类型参数,M是上下文绑定。这意味着该方法的创建者正在请求 type 的隐式参数M[T]。它相当于:
def foo[T](param: T)(implicit ev: M[T])
In the case of your method, it is actually expanded to:
就您的方法而言,它实际上扩展为:
def addSource[T](function: SourceFunction[T])(implicit evidence: TypeInformation[T]): DataStream[T]
This is why you see the compiler complaining, as it can't find the implicit parameter the method is requiring.
这就是您看到编译器抱怨的原因,因为它找不到方法所需的隐式参数。
If we go to the Apache Flink Wiki, under Type Informationwe can see why this happens :
如果我们访问 Apache Flink Wiki,在类型信息下我们可以看到为什么会发生这种情况:
No Implicit Value for Evidence Parameter Error
In the case where
TypeInformationcould not be created, programs fail to compile with an error stating “could not find implicit value for evidence parameter of type TypeInformation”.A frequent reason if that the code that generates theTypeInformationhas not been imported. Make sure to import the entire flink.api.scala package. import org.apache.flink.api.scala._
证据参数错误无隐含值
在
TypeInformation无法创建的情况下,程序无法编译并显示错误,指出“找不到类型信息类型的证据参数的隐式值”。如果TypeInformation未导入生成 的代码的常见原因。确保导入整个 flink.api.scala 包。导入 org.apache.flink.api.scala._
For generic methods, you'll need to require them to generate a TypeInformationat the call-site as well:
对于通用方法,您还需要要求它们TypeInformation在调用站点生成一个:
For generic methods, the data types of the function parameters and return type may not be the same for every call and are not known at the site where the method is defined. The code above will result in an error that not enough implicit evidence is available. In such cases, the type information has to be generated at the invocation site and passed to the method. Scala offers implicit parameters for that.
对于泛型方法,每次调用的函数参数和返回类型的数据类型可能都不相同,并且在定义该方法的站点上是未知的。上面的代码将导致一个错误,即没有足够的隐式证据可用。在这种情况下,类型信息必须在调用站点生成并传递给方法。Scala 为此提供了隐式参数。
For your types this means that if the invoking method is generic, it also needs to request the context bound for it's type parameter.
对于您的类型,这意味着如果调用方法是泛型的,它还需要请求为其类型参数绑定的上下文。

