PySpark运行WordCount程序报错:TypeError: ‘JavaPackage‘ object is not callable

代码:

#coding:utf8
from pyspark import SparkConf,SparkContext

if __name__ == '__main__':
conf = SparkConf().setMaster("local[*]").setAppName("WordCountHelloWord")
#通过SparkConf对象构建SparkContext对象
sc = SparkContext(conf=conf)


#读取文件
file_rdd = sc.textFile("hdfs://hadoop105:8020/study/spark/data/words.txt")

#将单词进行切割,得到一个存储全部单词的集合对象
words_rdd = file_rdd.flatMap(lambda line: line.split(" "))

#将单词转换为元组对象,key是单词,value是数字
words_with_one_rdd = words_rdd.map(lambda x: (x, 1))

#将元组的value 按照key来分组,对所有的value执行聚合操作
result_rdd = words_with_one_rdd.reduceByKey(lambda a, b: a + b)

#通过collect方法手机RDD的数据打印输出结果
print(result_rdd.collect())

报错为:

Traceback (most recent call last):
  File "/tmp/pycharm_project_909/00_example/HelloWord.py", line 21, in <module>
    result_rdd = words_with_one_rdd.reduceByKey(lambda a, b: a + b)
  File "/opt/module/anaconda3/envs/pyspark/lib/python3.8/site-packages/pyspark/rdd.py", line 3552, in reduceByKey
    return self.combineByKey(lambda x: x, func, func, numPartitions, partitionFunc)
  File "/opt/module/anaconda3/envs/pyspark/lib/python3.8/site-packages/pyspark/rdd.py", line 3987, in combineByKey
    shuffled = locally_combined.partitionBy(numPartitions, partitionFunc)
  File "/opt/module/anaconda3/envs/pyspark/lib/python3.8/site-packages/pyspark/rdd.py", line 3897, in partitionBy
    pairRDD = self.ctx._jvm.PairwiseRDD(keyed._jrdd.rdd()).asJavaPairRDD()
  File "/opt/module/anaconda3/envs/pyspark/lib/python3.8/site-packages/pyspark/rdd.py", line 5470, in _jrdd
    wrapped_func = _wrap_function(
  File "/opt/module/anaconda3/envs/pyspark/lib/python3.8/site-packages/pyspark/rdd.py", line 5270, in _wrap_function
    return sc._jvm.SimplePythonFunction(
TypeError: 'JavaPackage' object is not callable 

是因为pyspark库的版本过高导致

解决办法:

 pip install pyspark==3.2.0

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zc_777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值