选择除Spark SQL中特定列之外的所有列

Pat*_*tel 2 hiveql apache-spark-sql pyspark-sql spark-hive

我想选择表中除StudentAddress之外的所有列,因此我编写了以下查询:

select `(StudentAddress)?+.+` from student;
Run Code Online (Sandbox Code Playgroud)

它在Squirrel Sql客户端中给出以下错误。org.apache.spark.sql.AnalysisException:无法解析(StudentAddress)?+.+给定的输入列

Shu*_*Shu 10

使用spark sql尝试与

select * except(<columns to be excluded>) from tablename

Example:

select * from tmp
#+----+----+----+----+
#|col1|col2|col3|col4|
#+----+----+----+----+
#|a   |b   |c   |d   |
#+----+----+----+----+


#exclude col1,col2
select * except(col1,col2) from table_name
#+----+----+
#|col3|col4|
#+----+----+
#|c   |d   |
#+----+----+
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以在DataFrame API中使用drop()方法删除特定的列,然后选择所有列。

例如:

val df = hiveContext.read.table("student")
val dfWithoutStudentAddress = df.drop("StudentAddress")
Run Code Online (Sandbox Code Playgroud)

  • 删除后不需要select(“ *”)。 (2认同)