本文介绍了如何使用Java UDF向Spark数据框添加新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Dataset<Row> inputDS,其中有4列,即Id, List<long> time, List<String> value, aggregateType,我想使用map函数在Dataset value_new中再添加一列,该map函数需要timevalueaggregateType将其传递给函数getAggregate(String aggregateType, List<long> time, List<String> value),并在处理参数时返回一个双精度值.方法getAggregate返回的Double值将是新的列值,即value_new

I have a Dataset<Row> inputDS which has 4 columns namely Id, List<long> time, List<String> value, aggregateType I want to add one more column to the Dataset value_new using map function, that map function takes columns time , value and aggregateType passes that to a function getAggregate(String aggregateType, List<long> time, List<String> value) and return a double value on processing the parameters. The Double value returned by the method getAggregate will be the new column value i.e value of value_new

数据集输入DS

 +------+---+-----------+---------------------------------------------+---------------+
 |    Id| value         |     time                                   |aggregateType  |
 +------+---------------+---------------------------------------------+---------------+
 |0001  |  [1.5,3.4,4.5]| [1551502200000,1551502200000,1551502200000] | Sum           |
 +------+---------------+---------------------------------------------+---------------+

预期的数据集输出DS

 +------+---------------+---------------------------------------------+---------------+-----------+
 |    Id| value         |     time                                    |aggregateType  | value_new |
 +------+---------------+---------------------------------------------+---------------+-----------+
 |0001  |  [1.5,3.4,4.5]| [1551502200000,1551502200000,1551502200000] | Sum           |   9.4     |
 +------+---------------+---------------------------------------------+---------------+-----------+

我尝试过的代码.

 inputDS.withColumn("value_new",functions.lit(inputDS.map(new MapFunction<Row,Double>(){

 public double call(Row row){
 String aggregateType = row.getAS("aggregateType");
 List<long> timeList = row.getList("time");
 List<long> valueList= row.getList("value");

 return  getAggregate(aggregateType ,timeList,valueList);

 }}),Encoders.DOUBLE())));

错误

 Unsupported literal type class org.apache.spark.sql.Dataset [value:double]

注意:很抱歉,如果我错误地使用了map函数,请建议我是否有任何解决方法.

Note Sorry if I used map function wrongly and please suggest me if there is any workaround.

谢谢.!

推荐答案

出现错误是因为您尝试使用Dataset.map()的结果创建函数文字(lit()),您可以在文档中看到的是数据集.您可以在Dataset.withColumn()的API中看到,您需要一个作为列的参数.

You get the error because you are trying to create a function literal (lit()) using the result of Dataset.map(), which you can see in docs is a Dataset. You can see in the API for Dataset.withColumn() that you need a argument that is a column.

似乎您需要创建一个用户定义的函数.看看如何调用使用JAVA在Spark DataFrame上创建UDF?

It seems like you need to create a user-defined function. Take a look at How do I call a UDF on a Spark DataFrame using JAVA?

这篇关于如何使用Java UDF向Spark数据框添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:34