美文网首页
pyspark: rdd2dataframe踩过的坑

pyspark: rdd2dataframe踩过的坑

作者: 张虾米试错 | 来源:发表于2019-12-03 00:02 被阅读0次

大纲

主要记录在rdd2dataframe遇到的问题:

  1. Input row doesn't have expected number of values required by the schema
  2. Some of types cannot be determined by the first 100 rows, please try again with sampling

用toDF()的方式转换

from pyspark.sql import Row
rdd = sc.parallelize([Row(name='Alice', age=5, height=80),Row(name='Bob', age=5, height=80),Row(name='Cycy', age=10, height=80),Row(name='Cycy', age=10, height=80),Row(name='Didi', age=12, height=75)])
df = rdd.toDF()
df.show()
"""
+---+------+-----+                                                              
|age|height| name|
+---+------+-----+
|  5|    80|Alice|
|  5|    80|  Bob|
| 10|    80| Cycy|
| 10|    80| Cycy|
| 12|    75| Didi|
+---+------+-----+
"""
1. Input row doesn't have expected number of values required by the schema

问题:某些行的某些字段缺失

rdd = sc.parallelize([Row(name='Alice', age=5, height=80),Row(name='Bob', age=5, height=80),Row(name='Cycy', age=10, height=80),Row(name='Cycy', height=80),Row(name='Didi', age=12)])
df = rdd.toDF()
df.show()
### error: Input row doesn't have expected number of values required by the schema. 3 fields are required while 2 values are provided.
2. Some of types cannot be determined by the first 100 rows, please try again with sampling

问题:某个字段的类型不一致,导致spark无法识别

rdd = sc.parallelize([Row(name='Alice', age=5, height=80),Row(name='Bob', age=5, height=80),Row(name='Cycy', age=10, height=80),Row(name='Cycy', age=10, height=80),Row(name=-1, age=12.0, height=75.5)])
df = rdd.toDF()
df.show()
"""
+----+------+-----+                                                             
| age|height| name|
+----+------+-----+
|   5|    80|Alice|
|   5|    80|  Bob|
|  10|    80| Cycy|
|  10|    80| Cycy|
|null|  null|   -1|
+----+------+-----+
"""
rdd = sc.parallelize([Row(name=-1, age=10, height=80),Row(name='Alice', age=5, height=80),Row(name='Bob', age=5, height=80),Row(name=-1, age=10, height=80),Row(name=-1, age=10, height=80),Row(name='EaEa', age=5, height=80)])
df = rdd.toDF()
df.show()
"""
+---+------+----+
|age|height|name|
+---+------+----+
| 10|    80|  -1|
|  5|    80|null|
|  5|    80|null|
| 10|    80|  -1|
| 10|    80|  -1|
|  5|    80|null|
+---+------+----+
"""

当某个字段同时存在字符串型和数值型时,以第一个为准,若第一个字段为字符串型那么后面数值型可以正常显示;

扩展问题

同一个字段类型一定要一致,否则toDF()之后会被当作null处理!!!当不指定字段类型时,字段类型会遵照最开始的值。

rdd = sc.parallelize([Row(name='Alice', age=5, height=80),Row(name='Bob', age=5, height=80),Row(name='Cycy', age=10, height=80),Row(name='Cycy', age=10, height=80),Row(name='Didi', age=12.0, height=75.5)])
df = rdd.toDF()
df.show()
"""
+----+------+-----+                                                             
| age|height| name|
+----+------+-----+
|   5|    80|Alice|
|   5|    80|  Bob|
|  10|    80| Cycy|
|  10|    80| Cycy|
|null|  null| Didi|
+----+------+-----+
"""
rdd = sc.parallelize([Row(name='Alice', age=5, height=80.0),Row(name='Bob', age=5.0, height=80),Row(name='Cycy', age=10.0, height=80),Row(name='Cycy', age=10.0, height=80),Row(name='Didi', age=12.0, height=75)])
_df = rdd.toDF()
_df.show()
"""
+----+------+-----+
| age|height| name|
+----+------+-----+
|   5|  80.0|Alice|
|null|  null|  Bob|
|null|  null| Cycy|
|null|  null| Cycy|
|null|  null| Didi|
+----+------+-----+
"""

相关文章

  • pyspark: rdd2dataframe踩过的坑

    大纲 主要记录在rdd2dataframe遇到的问题: Input row doesn't have expect...

  • 交互设计师所要避免的几个坑

    前言 工作中难免会踩到几个坑,即使现在不踩以后还会踩,只有踩过才会深刻记住,踩过说明爱过!但是踩过的坑必须把坑填满...

  • vue踩过的坑

    vue踩过的坑

  • D1094:踩坑的价值最大化

    是人就会踩坑,不踩坑理论上就不属于人类,踩坑是人之常情,能回头站在坑边反思,才是对踩过的的坑价值最大化的体现,要不...

  • 投资避坑指南

    2022年9月14日(第224天) 经常反思踩过的坑,犯过的错,了解别人踩过的坑,犯过的错,思考如何避免自己下次踩...

  • PHP中的数据类型

    一说到数据类型,这个坑就太多了,多到有哪些坑,有多少坑,不知道自己还会踩哪些坑,以及踩过的坑还会不会再踩,我对...

  • 踩过的坑

    1、关于Windows下修改hosts文件无法生效,刚开始直接用浏览器打开发现404,后来ping域名才发现是连接...

  • 踩过的坑

    问题: 虚拟机中一打开metasploit就死机。 答: 发现虚拟机分配内存512M。分配2G后,正常。 问题: ...

  • 踩过的坑

    虚拟机/远程桌面实验室1&7:VNC远程桌面实验室2~6:VM虚拟机 VM虚拟机连接成功后不显示虚拟机资源库方法:...

  • 踩过的坑

    tomcat部署问题 问题概述:1. tomcat部署需要重启两次2. 定时任务要跑两次3. 配置的docbase...

网友评论

      本文标题:pyspark: rdd2dataframe踩过的坑

      本文链接:https://www.haomeiwen.com/subject/uxhqgctx.html