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

pyspark-如何使用pyspark计算每个字段的最小值和最大值?

  •  -1
  • Shankar Panda  · 技术社区  · 6 年前

    我试图找到sql语句生成的每个字段的最小值和最大值,并将其写入csv文件。我正试图以低于时尚的方式得到结果。你能帮忙吗?我已经用python编写过了,但是现在试图将它转换成pyspark,以便直接在hadoop集群中运行。

    enter image description here

    from pyspark.sql.functions import max, min, mean, stddev
    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_variables_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'")
    colum_names_as_python_list_of_rows = data1.collect()
    #data1.show()
    for line in colum_names_as_python_list_of_rows:
            #print value in MyCol1 for each row                
            ---Here i need to calculate min, max, mean etc for this particular field send by the for loop
    
    1 回复  |  直到 6 年前
        1
  •  4
  •   Neeraj Bhadani    6 年前

    可以使用不同的函数来查找最小值和最大值。下面是使用 agg 功能。

    from pyspark.sql.functions import *
    df = spark.table("HIVE_DB.HIVE_TABLE")
    df.agg(min(col("col_1")), max(col("col_1")), min(col("col_2")), max(col("col_2"))).show()
    

    但是,您也可以探索 describe summary (2.3版以后)函数,用于获取数据帧中各列的基本统计信息。

    希望这有帮助。

    当做,

    尼拉杰