简体   繁体   English

Spark Java:将可变数量的参数传递给函数

[英]Spark Java: Passing variable number of arguments to a function

Please see section " Programmatically Specifying the Schema ". 请参见“以编程方式指定架构 ”部分。 Java section. Java部分。

The example works. 该示例有效。 But I have a question about this particular code snippet. 但是我对此特定代码段有疑问。

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());
}

The Row create method is being called with a static number of objects determined at compile time. 正在使用在编译时确定的静态对象数调用Row创建方法。

However, in my code, I need to call the Row.create method for a dynamic number of arguments. 但是,在我的代码中,我需要为动态数量的参数调用Row.create方法。

I will only know the number of fields at run time 我只会知道运行时的字段数

For example, it may be one of: 例如,它可能是以下之一:

return Row.create(fields[0], fields[1].trim(), fields[2]);

or 要么

return Row.create(fields[0]);

or 要么

return Row.create(fields[0],fields[1].trim(), fields[2], fields[3],fields[4]);

How do I do it? 我该怎么做?

Here is how you can do it. 这是您的操作方法。 Worked for me. 为我工作。

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);
      }
      });

Try using elipsis in your implemented method as below. 尝试在实现的方法中使用省略号,如下所示。

public static void create(String ...arg) { ... }

Elipsis accept n number of arguments. 省略号接受n个参数。

You can specify a method to take multiple arguments by using three dots after the argument, for example: 您可以通过在参数后使用三个点来指定采用多个参数的方法,例如:

public static <return_type> create(String...args){
    // Yoo can now use the String[] args
}

Replace 更换 with your desired return type. 与您所需的返回类型。 Please change the signature of your call method as you have not specified a return type for it! 由于您尚未为其指定返回类型,因此请更改调用方法的签名!

Here is what I did in the same situation 这是我在相同情况下所做的

new Function<String, Row>(String s) {
    public Row call(String s){
        int n = /* width of actual schema */
        Object rec[] = new Object[n];
        for( int i = 0; i < n; ++i )
            rec[i] = /* Something that aligns with the type of #i field */
        return Row.create( rec );
    }
}

There might be dragons here. 这里可能有龙。 My version compiles, looks good, not tested yet. 我的版本可以编译,看起来不错,尚未测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM