如何将具有特殊字符的列名别名为带下划线而不是特殊字符的新列名?

时间:2020-06-13 17:01:07

标签: apache-spark-sql databricks

我有一个列名称为“ product / price”的表,我想在sparksql中创建现有表的副本时将其重命名为product_price。

创建表newtable作为选择产品/价格,并将其作为product_price 现有表格

这给了我错误“分析异常错误:无法解析'产品'

我也尝试过如下操作:

创建表newtable作为select'product / price'作为product_price 从现有表中

这确实会更改列名,但其中的所有值也会更改为product_price。

1 个答案:

答案 0 :(得分:0)

  // select `product/price` as product_price .... to your column ?
  //

  val d1 = Seq("test", "Test1", "test2").toDF("product/price")
  d1.show(false)
  //      +-------------+
  //      |product/price|
  //      +-------------+
  //      |test         |
  //      |Test1        |
  //      |test2        |
  //      +-------------+

  val d2 = d1.withColumnRenamed("product/price", "product_price")
  d2.show(false)
  //      +-------------+
  //      |product_price|
  //      +-------------+
  //      |test         |
  //      |Test1        |
  //      |test2        |
  //      +-------------+
相关问题