在Apache Spark中重命名后使用列



我试图理解为什么Spark在相同的场景中表现不同。我重命名了两列,并试图在某些计算中同时使用这两列,但有一条语句出错,无法找到重命名的列。以下是代码

intermediateDF = intermediateDF.drop("GEO.id")
.withColumnRenamed("GEO.id2", "id")
.withColumnRenamed("GEO.display-label", "label")
.withColumn("stateid", functions.expr("int(id/1000)"))
.withColumn("countyId", functions.expr("id%1000"))
//.withColumn("countyState", functions.split(intermediateDF.col("label"), ","))
.withColumnRenamed("rescen42010", "real2010")
.drop("resbase42010")
.withColumnRenamed("respop72010", "est2010")
.withColumnRenamed("respop72011", "est2011")
.withColumnRenamed("respop72012", "est2012")
.withColumnRenamed("respop72013", "est2013")
.withColumnRenamed("respop72014", "est2014")
.withColumnRenamed("respop72015", "est2015")
.withColumnRenamed("respop72016", "est2016")
.withColumnRenamed("respop72017", "est2017")

注释掉的行是在错误下面抛出的行

Exception in thread "main" org.apache.spark.sql.AnalysisException: Cannot resolve column name "label" among (GEO.id, GEO.id2, GEO.display-label, rescen42010, resbase42010, respop72010, respop72011, respop72012, respop72013, respop72014, respop72015, respop72016, respop72017);

有人能帮我理解为什么Spark可以找到一个重命名的列(从GEO.id2id(,并对其进行计算吗但在其他(从GEO.display-label到label(上失败。我正在使用Apache Spark 3和Java。感谢

尝试以下语法:

.withColumn("countyState", functions.split(col("label"), ","))

它应该很好用。

检查以下代码。

intermediateDF.select( 
col("GEO.id2").alias("id"), 
functions.expr("int(id/1000)").alias("stateid"), 
functions.expr("id%1000").alias("countyId"), 
split(col("GEO.display-label"),",").alias("countyState"), 
col("rescen42010").as("real2010"), 
col("respop72010").alias("est2010"), 
col("respop72011").alias("est2011"), 
col("respop72012").alias("est2012"), 
col("respop72013").alias("est2013"), 
col("respop72014").alias("est2014"), 
col("respop72015").alias("est2015"), 
col("respop72016").alias("est2016"), 
col("respop72017").alias("est2017"))

最新更新