【问题标题】:how to get an integer value while querying a count query through DataFrame?通过DataFrame查询计数查询时如何获取整数值?
【发布时间】:2017-05-04 07:31:31
【问题描述】:

我正在编写这段代码来获取指定表中count的整数值:

sc = SparkContext("local", "spar")
hive_context = HiveContext(sc)
hive_context.sql("use zs_trainings_trainings_db")
df = hive_context.sql("select count(*) from ldg_sales")

【问题讨论】:

    标签: python hive pyspark pyspark-sql hivecontext


    【解决方案1】:

    要么:

    hive_context.table("sales").count
    

    hive_context.sql("select count(*) from ldg_sales").first()[0]
    

    【讨论】:

      【解决方案2】:

      将数据框转换为 rdd,以便您可以在其上运行 map 任务以提取行值,例如 -

      df = hive_context.sql("select count(*) as cnt from ldg_sales")
      count = df.rdd.map(lambda _ : _.cnt).collect()[0]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多