如何将给定的Scala Spark代码转换为PySpark代码?
Hey there! Converting your Scala Spark code to PySpark is straightforward once you map the key constructs over. Here's the equivalent code, plus context to help you understand how it translates:
from pyspark.sql import Row def build_custom_row(x): # Initialize a list with 95 elements (using None to match Scala's default null for Strings) column_list = [None] * 95 # Assign substring values to the first three positions column_list[0] = x[:10] # Matches Scala's x.substring(0,10) column_list[1] = x[11:14] # Matches Scala's x.substring(11,14) column_list[2] = x[15:17] # Matches Scala's x.substring(15,17) # Convert the list to a Row object (same as Scala's Row.fromSeq) return Row.fromSeq(column_list)
Quick breakdown of the translation:
- String slicing: Python's
[start:end]syntax behaves exactly like Scala'ssubstring(start, end)here—both exclude the end index, so the values align perfectly. - List vs Array: Python's list is the closest equivalent to Scala's Array for this use case; we initialize it with 95 elements to match your original code's array size.
- Row creation:
Row.fromSeq()works identically in PySpark as it does in Scala, converting a sequence (list) into a Row object. You could also useRow(*column_list)to unpack the list directly if you prefer. - Default values: We used
Noneto mirror Scala's defaultnullfor String arrays. If you want empty strings instead, swap[None] *95with[""] *95.
If you plan to use this in a UDF (user-defined function) with a DataFrame, here's how to set that up:
from pyspark.sql.functions import udf from pyspark.sql.types import StructType, StructField, StringType # Define the schema for your custom Row (95 nullable string columns) row_schema = StructType([ StructField(f"column_{i}", StringType(), nullable=True) for i in range(95) ]) # Register the UDF build_row_udf = udf(build_custom_row, row_schema) # Apply it to your DataFrame df = df.withColumn("custom_row", build_row_udf(df["your_input_string_column"]))
Content of the question originates from Stack Exchange, question author Felipe Avalos
相关产品推荐
相关产品推荐

