Featured Article

Master Spark Basic Commands A Complete Guide

Kenneth Jul 13, 2026

Diving into the world of Apache Spark often starts with familiarizing oneself with its basic commands. Spark's interactive shell, `pyspark`, provides an intuitive interface for executing these commands. Let's explore some fundamental Spark operations through its Python API, PySpark.

an image of a computer screen with the text's name and numbers on it
an image of a computer screen with the text's name and numbers on it

Before we begin, ensure you've properly set up your Spark environment and have access to the `pyspark` shell. You can initiate it by running `pyspark` in your terminal. Now, let's delve into some essential Spark commands.

Typing speed   'Hack'
Typing speed 'Hack'

Loading Data into Spark

One of the primary operations in Spark is loading data. Spark supports various data formats like CSV, JSON, and Parquet. Here's how you can load a CSV file:

Scratch Programming Worksheet
Scratch Programming Worksheet

First, import the required SparkSession:

```python from pyspark.sql import SparkSession spark = SparkSession.builder.appName("LoadingData").getOrCreate() ```

Now, load your CSV file:

an image of the system command for windows and macosk, with instructions on how to use it
an image of the system command for windows and macosk, with instructions on how to use it

```python df = spark.read.csv("path/to/your/file.csv", header=True, inferSchema=True) ```

Understanding DataFrames

Spark uses DataFrames for structured data processing. Let's explore the DataFrame `df`:

```python print(df.show(5)) print(df.printSchema()) ```

This will display the first five rows of the DataFrame and its schema (column names and data types).

a handwritten diagram with words and phrases on the page, which are written in black ink
a handwritten diagram with words and phrases on the page, which are written in black ink

Reading JSON Files

Loading JSON files is as simple as:

```python df_json = spark.read.json("path/to/your/file.json") print(df_json.show(5)) ```

DataFrame Operations

20 GIT COMMANDS EVERY DEVELOPER SHOULD KNOW
20 GIT COMMANDS EVERY DEVELOPER SHOULD KNOW

Spark enables data manipulation using operations like filtering, selecting columns, and aggregating data.

Filtering Data

a table that has some different types of words on it, including the names and abbreviations
a table that has some different types of words on it, including the names and abbreviations
Git
Git
Comment créer des blocs avec Scratch 3
Comment créer des blocs avec Scratch 3
an image of some type of text on a white sheet with black and red writing
an image of some type of text on a white sheet with black and red writing
Flame Ignited! Elite Python, Java, JS Services: Spark Your Search – Order for Fiery Finishes!
Flame Ignited! Elite Python, Java, JS Services: Spark Your Search – Order for Fiery Finishes!
windows networking commands for the computer and it's operating system, with instructions on how to
windows networking commands for the computer and it's operating system, with instructions on how to
Check Point Quantum Spark 1900 Cheat Sheet 2025 - 30 Commands & Security Tips
Check Point Quantum Spark 1900 Cheat Sheet 2025 - 30 Commands & Security Tips
Ever wondered what powers the data-driven world we live in? 🌍
Ever wondered what powers the data-driven world we live in? 🌍
the css heat - sheet is shown in purple and white
the css heat - sheet is shown in purple and white

Filter data based on a condition:

```python filtered_data = df.filter(df.age > 30) ```

Selecting Columns

Pick specific columns for processing:

```python selected_columns = df.select("name", "age") ```

Aggregating Data

Use built-in functions like `groupBy`, `agg`, and `count`:

```python from pyspark.sql.functions import count agg_data = df.groupBy("category").agg(count("*").alias("total")) print(agg_data.show()) ```

Actions and Transformations

Spark operations fall into two categories: transformations and actions. Transformations create new DataFrames, while actions return values to the driver node or write output to an external storage system.

Transformations

Transformations like `select`, `filter`, and `groupBy` mentioned earlier. They allow chaining operations as follows:

```python result = df.filter(...).select(...).groupBy(...).agg(...) ```

Actions

Actions trigger a computation in Spark, like `show` or writing data to a storage system:

```python result.show() result.write.csv("output.csv") result.write.format("json").save("output.json") ```

With these basic commands, you're equipped to start your Spark journey. Keep practicing and exploring more complex operations, such as joins, window functions, and complex aggregations. Happy coding!