Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 多态函数_Scala_Apache Spark_Polymorphism - Fatal编程技术网

Scala 多态函数

Scala 多态函数,scala,apache-spark,polymorphism,Scala,Apache Spark,Polymorphism,我有一个数据集[Edge],其中每条边都是从父实体到子实体的边。使用下面的函数,我想从parent\u id映射到它的子实体 子实体可以是不同的类型,例如(公司的)高级职员、(公司的)中间人、地址(公司的)、高级职员或中间人 def MAPPARENTIDTOENTY[T](边:数据集[Edge],实体:数据集[T])(隐式标记:类型标记[T])={ 边缘 .joinWith( 实体, 边(“子id”)===实体(“id”), “左” ) .地图( 案例(edge:edge,entity:T)=

我有一个
数据集[Edge]
,其中每条边都是从父实体到子实体的边。使用下面的函数,我想从
parent\u id
映射到它的子实体

子实体可以是不同的类型,例如(公司的)高级职员、(公司的)中间人、地址(公司的)、高级职员或中间人

def MAPPARENTIDTOENTY[T](边:数据集[Edge],实体:数据集[T])(隐式标记:类型标记[T])={
边缘
.joinWith(
实体,
边(“子id”)===实体(“id”),
“左”
)
.地图(
案例(edge:edge,entity:T)=>edge.parent\u id->entity
)
}
我的问题与类型擦除有关。我已经添加了
(隐式标记:TypeTag[T])
,以确保Spark能够对
T
进行编码,但仍然存在以下错误:

abstract type T in type pattern reflect.runtime.universe.TypeTag[T] (the underlying of tag.type) 
is unchecked since it is eliminated by erasure
          case (edge: Edge, entity: T) => edge.parent_id -> entity


有人能解释为什么这种情况仍然发生,以及我如何修复它吗?

下面的代码编译(使用隐式
Encoder[(Int,T)]
而不是
TypeTag[T]


你需要的是编码器而不是标签。
import org.apache.spark.sql.{Dataset, Encoder}

case class Edge(parent_id: Int)

def mapParentIDtoEntity[T](edges: Dataset[Edge], entities: Dataset[T](implicit 
  enc: Encoder[(Int, T)]
) = {
  edges
    .joinWith(
      entities,
      edges("child_id") === entities("id"),
      "left"
    )
    .map {
      case (edge: Edge, entity: T) => edge.parent_id -> entity
    }
}