あなたの方法は行く方法です([1]を参照)。私はそれが少し異なる解決していても、アプローチは同様のまま:
可能性1つの
暗黙
object ExtraDataFrameOperations {
object implicits {
implicit def dFWithExtraOperations(df: DataFrame) = DFWithExtraOperations(df)
}
}
case class DFWithExtraOperations(df: DataFrame) {
def customMethod(param: String) : DataFrame = {
// do something fancy with the df
// or delegate to some implementation
//
// here, just as an illustrating example: do a select
df.select(df(param))
}
}
使用
DataFrame
上の新しいcustomMethod
メソッドを使用するには、次の
を
import ExtraDataFrameOperations.implicits._
val df = ...
val otherDF = df.customMethod("hello")
可能性2
代わりのimplicit method
(上記参照)、あなたもimplicit class
を使用することができます使用して:あなたはケースで
暗黙のクラス
object ExtraDataFrameOperations {
implicit class DFWithExtraOperations(df : DataFrame) {
def customMethod(param: String) : DataFrame = {
// do something fancy with the df
// or delegate to some implementation
//
// here, just as an illustrating example: do a select
df.select(df(param))
}
}
}
使用
import ExtraDataFrameOperations._
val df = ...
val otherDF = df.customMethod("hello")
備考
を追加しないようにしたいimport
、object
ExtraDataFrameOperations
をpackage object
に変更し、パッケージ内のpackage.scala
というファイルに保存します。
公式ドキュメント/参照
[1] M. Odersky著によるオリジナルのブログ "ポン引き私のライブラリは" http://www.artima.com/weblogs/viewpost.jsp?thread=179766