Created
October 3, 2018 06:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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