How to use analytic / window functions in Spark Java?

I am trying to use the analytic / window function last_value in Spark Java.

Netezza request:

select sno, name, addr1, addr2, run_dt, 
last_value(addr1 ignore nulls) over (partition by sno, name, addr1, addr2, run_dt order by beg_ts , end_ts rows between unbounded preceding and unbounded following  ) as last_addr1
from daily

We want to implement this n Spark Java query (without using the HiveSQLContext):

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.expressions.Window;
import org.apache.spark.sql.expressions.WindowSpec;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.sql.DataFrame;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.sql.execution.WindowFunctionFrame;

    SparkConf conf = new SparkConf().setMaster("local").setAppName("Agg");
    JavaSparkContext sc = new JavaSparkContext(conf);
    SQLContext sqlContext = new SQLContext(sc);


    JavaRDD<Stgdailydtl> daily = sc.textFile("C:\\Testing.txt").map(
              new Function<String, Stgdailydtl>() {
                  private static final long serialVersionUID = 1L;
                public Stgdailydtl call(String line) throws Exception {
                  String[] parts = line.split(",");

                  Stgdailydtl daily = new Stgdailydtl();
                  daily.setSno(Integer.parseInt(parts[0].trim()));
                  .....

                  return daily;
                }
              });
DataFrame schemaDailydtl = sqlContext.createDataFrame(daily, Stgdailydtl.class);
schemaDailydtl.registerTempTable("daily");
WindowSpec ws = Window.partitionBy("sno, name, addr1, addr2, run_dt").orderBy("beg_ts , end_ts").rowsBetween(0, 100000);
DataFrame df = sqlContext.sql("select sno, name, addr1, addr2, run_dt "
            + "row_number() over(partition by mach_id, msrmt_gbl_id, msrmt_dsc, elmt_dsc, end_cptr_dt order by beg_cptr_ts, end_cptr_ts) from daily ");

}

}

Mistake:

Exception in thread "main" java.lang.RuntimeException: [1.110] failure: ``union'' expected but `(' found

select stg.mach_id, stg.msrmt_gbl_id, stg.msrmt_dsc, stg.elmt_dsc, stg.elmt_dsc_grp_concat, row_number() over(partition by mach_id, msrmt_gbl_id, msrmt_dsc, elmt_dsc, end_cptr_dt order by beg_cptr_ts, end_cptr_ts) from stgdailydtl stg 
                                                                                                             ^
    at scala.sys.package$.error(package.scala:27)

I could not figure out how to use the WindowSpec / Window object. Please suggest about it. Thank you for your help.

+4
source share
1 answer

You mix dataframe syntax and sql syntax - in particular, you created WindowSpec but then did not use it.

Import org.apache.spark.sql.functionsto get the function row_number, then create the column you are trying to select:

Column rowNum = functions.row_number().over(ws)

Then select it using the dataframe API:

df.select(each, column, you, want, rowNum)

, scala python, - .

+2

Source: https://habr.com/ru/post/1613026/


All Articles