var locs = List("China","United States","European Union","Russia","Japan","United Kingdom","Singapore") val outputPath = "/dbcovid/result/new_cases/China" println(outputPath+" ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt") //(重点国家)新增病例/死亡数量 for(loc <- locs){ spark.sql("select new_cases from data where location='"+loc+"'") .write.json("/dbcovid/result/new_cases/"+loc+"/") spark.sql("select new_deaths from data where location='"+loc+"'") .write.json("/dbcovid/result/new_deaths/"+loc+"/") }之前报过一个错误,但是这句话执行过,导致我再执行的时候会报路径存在的错误,但是我反复检查又看不到这个路径。这是出了什么问题
时间: 2023-07-08 13:31:13 浏览: 153
ppx_let_locs:改善让monadic的堆栈跟踪
根据你提供的代码,可以看出你在循环中多次将数据写入同一个路径 `/dbcovid/result/new_cases/China`,因此在第一次循环中如果该路径不存在,程序将正常运行并将数据写入该路径,但在第二次循环中由于该路径已经存在,程序将会抛出“路径已存在”的错误。
为了解决这个问题,你需要使用每个国家的名称来构建不同的输出路径。你可以使用以下代码来动态构建输出路径,以确保每个国家的数据都被写入不同的路径:
```
for (loc <- locs) {
val newCasesOutputPath = s"/dbcovid/result/new_cases/$loc/"
val newDeathsOutputPath = s"/dbcovid/result/new_deaths/$loc/"
spark.sql(s"select new_cases from data where location='$loc'")
.write.json(newCasesOutputPath)
spark.sql(s"select new_deaths from data where location='$loc'")
.write.json(newDeathsOutputPath)
}
```
在上述代码中,我们使用 `$loc` 变量来构建不同的输出路径。这将确保每个国家的数据都被写入不同的路径,避免了多次写入同一个路径的问题。
阅读全文