forked from spark-examples/pyspark-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelimiter.py
28 lines (20 loc) · 812 Bytes
/
Delimiter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# different delimiter in a file pyspark
from pyspark.sql import SparkSession
from pyspark.sql.functions import split, col
# Initialize Spark Session
#spark = SparkSession.builder.appName("mixedDelimiterExample").getOrCreate()
# Sample data
data = ["1,Alice\t30|New York"]
# Creating a DataFrame with a single column
df = spark.createDataFrame(data, "string")
# Custom logic to split the mixed delimiter row
split_col = split(df['value'], ',|\t|\|')
# Creating new columns for each split part
df = df.withColumn('id', split_col.getItem(0))\
.withColumn('name', split_col.getItem(1))\
.withColumn('age', split_col.getItem(2))\
.withColumn('city', split_col.getItem(3))
# Selecting and showing the result
df.select('id', 'name', 'age', 'city').show()
# Stop Spark Session
#spark.stop()