I am new to PySpark. My goal is to translate this table:
|Key|Value1|Value2|Value3| ...
+---+------+------+------+
| 1| v1| v2| v3|
into a table like this:
|Key|ColumnName|ColumnValue|
+---+----------+-----------+
| 1| Value1| v1|
| 1| Value2| v2|
| 1| Value3| v3|
And here is my code for doing so:
from pyspark.sql import *
sample = spark.read.format("csv").options(header='true', delimiter = ',').load("/FileStore/tables/sample.csv")
class Closure:
def __init__(self, columnNames):
self.columnNames = columnNames
def flatMapFunction(self, columnValues):
result = []
columnIndex = 0
for columnValue in columnValues:
if not columnIndex == 0:
result.append(Row(Key = columnValues[0], ColumnName = self.columnNames[columnIndex], ColumnValue = columnValue))
columnIndex = columnIndex + 1
return result
closure = Closure(sample.columns)
sample.rdd.flatMap(closure.flatMapFunction).toDF().show()
I know it is weird that I have to explicitly created the closure, but it looks like it is required, without which the execution would fail with a weird exception.
Questions:
- Is it good code?
- Is it going to scale well? There will be a few hundred columns but millions of rows.
- Is there anything I should do to improve the code?