Ken*_*war 2 sql apache-spark apache-spark-sql
不确定为什么转义的单引号没有出现在 SQL 输出中。最初在 Jupyter Notebook 中尝试过此操作,但在下面的 PySpark shell 中重现了它。
$ pyspark
SPARK_MAJOR_VERSION is set to 2, using Spark2
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Using Python version 3.6.3 (default, Oct 13 2017 12:02:49)
SparkSession available as 'spark'.
>>> spark.version
'2.3.0.2.6.5.0-292'
>>> spark.sql("select 'This is Bob''s home' as column1").show(truncate=False)
+-----------------+
| column1|
+-----------------+
|This is Bobs home|
+-----------------+
Run Code Online (Sandbox Code Playgroud)
输出显示Bobs home而不是Bob's home
使用反斜杠而不是单引号来转义单引号:
spark.sql("select 'This is Bob\\'s home' as column1").show()
+------------------+
| column1|
+------------------+
|This is Bob's home|
+------------------+
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用双引号将字符串括起来,这样就不需要转义单引号:
spark.sql("""select "This is Bob's home" as column1""").show()
+------------------+
| column1|
+------------------+
|This is Bob's home|
+------------------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4211 次 |
| 最近记录: |