我必须根据用户输入应用过滤条件。假定条件如下,“ mod”是用户过滤条件字符串输入。
df.filter(df("rollnumber").mod(2))
如何编写一个将参数作为filtercolumnname,filtercondition,filtervalue的函数并将其应用于数据框?
期望:
def applyfiltering(filtercolumnname: String,filtercondition : String,filtervalue : String) ={
df.filter(df("rollnumber").mod(2))
}
applyfiltering("rollnumber","mod","2")
预先感谢
我只是这样尝试:
df.filter(df("rollnumber").cast("===")(2)).toDF
df.filter(df("rollnumber").cast("mod")(2)).toDF
答案 0 :(得分:0)
您可以执行类似已知操作的要求:
val cond1 = df1("rollnumber").mod(lit(2))
val cond2 = df1("rollnumber") === lit(2)
def applyFiltering(column: Column): Unit = {
df1.filter(column)
}
applyFiltering(cond2).show
另一种方法是:
def applyFiltering(colName: String, filtercondition: String, filtervalue: Any): DataFrame = {
val dfCol = df1(colName)
val fltValue = lit(filtervalue)
val flt: Column =
if (filtercondition == "===") dfCol === fltValue
else if (filtercondition == "mod") dfCol.mod(fltValue)
else throw new UnsupportedOperationException()
df1.filter(flt)
}
applyFiltering("rollnumber", "===", 2)