【问题标题】:Persistent PySpark Dataframe from a Hive query来自 Hive 查询的持久 PySpark 数据框
【发布时间】:2016-03-29 00:57:51
【问题描述】:

我正在从 Hive 表中获取一些数据:

df = sqlContext.sql('select shubiru, date from thebigtable bt where bt.num > 10 ')
df.show() # here the query is processed and the results shown

而且效果很好。现在我想对 df 进行操作,但是每次我对 df 进行操作时,它都会再次针对 Hive 运行查询:

import pyspark.sql.functions as func
from datetime import datetime
from pyspark.sql.types import TimestampType

dt_udt = func.udf(lambda x: datetime.strptime(str(x), '%Y%m%d') if x else None, TimestampType())
df = df.withColumn('fdate', dt_udt(df.date)) 
df.show()  # here the query is run again and the transformation is done

所以我认为如果我在 df 上调用persist,查询将不会再次运行:

df.cache()
df = df.withColumn('fdate', dt_udf(df.date))

但没有骰子,查询再次针对 Hive 运行并由 UDF 处理。有没有办法在内存中缓存查询结果并在数据帧上运行操作而无需每次都点击 Hive?

【问题讨论】:

    标签: python hadoop apache-spark pyspark


    【解决方案1】:

    每当对数据执行操作时,Spark SQL 都会从 DataSource(在您的情况下为 Hive)中提取数据。在这种情况下,您试图在 cache() 之后重命名列,这将是无用的。我的建议是有类似的东西

    df = df.withColumn('fdate', dt_udf(df.date)).withColumn('date_column_2', dt_udf(df.date)).cache()
    

    此语句之后的所有操作都将对 spark 中持久化的数据进行操作。但是缓存大量数据会自动驱逐旧的 RDD 分区,并且需要返回 hive 重新生成丢失的分区。

    【讨论】:

    • 谢谢。为什么是第二个withColumn?我不是要重命名date 列,而是尝试用新格式对​​其进行转换。
    猜你喜欢
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2016-07-03
    • 2019-11-19
    • 2020-11-22
    • 2022-01-02
    相关资源
    最近更新 更多