Skip to content

Instantly share code, notes, and snippets.

@YordanGeorgiev
Created October 3, 2018 06:16
Show Gist options
  • Save YordanGeorgiev/db864692822fdd4c3dd7cba93d44fc33 to your computer and use it in GitHub Desktop.
Save YordanGeorgiev/db864692822fdd4c3dd7cba93d44fc33 to your computer and use it in GitHub Desktop.
[how-to use implicit class] how-to use implicit classes #scala #implicits #implicit #Row #scope
package com.corp.dept.spark.dfutils
import org.apache.spark.sql.Row
object RowEnhancements {
implicit class RowExtender(row: Row) {
def isNullAtCol(cellName: String): Boolean = {
val index: Int = row.fieldIndex(cellName)
if (row.isNullAt(index)) {
return true
}
false
}
}
}
// and use it like this
package com.corp.dept.biz.pckg
import com.corp.dept.spark.dfutils.RowEnhancements._ // obs !!! otherwise no joy !!!
class FooBar {
// just an example method using the implicit class , hence the logic
def firstRowColumnIsNull(df:DataFrame,col:String) {
val row: Row = df.first()
return row.isNullAtCol(col) // Action !!! <- here the method of the implicit class is used
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment