您的位置:首页 > 移动开发 > Objective-C

AttributeError: 'NoneType' object has no attribute 'sc' 解决方法!

2016-11-26 00:24 721 查看
自从解决了第一个问题,于是博主又开始了第二个代码了。

依旧说下:本人的操作系统为win7,python3,spark 1.6,anaconda3

代码如下:

lines = sc.textFile("D://Program Files//spark//spark-1.6.0-bin-hadoop2.6//README.md")
pythonLines = lines.filter(lambda line: "Python" in line)
print("过滤后文本的第一行:",pythonLines.first())
pythonLines.persist
print("过滤后文本的行数:",pythonLines.count())
sc.stop()


第一次run程序的结果如下:

过滤后文本的第一行: high-level APIs in Scala, Java, Python, and R, and an optimized engine that
过滤后文本的行数: 3


第二以及第N次run的结果如下:

AttributeError                            Traceback (most recent call last)
<ipython-input-8-37c825a1b443> in <module>()
1
----> 2 lines = sc.textFile("D://Program Files//spark//spark-1.6.0-bin-hadoop2.6//README.md")
3 pythonLines = lines.filter(lambda line: "Python" in line)
4 print("过滤后文本的第一行:",pythonLines.first())
5 pythonLines.persist

D:\spark\spark-1.6.0-bin-hadoop2.6\python\pyspark\context.py in textFile(self, name, minPartitions, use_unicode)
471         [u'Hello world!']
472         """
--> 473         minPartitions = minPartitions or min(self.defaultParallelism, 2)
474         return RDD(self._jsc.textFile(name, minPartitions), self,
475                    UTF8Deserializer(use_unicode))

D:\spark\spark-1.6.0-bin-hadoop2.6\python\pyspark\context.py in defaultParallelism(self)
346         reduce tasks)
347         """
--> 348         return self._jsc.sc().defaultParallelism()
349
350     @property

AttributeError: 'NoneType' object has no attribute 'sc'


竟然报错了,而且报了个空对象没有sc的这个属性成员。这。。。完全不可能吧,于是又是一堆google、百度、stack overflow,依旧解决不了问题,最后灵机一动,它不是说空对象吗?那我直接给他初始化一个对象不就可以了吗?

于是代码如下:

sc = SparkContext("local", "Simple App") #初始化一个新的sc

lines = sc.textFile("D://Program Files//spark//spark-1.6.0-bin-hadoop2.6//README.md") pythonLines = lines.filter(lambda line: "Python" in line) print("过滤后文本的第一行:",pythonLines.first()) pythonLines.persist print("过滤后文本的行数:",pythonLines.count()) sc.stop()


第一次以及第N次运行程序结果如下:



完美解决了这个错误!!

最后,希望我的错误记录能帮到未知的你!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐