spark中python读取json

本文展示了如何在Spark中使用Python读取JSON文件。通过创建SparkContext,指定输入文件路径,然后使用map函数配合json.loads进行解析,最后通过foreach打印结果。在运行示例代码时,虽然遇到了SparkUI端口占用的问题,但最终成功输出了JSON数据。
部署运行你感兴趣的模型镜像

找到json文件

       /usr/local/spark/examples/src/main/resources/people.json

编写应用程序jsontext.py

       frompyspark import SparkContext

       importjson

       sc=SparkContext('local','JSONAPP')

       inputFile="file:///usr/local/spark/examples/src/main/resources/people.json"

       jsonStrs=sc.textFile(inputFile)

       result=jsonStrs.map(lambdas : json.loads(s))

       result.foreach(print)

spark-submmit运行显示结果

       root@master:~/pysparkfile#spark-submit --master yarn /root/pysparkfile/jsontext.py

       SLF4J:Class path contains multiple SLF4J bindings.

       SLF4J:Found binding in[jar:file:/usr/local/spark/jars/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]

       SLF4J:Found binding in [jar:file:/usr/local/hadoop-2.7.5/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]

       SLF4J:See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

       SLF4J:Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

       18/04/2212:41:07 WARN Utils: Service 'SparkUI' could not bind on port 4040. Attemptingport 4041.

       18/04/2212:41:07 WARN Utils: Service 'SparkUI' could not bind on port 4041. Attemptingport 4042.

       18/04/2212:41:07 WARN Utils: Service 'SparkUI' could not bind on port 4042. Attemptingport 4043.

       18/04/2212:41:07 WARN Utils: Service 'SparkUI' could not bind on port 4043. Attemptingport 4044.

       18/04/2212:41:07 WARN Utils: Service 'SparkUI' could not bind on port 4044. Attemptingport 4045.

       18/04/2212:41:07 WARN Utils: Service 'SparkUI' could not bind on port 4045. Attemptingport 4046.

       18/04/2212:41:07 WARN Utils: Service 'SparkUI' could not bind on port 4046. Attemptingport 4047.

       {'name':'Michael'}

       {'age':30, 'name': 'Andy'}

       {'age':19, 'name': 'Justin'}

       root@master:~/pysparkfile#

      

 


您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值