使用Spark SQL设置全局常量?

huangapple go评论46阅读模式
英文:

Setting global constants using Spark SQL?

问题

我在Synapse中有一个笔记本global_constant,并且我已经使用pyspark定义了一个全局常量constant= 'test':

现在我创建了另一个笔记本,我想使用SparkSQL和"SET"引用该常量。到目前为止,我已经执行了%run global_constant,但我如何引用这个常量变量?

英文:

I have a notebook global_constant in Synapse and I have defined a global constant using pyspark constant= 'test':

Now I created another notebook and I want to refer to the constant using SparkSQL with "SET". I have done %run global_constant so far but how can I refer to the constant variable?

答案1

得分: 0

AFAIK, 在 Azure Synapse 笔记本中没有全局常量。在单元格中声明的变量可以是全局变量,如果您想从一个笔记本中的另一个笔记本获取变量而不使用 %run,您可以尝试以下解决方法。

首先,在 global_constant 笔记本中创建一个临时视图,其中包含常量的值。

constant = 'Rakesh'
spark.sql("create or replace view con_view AS SELECT '{}' as const".format(constant))

然后,可以在另一个笔记本中如下访问此值。

spark.sql("set constant = (SELECT const FROM con_view)")
spark.sql("select ${constant} as con").show()

使用Spark SQL设置全局常量?

英文:

AFAIK, in there are no Global constants in Azure synapse notebooks. The variables which declared in cells can be global variables and if you want to get the variable of one notebook from another notebook without using %run, you can try this workaround.

First create a temporary view with the value of the constant in global_constant notebook.

constant='Rakesh'
spark.sql("create or replace view con_view AS SELECT '{}' as const".format(constant))

使用Spark SQL设置全局常量?

Then, access this value in another like below.

spark.sql("set constant = (SELECT const FROM con_view)")
spark.sql("select ${constant} as con").show()

使用Spark SQL设置全局常量?

huangapple
  • 本文由 发表于 2023年6月8日 19:46:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定