在 dagster 教程的Materializiations 部分中,我们为中间输出选择一个文件名 ( sorted_cereals_csv_path
),然后将其作为物化输出:
@solid
def sort_by_calories(context, cereals):
# Sort the data (removed for brevity)
sorted_cereals_csv_path = os.path.abspath(
'calories_sorted_{run_id}.csv'.format(run_id=context.run_id)
)
with open(sorted_cereals_csv_path, 'w') as fd:
writer = csv.DictWriter(fd, fieldnames)
writer.writeheader()
writer.writerows(sorted_cereals)
yield Materialization(
label='sorted_cereals_csv',
description='Cereals data frame sorted by caloric content',
metadata_entries=[
EventMetadataEntry.path(
sorted_cereals_csv_path, 'sorted_cereals_csv_path'
)
],
)
yield Output(None)
然而,这依赖于我们可以使用本地文件系统(这可能不是真的)这一事实,它可能会被以后的运行覆盖(这不是我想要的)并且它也迫使我们想出一个文件名永远不会被使用。
我想在我的大部分实体中做的只是说“这是一个文件对象,请为我存储它”,而不用关心它的存储位置。我可以在不考虑所有这些事情的情况下实现文件吗?我应该tempfile
为此使用python的工具吗?