GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing spark.apache.org — Examples | Apache Spark ExamplesThis page shows you how to use different Apache Spark APIs with simple examples. Spark is a great engine for small and large datasets. It can be used with single-node/localhost environments, or distributed clusters. Spark’s expansive API, excellent performance, and flexibility make it a good option for many analyses. This guide shows examples with the following Spark APIs:
The examples use small datasets so the they are easy to follow. Spark DataFrame exampleThis section shows you how to create a Spark DataFrame and run simple operations. The examples are on a small DataFrame, so you can easily see the functionality. Let’s start by creating a Spark Session: Some Spark runtime environments come with pre-instantiated Spark Sessions. The Create a Spark DataFrame Start by creating a DataFrame with Use the Now, let’s perform some data processing operations on the DataFrame. Add a column to a Spark DataFrame Let’s add a It’s easy to add columns to a Spark DataFrame. Let’s view the contents of Notice how the original DataFrame is unchanged: Spark operations don’t mutate the DataFrame. You must assign the result to a new variable to access the DataFrame changes for subsequent operations. Filter a Spark DataFrame Now, filter the DataFrame so it only includes teenagers and adults. Group by aggregation on Spark DataFrame Now, let’s compute the average age for everyone in the dataset: You can also compute the average age for each Spark lets you run queries on DataFrames with SQL if you don’t want to use the programmatic APIs. Query the DataFrame with SQL Here’s how you can compute the average age for everyone with SQL: And here’s how to compute the average age by Spark lets you use the programmatic API, the SQL API, or a combination of both. This flexibility makes Spark accessible to a variety of users and powerfully expressive. Spark SQL ExampleLet’s persist the DataFrame in a named Parquet table that is easily accessible via the SQL API. Make sure that the table is accessible via the table name: Now, let’s use SQL to insert a few more rows of data into the table: Inspect the table contents to confirm the row was inserted: Run a query that returns the teenagers: Spark makes it easy to register tables and query them with pure SQL. Spark Structured Streaming ExampleSpark also has Structured Streaming APIs that allow you to create batch or real-time streaming applications. Let’s see how to use Spark Structured Streaming to read data from Kafka and write it to a Parquet table hourly. Suppose you have a Kafka stream that’s continuously populated with the following data: Here’s how to read the Kafka source into a Spark DataFrame: Create a function that cleans the input data. Now, create a function that will read all of the new data in Kafka whenever it’s run. Invoke the You can set up a cron job to run the Spark RDD ExampleThe Spark RDD APIs are suitable for unstructured data. The Spark DataFrame API is easier and more performant for structured data. Suppose you have a text file called You would like to compute the count of each word in the text file. Here is how to perform this computation with Spark RDDs: Let’s take a look at the result: Spark allows for efficient execution of the query because it parallelizes this computation. Many other query engines aren’t capable of parallelizing computations. ConclusionThese examples have shown how Spark provides nice user APIs for computations on small datasets. Spark can scale these same code examples to large datasets on distributed clusters. It’s fantastic how Spark can handle both large and small datasets. Spark also has an expansive API compared with other query engines. Spark allows you to perform DataFrame operations with programmatic APIs, write SQL, perform streaming analyses, and do machine learning. Spark saves you from learning multiple frameworks and patching together various libraries to perform an analysis. Additional examplesMany additional examples are distributed with Spark:
Latest News
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |