2

I am using apache spark. I want to retrieve the values pf DataFrame in a String type array. I have created a table using DataFrame.

dataframe.registerTempTable("table_name");
DataFrame d2=sqlContext.sql("Select * from table_name");

Now I want this data to be retrieved in a java Array(String type would be fine). How can I do that.

1 Answer 1

4

You can use collect() method to get Row[]. Each Row contains column values of your Dataframe.If there is single value in each row then you can add them in ArrayList of String. If there are more than one column in each row then use ArrayList of your custom object type and set the properties. In below code instead of printing "Row Data" you can add them in ArrayList.

    Row[] dataRows = d2.collect();
    for (Row row : dataRows) {
        System.out.println("Row : "+row);
         for (int i = 0; i < row.length(); i++) {
                System.out.println("Row Data : "+row.get(i));
         }
    }
Sign up to request clarification or add additional context in comments.

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.