星火API的基本子集需要隐ClassTags
(见Scala: What is a TypeTag and how do I use it?)和PairDStreamFunctions.mapWithState
是没有什么不同。检查class definition:
class PairDStreamFunctions[K, V](self: DStream[(K, V)])
(implicit kt: ClassTag[K], vt: ClassTag[V], ord: Ordering[K])
and:
def mapWithState[StateType: ClassTag, MappedType: ClassTag](
spec: StateSpec[K, V, StateType, MappedType]
): MapWithStateDStream[K, V, StateType, MappedType] = {
...
}
如果想创造出运行在一个通用的对流和使用mapWithState
你至少应该为KeyType
和ValueType
类型提供ClassTags
功能:
def foo[T : ClassTag, U : ClassTag](
stream: DStream[(T, U)], f: StateSpec[T, U, Int, Int]) = stream.mapWithState(f)
如果StateType
和MappedType
也是参数化的,您也需要ClassTags
:
def bar[T : ClassTag, U : ClassTag, V : ClassTag, W : ClassTag](
stream: DStream[(T, U)], f: StateSpec[T, U, V, W]) = stream.mapWithState(f)