Failed to rename Dataframe column

When displaying table sections using Spark 1.6.3, I am unable to rename the Dataframe column. To reproduce the problem, I create the following partitioned table

sqlContext.sql("create table test (foo string) partitioned by (dt string)")

val df = sqlContext.sparkContext.parallelize(Seq(("foo", 1), ("bar", 2)))
  .toDF("column", "dt")

df.write.mode("overwrite").partitionBy("dt").insertInto("test")

      

I can get a list of available partitions in a table

scala> sqlContext.sql(s"show partitions test").show

+------+
|result|
+------+
|  dt=1|
|  dt=2|
+------+

      

Now I would like to rename the column using withColumnRenamed

like this

sqlContext.sql(s"show partitions test").withColumnRenamed("result", "partition").show

      

What happens with the following error message

org.apache.spark.sql.AnalysisException: Result of result attribute (s) # 835 is missing from result # 826 in statement! Project [result # 835 AS partition # 836];

I can get around this problem by using a simple alias

> sqlContext.sql(s"show partitions test").select($"result".as("partition")).show

+---------+
|partition|
+---------+
|     dt=1|
|     dt=2|
+---------+

      

But why is this happening in the first place?

+3
scala apache-spark apache-spark-sql


source to share


No one has answered this question yet

Check out similar questions:

137
How can I change column types in Spark SQL DataFrame?
63
Renaming DataFrame column names in Spark Scala
6
Spark joinWithCassandraTable () on a map partitioned key ERROR
3
flatMap Compile Error encountered: TraversableOnce [String] required: TraversableOnce [String]
1
Spark SQL ignores dynamic section filter value
0
Spark Job just stops when querying full cassandra table
0
excluding hive select query when spark creates external table using ORC format
0
Spark Dataframe connects heap issue and too many partitions
0
Add row to empty dataframe using spark scala
0
Inserting values ​​into a partitioned table with a space in the column name succeeds in spark sql



All Articles
Loading...
X
Show
Funny
Dev
Pics