我定义了以下功能:
def hdfsToSchemaRDD(hdfsPath, tableName, tableHeader):
lines = sc.textFile(hdfsPath)
fields = [StructField(field_name, StringType(), True) for field_name in tableHeader.split()]
schema = StructType(fields)
columns = lines.map(lambda l: l.split(","))
tempTable = columns.map(lambda c: tuple([ c[i] for i,v in enumerate(tableHeader.split()) ]))
schemaTable = sqlContext.applySchema(tempTable, schema)
schemaTable.registerTempTable(tableName)
sqlContext.cacheTable(tableName)
目的是在给定路径的情况下从hdfs读取数据集,并将其存储到内存中,这样我每次需要查询时都不需要重新读取它,这通常是。
我遇到的问题是假设我加载了一些大表LargeTable
并运行该函数:
def FeatureSummary(features):
results = sqlContext.sql("""
SELECT
%s
,SUM(summable) as s1, SUM(othersummable) as s2
FROM
LargeTable
GROUP BY
%s
ORDER BY
%s
"""%(features,features,features))
for row in results.map(lambda x: x).collect():
print row
FeatureSummary(attribute1)
,FeatureSummary(attribute2)
,FeatureSummary(attribute3)
似乎每次都在LargeTable
中阅读。特别是,我认为,鉴于属性的级别非常少,FeatureSummary
函数中的查询运行速度会比LargeTable
的读取速度快得多。事实并非如此。
有没有办法检查表是否已被缓存?
如何修改函数hdfsToSchemaRDD
以实际缓存来自hdfs的数据?