0

I have a problem with the WINDOW FUNCTION spark API :

my question is similar to this one : How to drop duplicates using conditions

I have a dataset :

+---+----------+---------+
| ID|    VALUEE|    OTHER|
+---+----------+---------+
|  1|      null|something|
|  1|[1.0, 0.0]|something|
|  1|[1.0, 0.0]|something|
|  1|[0.0, 2.0]|something|
|  1|[3.0, 5.0]|something|
|  2|[3.0, 5.0]|something|
|  1|[3.0, 5.0]|something|
|  2|      null|something|
|  3|[3.0, 5.0]|something|
|  4|      null|something|
+---+----------+---------+

I want a keep only one ID of each ( no duplicate ) and I don't care of the VALUEE but I prefer a non NULL value

expected result

+---+----------+---------+
| ID|    VALUEE|    OTHER|
+---+----------+---------+
|  1|[0.0, 2.0]|something|
|  3|[3.0, 5.0]|something|
|  4|      null|something|
|  2|[3.0, 5.0]|something|
+---+----------+---------+

windowsFunction with the Aggregate function first() do not work whereas with row_number() it work

but i don't understand why first do not work

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.sql.*;
import org.apache.spark.sql.expressions.Window;
import org.apache.spark.sql.expressions.WindowSpec;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
import org.spark_project.guava.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import static org.apache.spark.sql.types.DataTypes.IntegerType;
import static org.apache.spark.sql.types.DataTypes.StringType;
import static org.apache.spark.sql.types.DataTypes.createStructField;

public class TestSOF {

    public static void main(String[] args) {

        StructType schema = new StructType(
                new StructField[]{
                        createStructField("ID", IntegerType, false),
                        createStructField("VALUEE", DataTypes.createArrayType(DataTypes.DoubleType), true),
                        createStructField("OTHER", StringType, true),
                });

        double [] a =new double[]{1.0,0.0};
        double [] b =new double[]{3.0,5.0};
        double [] c =new double[]{0.0,2.0};
        List<Row> listOfdata = new ArrayList();
        listOfdata.add(RowFactory.create(1,null,"something"));
        listOfdata.add(RowFactory.create(1,a,"something"));
        listOfdata.add(RowFactory.create(1,a,"something"));
        listOfdata.add(RowFactory.create(1,c,"something"));
        listOfdata.add(RowFactory.create(1,b,"something"));
        listOfdata.add(RowFactory.create(2,b,"something"));
        listOfdata.add(RowFactory.create(1,b,"something"));
        listOfdata.add(RowFactory.create(2,null,"something"));
        listOfdata.add(RowFactory.create(3,b,"something"));
        listOfdata.add(RowFactory.create(4,null,"something"));
        List<Row> rowList  = ImmutableList.copyOf(listOfdata);
        SparkSession sparkSession = new SparkSession.Builder().config("spark.master", "local[*]").getOrCreate();
        sparkSession.sparkContext().setLogLevel("ERROR");
        Dataset<Row> dataset = sparkSession.createDataFrame(rowList,schema);
        dataset.show();


        WindowSpec windowSpec = Window.partitionBy(dataset.col("ID")).orderBy(dataset.col("VALUEE").asc_nulls_last());

        // wind solution
        // lost information
        Dataset<Row> dataset0 = dataset.groupBy("ID").agg(functions.first(dataset.col("VALUEE"), true));

        Dataset<Row> dataset1 = dataset.withColumn("new",functions.row_number().over(windowSpec)).where("new = 1").drop("new");

        //do not work
        Dataset<Row> dataset2 = dataset.withColumn("new",functions.first("VALUEE",true).over(windowSpec)).drop("new");

        JavaRDD<Row> rdd =
                dataset.toJavaRDD()
                .groupBy(row -> row.getAs("ID"))
                .map(g -> {
                    Iterator<Row> iter =g._2.iterator();
                    Row rst = null;
                    Row tmp;
                    while(iter.hasNext()){
                        tmp = iter.next();
                        if (tmp.getAs("VALUEE") != null) {
                                rst=tmp;
                                break;
                        }
                        if(rst==null){
                            rst=tmp;
                        }
                    }
                    return rst;
                });

        Dataset<Row> dataset3 = sparkSession.createDataFrame(rdd, schema);

        dataset0.show();
        dataset1.show();
        dataset2.show();
        dataset3.show();
    }

}
raphaelauv
  • 670
  • 1
  • 11
  • 22

3 Answers3

1

First is not a Window function in SPARK 2.3 it's only an Aggregate function

firstValue is not present in the dataframe API

raphaelauv
  • 670
  • 1
  • 11
  • 22
0

For the example data you've provided, the short version of the solution dataset1, that you provided:

dataset.groupBy("ID").agg(functions.first(dataset.col("VALUEE"), true)).show();

For understanding of Window Functions and optimization of performance of WindowFunction vs groupBy in Spark i strongly recommend presentations by Jacek Laskowski:

  1. https://databricks.com/session/from-basic-to-advanced-aggregate-operators-in-apache-spark-sql-2-2-by-examples-and-their-catalyst-optimizations
  2. https://databricks.com/session/from-basic-to-advanced-aggregate-operators-in-apache-spark-sql-2-2-by-examples-and-their-catalyst-optimizations-continues
raphaelauv
  • 670
  • 1
  • 11
  • 22
wind
  • 892
  • 1
  • 11
  • 27
  • I don't want lose the others columns of the dataset , also what do you think of my rdd solution ( see datatset3) , again thank you – raphaelauv Aug 03 '18 at 09:33
0

You can use an equivalent solution as the one you posted. In your case, the null values will appear in the first order. So :

val df: DataFrame = ???

import df.sparkSession.implicits._
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.functions.{col, last}

val id_cols = "ID"
val windowSpec = Window.partitionBy(id_cols).orderBy($"VALUEE".asc)
val list_cols = Seq("VALUE", "OTHER")

val df_dd = df.select(col(id_cols) +: list_cols.map(x => last(col(x)).over(windowSpec).alias(x)):_*).distinct
raphaelauv
  • 670
  • 1
  • 11
  • 22
Sophie D.
  • 361
  • 3
  • 6