问题描述
第二次我运行查询的速度明显更快。为什么?
代码:
publicvoidtest3() {
Dataset<Row>sqlDF=spark.read().json(path:"src/main/resources/data/ipl.json");
sqlDF.repartition(2);
Dataset<Row>result1=sqlDF.where("run>10000").select(col:"team",...cols:"run");
//Dataset<Row>cachedPartition=result1.cache();
result.collect();
//result1.show();log.info("PhysicalPlan\n"+result1.queryExecution().executedplan());
Dataset<Row>result2=sqlDF.where("run>10000").select(col:"team",..cols:"run");
result2.collect();
//result1.show();
Log.info("PhysicalPlan\n"+result2.queryExecution().executedplanq);
}
身体计划:
spark UI的执行时间:
为什么这些查询花费的时间不同,为什么执行时间却有如此大的差异?缓存是在后台进行的吗?如果是,为什么在物理计划中未提及?
解决方法
您正在将Spark指向文件。第二次访问同一文件时,将更快地访问该文件。
如果您两次运行以下代码,则情况相同(当然,Scala使用JVM以及java.nio和java.io)。
with open("src/main/resources/data/ipl.json") as f:
t = f.read()
print(t)
第一次,必须初始化I / O操作。第二次,I / O操作可以重用上次运行的部分内容。如果文件很小(如您所愿),则整个文件将被缓存。