1

Please see section "Programmatically Specifying the Schema". Java section.

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.

However, in my code, I need to call the Row.create method for a dynamic number of arguments.

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?

1
  • Some people have suggested that "three dots after the argument" will solve my problem. It wont. The method i am calling already accepts a dynamic number of arguments. The problem is in calling. I wont know how many arguments to pass to it at compile time. I will know it at runtime. The problem is not the function. Commented Oct 7, 2014 at 7:26

4 Answers 4

1

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);
      }
      });
Sign up to request clarification or add additional context in comments.

Comments

0

Try using elipsis in your implemented method as below.

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

Elipsis accept n number of arguments.

Comments

0

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!

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.