热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

FlinkScalaAPI在泛型参数上执行函数-FlinkScalaAPIfunctionsongenericparameters

ItsafollowupquestiononFlinkScalaAPInotenougharguments.这是关于FlinkScalaAPI“参数不足”的后续问题

It's a follow up question on Flink Scala API "not enough arguments".

这是关于Flink Scala API“参数不足”的后续问题。

I'd like to be able to pass Flink's DataSets around and do something with it, but the parameters to the dataset are generic.

我希望能够传递Flink的数据集并对其进行处理,但是数据集的参数是通用的。

Here's the problem I have now:

我现在遇到的问题是:

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)
}

I have this error for ds.map(r => r):

这是ds的误差。地图(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]

Of course, the id function here is just an example, and I'd like to be able to do something more complex with it.

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

How it can be solved?

如何解决这个问题?

1 个解决方案

#1


7  

you also need to have TypeInformation as a context bound on the K parameter, so:

您还需要将类型信息作为上下文绑定在K参数上,所以:

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

The reason is, that Flink analyses the types that you use in your program and creates a TypeInformation instance for each type you use. If you want to create generic operations then you need to make sure a TypeInformation of that type is available by adding a context bound. This way, the Scala compiler will make sure an instance is available at the call site of the generic function.

原因是Flink分析了您在程序中使用的类型,并为您使用的每种类型创建了一个类型信息实例。如果您想要创建泛型操作,那么您需要通过添加上下文绑定来确保该类型的类型信息是可用的。通过这种方式,Scala编译器将确保一个实例在泛型函数的调用站点上可用。


推荐阅读
author-avatar
木卫二
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有