代码之家  ›  专栏  ›  技术社区  ›  Shankar Panda

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

  •  0
  • Shankar Panda  · 技术社区  · 6 年前

    我正在尝试将SQL结果发送到for循环。我是新来的火花和巨蟒,请帮助。

        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()
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   y2k-shubham    6 年前
    • 使用 DataFrame.collect() method 集合了 Spark-SQL 从所有查询 遗嘱执行人 进入之内 司机 .

    • 这个 collect() 方法将返回 Python list ,每个元素都是 Spark Row

    • 然后可以在 for 回路


    代码片段:

    data1 = hive_context.sql("select col_name from schema_def where data_type<>'string'")
    colum_names_as_python_list_of_rows = data1.collect()
    
        2
  •  1
  •   ThatDataGuy    6 年前

    我想你需要问问自己 为什么 您希望对数据进行迭代。

    您正在进行聚合吗?转换数据?如果是这样,考虑使用Spark API来实现。

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