amazon-s3 - 作为 luigi 任务的结果,如何将泡菜文件写入 S3?
问题描述
由于 luigi 任务,我想在 S3 上存储一个泡菜文件。下面是定义任务的类:
class CreateItemVocabulariesTask(luigi.Task):
def __init__(self):
self.client = S3Client(AwsConfig().aws_access_key_id,
AwsConfig().aws_secret_access_key)
super().__init__()
def requires(self):
return [GetItem2VecDataTask()]
def run(self):
filename = 'item2vec_results.tsv'
data = self.client.get('s3://{}/item2vec_results.tsv'.format(AwsConfig().item2vec_path),
filename)
df = pd.read_csv(filename, sep='\t', encoding='latin1')
unique_users = df['CustomerId'].unique()
unique_items = df['ProductNumber'].unique()
item_to_int, int_to_item = utils.create_lookup_tables(unique_items)
user_to_int, int_to_user = utils.create_lookup_tables(unique_users)
with self.output()[0].open('wb') as out_file:
pickle.dump(item_to_int, out_file)
with self.output()[1].open('wb') as out_file:
pickle.dump(int_to_item, out_file)
with self.output()[2].open('wb') as out_file:
pickle.dump(user_to_int, out_file)
with self.output()[3].open('wb') as out_file:
pickle.dump(int_to_user, out_file)
def output(self):
files = [S3Target('s3://{}/item2int.pkl'.format(AwsConfig().item2vec_path), client=self.client),
S3Target('s3://{}/int2item.pkl'.format(AwsConfig().item2vec_path), client=self.client),
S3Target('s3://{}/user2int.pkl'.format(AwsConfig().item2vec_path), client=self.client),
S3Target('s3://{}/int2user.pkl'.format(AwsConfig().item2vec_path), client=self.client),]
return files
当我运行这个任务时,我得到了错误ValueError: Unsupported open mode 'wb'
。我尝试转储到 pickle 文件中的项目只是 python 字典。
完整追溯:
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\luigi\worker.py", line 203, in run
new_deps = self._run_get_new_deps()
File "C:\Anaconda3\lib\site-packages\luigi\worker.py", line 140, in _run_get_new_deps
task_gen = self.task.run()
File "C:\Users\user\Documents\python workspace\pipeline.py", line 60, in run
with self.output()[0].open('wb') as out_file:
File "C:\Anaconda3\lib\site-packages\luigi\contrib\s3.py", line 714, in open
raise ValueError("Unsupported open mode '%s'" % mode)
ValueError: Unsupported open mode 'wb'
解决方案
这是一个仅在 python 3.x 上发生的问题,如此处所述。为了使用 python 3 并编写二进制文件或目标(即使用 'wb' 模式),只需将 S3Target 的格式参数设置为Nop
. 像这样:
S3Target('s3://path/to/file', client=self.client, format=luigi.format.Nop)
请注意,这只是一个技巧,并不那么直观,也没有记录。
推荐阅读
- html - 使用 css 选择器的 next 按钮和 href 属性打开不同的网页
- javascript - 如何根据数组显示类别子类别
- c++ - 如何在 boost::variant 中存储引用
而不是复制对象? - r - 函数为空后的新数据框
- extjs - extjs如何参数化组件
- android - 为什么我在这个 Android 项目中得到 NullPointer 异常
- ios - 在 WKWebView 中点击按钮后 UIButton 选择器不起作用
- emacs - 带有emacs org-mode的Haskell:变量不在范围内
- python - 按分位数对 Pandas 数据帧进行排名
- r - 在 R Studio 中自定义制图输出的困难