英文:
How to insert variable into methods with Databricks using PySpark
问题
The title of this question is terrible, as I couldn't find the correct words to describe exactly what I would like in a sentence.
Anyway, I have a variable which returns two values separated by a comma, see below:
At the moment I'm reading in a database in Databricks as follows, df = spark.table("samples.cdcTest.cdcmergetest")
I would like to use the variable 'regName' instead of typing cdcTest.cdcmergetest.
I have tried the following:
df = spark.table("samples".regName)
But I get the error AttributeError: 'str' object has no attribute 'regName'
I also tried the following
This looks like an indexing and slicing issue, but not sure
Ok, I've tried a few things that appear to be getting me closer to my goal see below
The problem I have is combining all the parts together:
df = spark.table("samples", db_name, table_name)
Any thoughts much appreciated.
英文:
The title of this question is terrible, as I couldn't find the correct words to describe exactly what I would like in a sentence.
Anyway, I have a variable which returns two values separated by a comma, see below:
At the moment I'm reading in a database in Databricks as follows, df = spark.table("samples.cdcTest.cdcmergetest")
I would like to use the variable 'regName' instead of typing cdcTest.cdcmergetest.
I have tried the followiong:
df = spark.table("samples".regName)
But I get the error AttributeError: 'str' object has no attribute 'regName'
I also trid the following
This looks like an indexing and slicing issue, but not sure
Ok, I've tried a few things that appear to be getting me closer to my goal see below
The problem I have is combining all the parts together:
df = spark.table("samples",db_name,table_name)
Any thoughts much appreciated.
答案1
得分: 1
以下是翻译好的部分:
- Just concatenate names:
"samples" + "." + regName
-> 只需连接名称:"samples" + "." + regName
- Use f-strings
f"samples.{regName}"
-> 使用 f-stringsf"samples.{regName}"
- join the list:
".".join(["samples",db_name,table_name])
-> 连接列表:".".join(["samples",db_name,table_name])
英文:
You have multiple possibilities to generate a full name:
- Just concatenate names:
"samples" + "." + regName
- Use f-strings `f"samples.{regName}"
- join the list:
".".join(["samples",db_name,table_name])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论