如何使用pyspark将sql语句的结果发送到for循环?

2024-05-19 05:07:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将sql结果发送到for循环。我刚接触过spark和python,请帮忙。在

    from pyspark import SparkContext
sc =SparkContext()
from pyspark.sql import HiveContext
hive_context = HiveContext(sc)
#bank = hive_context.table("cip_utilities.file_upload_temp")
data=hive_context.sql("select * from cip_utilities.cdm_variable_dict")
hive_context.sql("describe cip_utilities.cdm_variables_dict").registerTempTable("schema_def")
temp_data=hive_context.sql("select * from schema_def")
temp_data.show()
data1=hive_context.sql("select col_name from schema_def where data_type<>'string'")
data1.show()

Tags: fromimportsqldataschemadefcontextselect
2条回答

我想你应该问问你自己为什么要迭代数据。在

你在做汇总吗?转换数据?如果是这样,可以考虑使用spark API。在

打印一些文本?如果是这样,则使用.collect()并将数据检索回驱动程序进程。然后,可以用通常的python方法循环结果。在

  • 使用^{} method,它将来自所有执行器的Spark-SQL查询的结果聚合到驱动程序

  • collect()方法将返回一个Pythonlist,其中每个元素都是SparkRow

  • 然后可以在for-循环中迭代该列表


代码段:

data1 = hive_context.sql("select col_name from schema_def where data_type<>'string'")
colum_names_as_python_list_of_rows = data1.collect()

相关问题 更多 >

    热门问题