【发布时间】:2019-04-10 08:34:01
【问题描述】:
我正在使用 ConfigParser 读取传递给我的 pyspark 程序的键值。当我从 hadoop 集群的边缘节点执行时,代码工作正常,配置文件位于边缘节点的本地目录中。如果配置文件上传到 hdfs 路径并且我尝试使用解析器访问相同的路径,则不会。
配置文件 para.conf 有以下内容
[tracker]
port=9801
在本地客户端模式下,使用本地目录中的 para.conf 来访问我正在使用下面的值。
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read("para.conf")
myport = parser.get('tracker', 'port')
上面的工作正常...
在 Hadoop 集群上: 上传para.conf文件到hdfs目录路径bdc/para.conf
parser.read("hdfs://clusternamenode:8020/bdc/para.conf")
这不会返回任何东西,下面的也不会通过转义..
parser.read("hdfs:///clusternamenode:8020//bdc//para.conf")
虽然使用 sqlCONtext 我可以读取这个返回有效 rdd 的文件。
sc.textFile("hdfs://clusternamenode:8020/bdc/para.conf")
虽然不确定使用 configParser 是否可以从中提取键值..
谁能建议 configParser 是否可用于从 hdfs 读取文件?或者有没有其他选择?
【问题讨论】:
-
问题是ConfigParser不能处理hdfs文件路径。您可以做的是实现自己的 configreader 或使用
bla = sc.textFile("hdfs://clusternamenode:8020/bdc/para.conf").collect()读取,它会为您提供字符串列表。 configreader 可以处理带有read_string 的字符串。 -
read_string 不是选项,因为我使用的是 Python 2.7+ 。尝试按照stackoverflow.com/questions/21766451/… buf = StringIO.StringIO(s_config) config = ConfigParser.ConfigParser() config.readfp(buf) 中的建议使用,但这给出了 no Section 错误!
-
您能否用您使用的代码扩展您的问题并完成您收到的错误消息?
-
使用 read_string 选项 import ConfigParser credstr = sc.textFile("hdfs://clusternamenode:8020/bdc/cre.conf").collect() parse_str=ConfigParser.ConfigParser() parse_str.read_string( credstr) 收到错误:AttributeError: ConfigParser instance has no attribute 'read_string'
-
使用文件缓冲选项` import ConfigParser import StringIO credstr = sc.textFile("hdfs://clusternamenode:8020/bdc/cre.conf").collect() buf = StringIO.StringIO(credstr ) parse_str = ConfigParser.ConfigParser() parse_str.readfp(buf) parse_str.get('tracker','port') ` 收到错误:- raise NoSectionError(section) ConfigParser.NoSectionError: No section: 'tracker'
标签: hadoop pyspark hdfs pyspark-sql