Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to express a column which name contains spaces in Spark SQL

Tags:

We have tried wrapping the column name with brackets [column name], single & double quotes, and backticks, none of them works.

Does Spark SQL support columns whose name contains spaces?

like image 480
DarkZero Avatar asked Oct 10 '15 10:10

DarkZero


People also ask

How do you query column names with spaces in SQL?

To select a column name with spaces, use the back tick symbol with column name. The symbol is ( ` `). Back tick is displayed in the keyboard below the tilde operator ( ~).

Can SQL column names have spaces?

Column names can contain any valid characters (for example, spaces).

How do you remove spaces from column names in PySpark?

To Remove both leading and trailing space of the column in pyspark we use trim() function. trim() Function takes column name and trims both left and right white space from that column.

How do I select a specific column in Spark SQL?

In Spark SQL, select () function is used to select one or multiple columns, nested columns, column by index, all columns, from the list, by regular expression from a DataFrame. select () is a transformation function in Spark and returns a new DataFrame with the selected columns. You can also alias column names while selecting.

How to select a column name with spaces in Excel?

To select a column name with spaces, use the back tick symbol with column name. The symbol is ( ` `). Back tick is displayed in the keyboard below the tilde operator ( ~).

How to get column name with space in MySQL?

The syntax to get column name with space is as follows −. SELECT `column_name` from yourTableName; Now I will apply the above syntax to get the result for my column. The query is as follows −. mysql> SELECT `Student Name` from SpaceColumn; The following is the output −.

How to access pyspark/spark dataframe column name with a dot?

In order to access PySpark/Spark DataFrame Column Name with a dot from wihtColumn () & select (), you just need to enclose the column name with backticks (`) Using Column Name with Dot on select (). Using Column Name with Dot on withColumn ()


2 Answers

Backticks seem to work just fine:

scala> val df = sc.parallelize(Seq(("a", 1))).toDF("foo bar", "x") df: org.apache.spark.sql.DataFrame = [foo bar: string, x: int] scala> df.registerTempTable("df")  scala> sqlContext.sql("""SELECT `foo bar` FROM df""").show foo bar a   

Same as DataFrame API:

scala> df.select($"foo bar").show foo bar a    

So it looks like it is supported, although I doubt it is recommended.

like image 115
zero323 Avatar answered Sep 18 '22 13:09

zero323


Instead of using Brackets like in T-SQL [column name] Use backticks to wrap the column name `column name`. This is when you run SQL. You also use Backticks in spark SQL to wrap the column name but use triple quotes as answered by zero323.

like image 31
SaadK Avatar answered Sep 18 '22 13:09

SaadK