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