我尝试使用Dataset的groupByKey方法。我找不到问题所在,也找不到使用groupByKey的任何工作示例。
因此,让我指出我在解决方案中寻找的要点:
下面是我所做的:
//Inner class
public static class Bean implements Serializable {
private static final long serialVersionUID = 1L;
private String k;
private int something;
public Bean(String name, int value) {
k = name;
something = value;
}
public String getK() {return k;}
public int getSomething() {return something;}
public void setK(String k) {this.k = k;}
public void setSomething(int something) {this.something = something;}
}
//usage
List<Bean> debugData = new ArrayList<Bean>();
debugData.add(new Bean("Arnold", 18));
debugData.add(new Bean("Bob", 7));
debugData.add(new Bean("Bob", 13));
debugData.add(new Bean("Bob", 15));
debugData.add(new Bean("Alice", 27));
Dataset<Row> df = sqlContext.createDataFrame(debugData, Bean.class);
df.groupByKey(row -> {new Bean(row.getString(0), row.getInt(1));}, Encoders.bean(Bean.class)); //doesn't compile
我得到的错误是:
无法解析不明确的方法调用-集成开发环境显示有关无法显示和无法解析matching.
MapFunction的警告:我无法显示/打印的结果
发布于 2017-12-12 00:24:53
使用Java 8 lambda
df.groupByKey(row -> {
return new Bean(row.getString(0), row.getInt(1));
}, Encoders.bean(Bean.class));
使用MapFunction
df.groupByKey(new MapFunction<Row, Bean>() {
@Override
public Bean call(Row row) throws Exception {
return new Bean(row.getString(0), row.getInt(1));
}
}, Encoders.bean(Bean.class));
发布于 2018-09-29 13:31:29
出现此错误的原因是groupByKey
有两个覆盖的实现。其中一个方法将MapFunction
作为第一个参数,第二个方法将Function1
作为参数。您的lambda代码可以转换为这两种类型。所以你应该明确声明哪一个是你的意图。造型是一个简单的解决方案:
df.groupByKey(row -> (MapFunction<Row, Bean>) new Bean(row.getString(0), row.getInt(1))
, Encoders.bean(Bean.class));
https://stackoverflow.com/questions/47756167
复制相似问题