【发布时间】: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