考虑到输入数据集适合本地计算机的内存,建议在本地开发和测试 python 转换代码的方法是什么?
最佳答案
不需要模拟转换包的最简单方法是将逻辑提取到带有 pyspark 函数的纯 python 中,该函数接收数据帧作为输入并返回数据帧。
即:
# yourtransform.py
from my_business_logic import magic_super_complex_computation
@transform_df(
Output("/foo/bar/out_dataset"),
input1=Input("/foo/bar/input1"),
input2=Input("/foo/bar/input2"))
def my_transform(input1, input2):
return magic_super_complex_computation(input1, input2)
您现在可以在测试中导入 magic_super_complex_computation
并仅使用 pyspark 进行测试。
即:
from my_business_logic import magic_super_complex_computation
def test_magic_super_complex_computation(spark_context):
df1 = load_my_data_as_df(spark_context, "input1")
df2 = load_my_data_as_df(spark_context, "input2")
result = magic_super_complex_computation(input1, input2).collect()
assert len(result) == 123
请注意,这要求您提供有效的 Spark 上下文作为 pytest(或您正在使用的任何测试框架)中的固定装置
关于pyspark - 如何在本地开发和测试python转换代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64711056/