【问题标题】:is there a way convert a spark dataframe generated from a sql statement into an rdd?有没有办法将从 sql 语句生成的 spark 数据帧转换为 rdd?
【发布时间】:2019-04-14 12:01:24
【问题描述】:

如果我使用这个 spark sql 语句:

df = spark.sql('SELECT col_name FROM table_name')

它将返回一个火花数据框对象。如何将其转换为 rdd?有没有办法直接使用 sql 读取表但生成 rdd 而不是数据框?

提前致谢

【问题讨论】:

  • df.rdd 应该给你 RDD
  • 我试过了,但没有,相反,我收到以下错误:PicklingError: Could not serialize object: Py4JError: An error occurred while calling o60.__getstate__. Trace: py4j.Py4JException: Method __getstate__([]) does not exist at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318) at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326) at py4j.Gateway.invoke(Gateway.java:274) at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132) at py4j.commands.CallCommand.execute(CallCommand.java:79)
  • 我在这里访问了很多帖子,谈论或多或少相同的事情,但我得到了这个错误
  • stackoverflow.com/questions/29000514/… 尝试此处建议的替代方案之一

标签: python python-3.x apache-spark dataframe pyspark


【解决方案1】:
df = spark.sql('SELECT col_name FROM table_name')

df.rdd # 你可以保存它,执行转换等。

df.rdd 将内容作为行的pyspark.RDD 返回。

然后,您可以在 Row 的 RDD 上进行映射,将每一行转换为 numpy 向量。我无法更具体地了解转换,因为我不知道您的向量在给出的信息中代表什么。

注意 1df 是定义我们的 Dataframe 的变量。

注意 2:此功能自 Spark 1.3 起可用

【讨论】:

  • 这解决了PicklingError: Could not serialize object: Py4JError: An error occurred while calling o60.__getstate__. Trace: py4j.Py4JException: Method __getstate__([]) does not exist at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318) at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326) at py4j.Gateway.invoke(Gateway.java:274) at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132) at py4j.commands.CallCommand.execute(CallCommand.java:79) 错误吗?
猜你喜欢
  • 1970-01-01
  • 2020-09-06
  • 2020-04-09
  • 2018-05-24
  • 2017-08-24
  • 1970-01-01
  • 2021-05-11
  • 2019-12-08
  • 1970-01-01
相关资源
最近更新 更多