我有一个脚本,希望将配置文件传递到该脚本中。在“胶水作业”页面上,我看到有一个“参考文件路径”指向我的配置文件。然后如何在ETL脚本中使用该文件?
我尝试过from configuration import *
,其中引用的文件名为configuration.py
,但是没有运气(ImportError:没有名为配置的模块)。
我注意到了同样的问题。我相信已经有解决这个问题的票证了,但是这是AWS支持同时建议的。
如果您在Python中使用引用文件路径变量shell job,在
/tmp
中找到了引用的文件,其中Python Shell默认情况下,作业没有访问权限。但是,相同的操作有效在Spark作业中成功完成,因为该文件在默认文件中找到文件目录。
下面的代码有助于找到在胶水作业配置中引用的samplefile.json
的绝对路径并打印其内容。
import sys, os
def _find(pathname, matchFunc=os.path.isfile):
for dirname in sys.path:
candidate = os.path.join(dirname, pathname)
if matchFunc(candidate):
return candidate
raise Exception("Can't find the file "+ pathname)
def findFile(pathname):
return _find(pathname)
print(findFile('samplefile.json'))
with open(findFile('samplefile.json'), "r") as f:
log = f.read()
print(log)
Boto3 API也可以用来访问引用的文件
import boto3
s3 = boto3.resource('s3')
obj = s3.Object('test-bucket-hc-7', 'samplefile.json')
for line in obj.get()['Body']._raw_stream:
print(line)