Here's a PySpark program that calculates the circle's area based on user input:
from pyspark.sql import SparkSession
from pyspark.sql.types import *
from pyspark.sql.functions import *
import math
# Initialize Spark Session
spark = SparkSession.builder \
.appName("Circle Area Calculator") \
.getOrCreate()
def calculate_circle_area():
try:
# Get radius input from user
radius = float(input("Enter the radius of the circle: "))
# Create a DataFrame with the radius
data = [(radius,)]
schema = StructType([StructField("radius", DoubleType(), True)])
df = spark.createDataFrame(data, schema)
# Calculate area using PySpark SQL functions
# Area = π * r²
df_with_area = df.withColumn("area",
pi() * col("radius") * col("radius"))
# Display results
print(f"r = {radius}")
print(f"Area = {df_with_area.select('area').collect()[0][0]}")
except ValueError:
print("Please enter a valid number for the radius")
finally:
# Stop Spark Session
spark.stop()
if __name__ == "__main__":
calculate_circle_area()
Let's break down how this program works:
-
First, we import necessary PySpark libraries and math module.
-
We create a SparkSession, which is the entry point for PySpark functionality.
-
The calculate_circle_area() function:
- Takes user input for radius
- Creates a DataFrame with the radius value
- Uses PySpark SQL functions to calculate the area
- Displays the results
-
We use error handling to catch invalid inputs.
Alternative simpler version using just PySpark RDD:
from pyspark.sql import SparkSession
import math
# Initialize Spark Session
spark = SparkSession.builder \
.appName("Circle Area Calculator") \
.getOrCreate()
def calculate_circle_area():
try:
# Get radius input from user
radius = float(input("Enter the radius of the circle: "))
# Create RDD with radius
radius_rdd = spark.sparkContext.parallelize([radius])
# Calculate area using map transformation
area_rdd = radius_rdd.map(lambda r: math.pi * r * r)
# Get the result
area = area_rdd.collect()[0]
# Display results
print(f"r = {radius}")
print(f"Area = {area}")
except ValueError:
print("Please enter a valid number for the radius")
finally:
spark.stop()
if __name__ == "__main__":
calculate_circle_area()
Sample execution:
Enter the radius of the circle: 1.1
r = 1.1
Area = 3.8013271108436504
Key points about this program:
-
It uses PySpark's distributed computing capabilities (though for this simple calculation it's overkill - a regular Python program would be more appropriate for real-world use).
-
The first version uses DataFrame API which is more suitable for structured data processing.
-
The second version uses RDD API which is more basic but simpler for this particular case.
-
Both versions include proper error handling for invalid inputs.
-
Both properly manage the SparkSession by stopping it when done.
For such a simple calculation, you could use a much simpler regular Python program:
import math
def calculate_circle_area():
try:
radius = float(input("Enter the radius of the circle: "))
area = math.pi * radius * radius
print(f"r = {radius}")
print(f"Area = {area}")
except ValueError:
print("Please enter a valid number for the radius")
if __name__ == "__main__":
calculate_circle_area()
The PySpark version would be more appropriate when dealing with large-scale data processing where you need to calculate areas for millions of circles in parallel, or when this calculation is part of a larger data processing pipeline.