英文:
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()
英文:
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))
Then, access this value in another like below.
spark.sql("set constant = (SELECT const FROM con_view)")
spark.sql("select ${constant} as con").show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论