Spark 2.x introduced a few key schemes for structuring Spark. One is to express com‐ putations by using common patterns found in data analysis. These patterns are expressed as high-level operations such as filtering, selecting, counting, aggregating, averaging, and grouping. This provides added clarity and simplicity.
Awll 7aje chnowa el 7aje eli zedaha spark fe 2x ?
—> zeded nw3an ame chnowa nsmoh “structuring spark”
Which leverage high level operation kema “filtering , selecting , counting , aggegatiog , averaging and grouping “ okii
A4hom patterns coming from the world of sql world okij
5law el clarity and simplicity more in spark okii
Q- give me 3 qcm about this
Q- now can you give me syntatic data that can be used here and and apply the
filtering, selecting, counting, aggregating, averaging, and grouping. This provides added clarity and simplicity.
Q- operation in it oki
Assistant
Sure! Let's break down your request into three parts:
Three Multiple-Choice Questions (QCM) about Spark 2.x's High-Level Operations
Synthetic Data Example Applying Various Spark Operations
Explanation of Operations in Spark 2.x
1. Multiple-Choice Questions (QCM) about Spark 2.x's High-Level Operations
Q1: Which of the following high-level operations introduced in Spark 2.x enhance clarity and simplicity in data processing?
A) Sorting, Merging, Indexing
B) Filtering, Selecting, Aggregating
C) Parsing, Tokenizing, Encoding
D) Compressing, Encrypting, Decompressing
Correct Answer: B) Filtering, Selecting, Aggregating
Q2: How do high-level operations in Spark 2.x contribute to structuring computations?
A) By requiring detailed low-level code
B) By using common SQL-like patterns for data analysis
C) By eliminating the need for any data transformations
D) By focusing solely on machine learning tasks
Correct Answer: B) By using common SQL-like patterns for data analysis
Q3: Which of the following is NOT a high-level operation commonly used in Spark 2.x?
A) Filtering
B) Selecting
C) Rendering
D) Grouping
Correct Answer: C) Rendering
2. Synthetic Data Example Applying Spark Operations
Let's create a synthetic dataset of employees and apply various Spark high-level operations such as filtering, selecting, counting, aggregating, averaging, and grouping.
Sample Data:
EmployeeID
Name
Department
Salary
YearsAtCompany
1
Alice
Sales
70,000
5
2
Bob
Engineering
90,000
3
3
Charlie
Sales
65,000
4
4
David
HR
60,000
2
5
Eva
Engineering
95,000
6
6
Frank
Sales
72,000
5
7
Grace
HR
58,000
1
Applying Operations Using PySpark:
python
from pyspark.sql import SparkSession
from pyspark.sql.functions import avg, count, sum as _sum
# Initialize Spark session
spark = SparkSession.builder.appName("SparkOperationsExample").getOrCreate()
# Create DataFrame
data = [
(1, "Alice", "Sales", 70000, 5),
(2, "Bob", "Engineering", 90000, 3),
(3, "Charlie", "Sales", 65000, 4),
(4, "David", "HR", 60000, 2),
(5, "Eva", "Engineering", 95000, 6),
(6, "Frank", "Sales", 72000, 5),
(7, "Grace", "HR", 58000, 1)
]
columns = ["EmployeeID", "Name", "Department", "Salary", "YearsAtCompany"]
df = spark.createDataFrame(data, schema=columns)
# 1. Filtering: Select employees with Salary > 70,000
filtered_df = df.filter(df.Salary > 70000)
print("Employees with Salary > 70,000:")
filtered_df.show()
# 2. Selecting: Select Name and Department columns
selected_df = df.select("Name", "Department")
print("Selected Columns (Name and Department):")
selected_df.show()
# 3. Counting: Count the number of employees in each Department
count_df = df.groupBy("Department").count()
print("Number of Employees per Department:")
count_df.show()
# 4. Aggregating: Calculate total salary per Department
aggregate_df = df.groupBy("Department").agg(_sum("Salary").alias("TotalSalary"))
print("Total Salary per Department:")
aggregate_df.show()
# 5. Averaging: Calculate average YearsAtCompany
average_df = df.agg(avg("YearsAtCompany").alias("AverageYears"))
print("Average Years at Company:")
average_df.show()
# 6. Grouping: Calculate average salary per Department
grouped_df = df.groupBy("Department").agg(avg("Salary").alias("AverageSalary"))
print("Average Salary per Department:")
grouped_df.show()
# Stop the Spark session
spark.stop()
Output Explanation:
Filtering: Displays employees earning more than $70,000.
Selecting: Shows only the Name and Department columns.
Counting: Counts the number of employees in each department.
Aggregating: Calculates the total salary expenditure per department.
Averaging: Computes the average number of years employees have been at the company.
Grouping: Determines the average salary within each department.
Spark 2.x introduced a structured API that allows developers to perform data analysis using high-level operations. These operations abstract the complexity of low-level transformations, making data processing more intuitive and maintainable. Here's a breakdown of the key operations:
Filtering (filter):
Selects rows that meet a specified condition.
Example:df.filter(df.Salary > 70000) selects employees with salaries over $70,000.
Selecting (select):
Chooses specific columns from the DataFrame.
Example:df.select("Name", "Department") retrieves only the Name and Department columns.
Counting (count):
Counts the number of records, optionally grouped by a certain column.
Example:df.groupBy("Department").count() counts employees per department.
Aggregating (agg):
Performs aggregate functions like sum, min, max on grouped data.
Example:df.groupBy("Department").agg(_sum("Salary").alias("TotalSalary")) sums salaries per department.
Averaging (avg):
Calculates the average of a numerical column.
Example:df.agg(avg("YearsAtCompany").alias("AverageYears")) computes average tenure.
Grouping (groupBy):
Groups data based on one or more columns to perform aggregate operations.
Example:df.groupBy("Department").agg(avg("Salary").alias("AverageSalary")) averages salaries by department.
Benefits of High-Level Operations in Spark 2.x:
Clarity: Code is more readable and closely resembles SQL queries, making it easier to understand.
Simplicity: Reduces the need for complex transformation logic, allowing developers to focus on the analysis.
Performance Optimization: Spark optimizes the execution plan behind the scenes, improving performance without additional effort from the developer.
Maintainability: High-level abstractions make the codebase cleaner and easier to maintain or extend.
By leveraging these high-level operations, Spark 2.x provides a powerful and user-friendly framework for data analysis, enabling both simplicity and efficiency in processing large datasets.