【发布时间】:2016-09-05 08:17:30
【问题描述】:
是否可以使用 UDF 中不可用的复杂函数更新 pyspark 中的 hiveContext 数据框列?
我有一个包含许多列的数据框,其中 2 列称为时间戳和数据。如果数据中的时间戳满足某些条件,我需要从数据中的 JSON 字符串中检索时间戳并更新时间戳列。我知道数据框是不可变的,但是可以以某种方式构建一个新的数据框,保留旧数据框的所有列但更新 timstamp 列?
说明我想做的代码:
def updateTime(row):
import json
THRESHOLD_TIME = 60 * 30
client_timestamp = json.loads(row['data'])
client_timestamp = float(client_timestamp['timestamp'])
server_timestamp = float(row['timestamp'])
if server_timestamp - client_timestamp <= THRESHOLD_TIME:
new_row = ..... # copy contents of row
new_row['timestamp'] = client_timestamp
return new_row
else:
return row
df = df.map(updateTime)
我想过将行内容映射到元组,然后使用 .toDF() 将其转换回数据帧,但我找不到将行内容复制到元组然后取回列名的方法。
【问题讨论】:
-
如果你使用
UDF呢? -
或许这篇文章能帮上忙:sparktutorials.net/…
-
对不起,我的意思是 UDF 而不是 HDF...错字...
标签: json apache-spark dataframe hive pyspark