What is the best way to define custom methods on a DataFrame?

前端 未结 3 1507
感情败类
感情败类 2020-12-13 21:51

I need to define custom methods on DataFrame. What is the better way to do it? The solution should be scalable, as I intend to define a significant number of custom methods

3条回答
  •  -上瘾入骨i
    2020-12-13 22:23

    I think you should add an implicit conversion between DataFrame and your custom wrapper, but use an implicit clas - this should be the easiest to use and you will store your custom methods in one common place.

       implicit class WrappedDataFrame(val df: DataFrame) {
            def customMethod(String arg1, int arg2) {
               ...[do your stuff here]
            }
         ...[other methods you consider useful, getters, setters, whatever]...
          }
    

    If the implicit wrapper is in DataFrame's scope, you can just use normal DataFrame as if it was your wrapper, ie.:

    df.customMethod("test", 100)

提交回复
热议问题