简体   繁体   English

有没有办法将sql语句生成的spark数据帧转换为rdd?

[英]is there a way convert a spark dataframe generated from a sql statement into an rdd?

if i use this spark sql statement: 如果我使用以下spark sql语句:

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

it will return a spark dataframe object. 它将返回spark数据框对象。 How can i convert this to an rdd? 如何将其转换为rdd? is there a way to read a table directly using sql but generating an rdd instead of a dataframe? 有没有一种方法可以直接使用sql读取表,但生成rdd而不是数据框?

Thanks in advance 提前致谢

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

df.rdd # you can save it, perform transformations etc. df.rdd #您可以保存它,执行转换等。

df.rdd returns the content as an pyspark.RDD of Row. df.rdd返回的内容以pyspark.RDD行。

You can then map on that RDD of Row transforming every Row into a numpy vector. 然后,您可以映射到该行的RDD ,将每个行转换为一个numpy向量。 I can't be more specific about the transformation since I don't know what your vector represents with the information given. 我不能更具体地说明转换,因为我不知道您的向量在给定的信息下代表什么。

Note 1 : df is the variable define our Dataframe. 注意1df是定义我们的数据框的变量。

Note 2 : this function is available since Spark 1.3 注2 :此功能自Spark 1.3起可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM