【问题标题】:Loading local file in sc.textFile在 sc.textFile 中加载本地文件
【发布时间】:2017-01-25 21:46:46
【问题描述】:

我尝试如下加载本地文件

File = sc.textFile('file:///D:/Python/files/tit.csv')
File.count()

完整的追溯

IllegalArgumentException                  Traceback (most recent call last)
<ipython-input-72-a84ae28a29dc> in <module>()
----> 1 File.count()

/databricks/spark/python/pyspark/rdd.pyc in count(self)
   1002         3
   1003         """
-> 1004         return self.mapPartitions(lambda i: [sum(1 for _ in i)]).sum()
   1005 
   1006     def stats(self):

/databricks/spark/python/pyspark/rdd.pyc in sum(self)
    993         6.0
    994         """
--> 995         return self.mapPartitions(lambda x: [sum(x)]).fold(0, operator.add)
    996 
    997     def count(self):

/databricks/spark/python/pyspark/rdd.pyc in fold(self, zeroValue, op)
    867         # zeroValue provided to each partition is unique from the one provided
868         # to the final reduce call
--> 869         vals = self.mapPartitions(func).collect()
    870         return reduce(op, vals, zeroValue)
    871 

/databricks/spark/python/pyspark/rdd.pyc in collect(self)
    769         """
    770         with SCCallSiteSync(self.context) as css:
--> 771             port = self.ctx._jvm.PythonRDD.collectAndServe(self._jrdd.rdd())
    772         return list(_load_from_socket(port, self._jrdd_deserializer))
    773 

/databricks/spark/python/lib/py4j-0.9-src.zip/py4j/java_gateway.py in __call__(self, *args)
    811         answer = self.gateway_client.send_command(command)
    812         return_value = get_return_value(
--> 813             answer, self.gateway_client, self.target_id, self.name)
    814 
    815         for temp_arg in temp_args:

/databricks/spark/python/pyspark/sql/utils.pyc in deco(*a, **kw)
     51                 raise AnalysisException(s.split(': ', 1)[1], stackTrace)
     52             if s.startswith('java.lang.IllegalArgumentException: '):
---> 53                 raise IllegalArgumentException(s.split(': ', 1)[1], stackTrace)
     54             raise
     55     return deco

IllegalArgumentException: u'java.net.URISyntaxException: Expected scheme-specific part at index 2: D:'

怎么了?我照常做 例如 load a local file to spark using sc.textFile() 要么 How to load local file in sc.textFile, instead of HDFS 这些示例适用于 scala,但如果我不介意的话,对于 python 来说也是一样的

但是

val File = 'D:\\\Python\\files\\tit.csv'


SyntaxError: invalid syntax
  File "<ipython-input-132-2a3878e0290d>", line 1
    val File = 'D:\\\Python\\files\\tit.csv'
           ^
SyntaxError: invalid syntax

【问题讨论】:

  • 您是否尝试过 textFile('D:/ 或使用转义的反斜杠,因为您在 Windows 上?
  • 虽然,看到/databricks/spark/ 让我觉得你根本没有使用 Windows 机器,而是使用了一些 Databricks 平台
  • 我在 Windows 上,我试过 sc.textFile('file://D:/Python/files/tit.csv') & sc.textFile('file:/D:/Python/ files/tit.csv') & sc.textFile('D:/Python/files/tit.csv')
  • 如何在普通的 Python 解释器中打开同一个文件? open(),大概?这行得通吗?

标签: python apache-spark


【解决方案1】:

更新: hadoop中的“:”似乎有问题...

filenames with ':' colon throws java.lang.IllegalArgumentException

https://issues.apache.org/jira/browse/HDFS-13

Path should handle all characters

https://issues.apache.org/jira/browse/HADOOP-3257

在这个问答中,有人设法用 spark 2.0 克服了它

Spark 2.0: Relative path in absolute URI (spark-warehouse)


题中有几个问题:

1) windows下python访问本地文件

File = sc.textFile('file:///D:/Python/files/tit.csv')
File.count()

你能不能试试:

import os
inputfile = sc.textFile(os.path.normpath("file://D:/Python/files/tit.csv"))
inputfile.count()

os.path.normpath(路径)

通过折叠冗余分隔符和上级引用来规范化路径名,以便 A//B、A/B/、A/./B 和 A/foo/../B 都变为 A/B。这种字符串操作可能会改变包含符号链接的路径的含义。在 Windows 上,它将正斜杠转换为反斜杠。要规范大小写,请使用 normcase()。

https://docs.python.org/2/library/os.path.html#os.path.normpath

输出是:

>>> os.path.normpath("file://D:/Python/files/tit.csv")
'file:\\D:\\Python\\files\\tit.csv'

2) 在 python 中测试的 scala 代码:

val File = 'D:\\\Python\\files\\tit.csv'
SyntaxError: invalid syntax

此代码不能在 python 中运行,因为它是 scala 代码。

【讨论】:

    【解决方案2】:

    我已经完成了

    import os
    os.path.normpath("file:///D:/Python/files/tit.csv")
    Out[131]: 'file:/D:/Python/files/tit.csv'
    

    然后

    inputfile = sc.textFile(os.path.normpath("file:/D:/Python/files/tit.csv"))
    inputfile.count()
    IllegalArgumentException: u'java.net.URISyntaxException: Expected scheme-specific part at index 2: D:'
    

    如果我喜欢这个

    inputfile = sc.textFile(os.path.normpath("file:\\D:\\Python\\files\\tit.csv"))
    inputfile.count()
    IllegalArgumentException: u'java.net.URISyntaxException: Relative path in absolute URI: file:%5CD:%5CPython%5Cfiles%5Ctit.csv'
    

    我确实喜欢这个

    os.path.normcase("file:///D:/Python/files/tit.csv")
    Out[136]: 'file:///D:/Python/files/tit.csv'
    inputfile = sc.textFile(os.path.normpath("file:///D:/Python/files/tit.csv"))
    inputfile.count()
    IllegalArgumentException: u'java.net.URISyntaxException: Expected scheme-specific part at index 2: D:'
    

    【讨论】:

    猜你喜欢
    • 2023-04-09
    • 2017-05-11
    • 2015-02-02
    • 2018-03-29
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多