请参阅“Programmatically Specifying the Schema”部分。 Java部分。
该示例有效。但是我对这个特定的代码片段有疑问。
JavaRDD<Row> rowRDD = people.map(
new Function<String, Row>() {
public Row call(String record) throws Exception {
String[] fields = record.split(",");
return Row.create(fields[0], fields[1].trim());
}
正在使用在编译时确定的静态对象数调用Row create方法。
但是,在我的代码中,我需要为动态数量的参数调用Row.create
方法。
我只会知道运行时的字段数
例如,它可能是以下之一:
return Row.create(fields[0], fields[1].trim(), fields[2]);
要么
return Row.create(fields[0]);
要么
return Row.create(fields[0],fields[1].trim(), fields[2], fields[3],fields[4]);
我该怎么做?
这是你如何做到的。为我工作。
JavaRDD<Row> rowRDD = people.map(
new Function<String, Row>() {
public Row call(String record) throws Exception {
String[] fields = record.split(",");
//return Row.create(fields[0], fields[1].trim());
Object[] fields_converted = fields;
return Row.create(fields_converted);
}
});
尝试在实现的方法中使用省略号,如下所示。
public static void create(String ...arg) { ... }
省略号接受qazxsw poi的论点数量。
您可以通过在参数后面使用三个点指定一个方法来获取多个参数,例如:
n
替换为您想要的返回类型。请更改您的通话方法的签名,因为您没有为其指定退货类型!
这是我在同样情况下所做的
public static <return_type> create(String...args){
// Yoo can now use the String[] args
}
这里可能有龙。我的版本编译,看起来不错,尚未测试。