Introduction
DataFrame actions in Apache Spark using Java are operations that trigger the execution of the computation plan built through DataFrame transformations. Unlike transformations, which are lazy and only define the computation to be performed, actions instruct Spark to execute those computations. They either return results back to the driver program or write them to an external system like HDFS, S3, or databases.
Actions within Spark DataFrames typically involve reading data from external sources, collecting data to the driver program, writing data to external storage systems, or performing computations that return non-DataFrame results.
Below are some common DataFrame actions in Spark when using Java:
| Action | Description |
| ------------------------------ | ------------------------------------------------------------------------------------------------ |
| show() | Displays the first few rows of the DataFrame in a tabular format. |
| collectAsList() | Retrieves all rows from the DataFrame and brings them to the driver program as a List. |
| take(int n) | Retrieves the first N rows from the DataFrame and brings them to the driver program as an array. |
| count() | Returns the number of rows in the DataFrame. |
| first() | Returns the first row of the DataFrame. |
| head(int n) | Returns the first N rows of the DataFrame as an array. |
| foreach(Consumer<Row>) | Applies a function to each row of the DataFrame. |
| write().format(format) | Writes the DataFrame to external storage systems like HDFS, S3, or databases in a specific format.|
| write().save(path) | Saves the DataFrame to an external storage system at the desired path. |
| toJavaRDD() | Converts the DataFrame to a JavaRDD, which can then be further processed in Java. |
| toDF(String... columnNames) | Converts a list of tuples or RDD into a DataFrame with specified column names. |
These actions trigger the execution of the computation plan and return results or write data as specified. It is important to note that actions are usually the concluding operations in a Spark program, as they trigger the actual computation and materialization of results.
