'HiveContext' object has no attribute 'jsonFile' && 'DataFrame' object has no attribute 'map'报错解决

本文针对SparkSQL在Ubuntu16.04环境下遇到的两个常见错误进行了解析,并提供了详细的解决方案。错误一涉及过时的jsonFile方法调用,已由read().json()替代;错误二则关于DataFrame对象的map方法误用,正确方式应使用rdd.map()。文章通过实例演示了如何避免这些错误,对初学者和开发者具有一定的参考价值。

最近在学习spark,在Spark SQL这一块遇到如题所示的一个小错,在“Stack Overflow“上找到了类似的解决方法,写下了做个小记。
测试环境:Ubuntu 16.04;Spark2.4

错误1 描述

input = hiveCtx.jsonFile(inputFile)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-431897e98712> in <module>()
----> 1 input = hiveCtx.jsonFile(inputFile)

AttributeError: 'HiveContext' object has no attribute 'jsonFile'

错误分析及解决

在较新的Spark版本中如上所示的方法已经被read().json()代替,而一些书籍上还没更新过来。按照最近的使用方法即可解决。

In [14]: input = hiveCtx.read.json(inputFile)
2019-01-23 15:24:30 WARN  Utils:66 - Truncated the string representation of a plan since it was too large. This behavior can be adjusted by setting 'spark.debug.maxToStringFields' in SparkEnv.conf.

错误2 描述

In [22]: topTweetText = topTweets.map(lambda row : row.text)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-22-9a3f5d661d19> in <module>()
----> 1 topTweetText = topTweets.map(lambda row : row.text)

/home/spark/python/pyspark/sql/dataframe.pyc in __getattr__(self, name)
   1298         if name not in self.columns:
   1299             raise AttributeError(
-> 1300                 "'%s' object has no attribute '%s'" % (self.__class__.__name__, name))
   1301         jc = self._jdf.apply(name)
   1302         return Column(jc)

AttributeError: 'DataFrame' object has no attribute 'map'

报错解决

In [23]: topTweetText = topTweets.rdd.map(lambda row : row.text)

In [24]: topTweetText.collect()
Out[24]: [u'Adventures With Coffee, Code, and Writing.']  

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值