如何在Azure数据湖中使用Databricks将SQL表创建在项目路径内而不是外部?

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

How to create a SQL table inside the project path instead of outside in Azure datalake from databricks?

问题

I am working on a project in which I want to create a SQL table and save it to a project path. Here is how my project path looks like:

abfss://dev@xyz.dfs.core.windows.net/hkay/project_name/

When I use the below SQL code in Databricks, it saves the data in this folder instead of the project path given above. I tried using LOCATION {project_path} inside the CREATE statement but it failed due to wrong syntax.

This creates a folder in abfss://dev@xyz.dfs.core.windows.net/hkay/ outside the project path which I don't want. Any idea?

Edit 1:

This is what I was using. Not sure if the syntax is correct.

spark.sql(f"""
  CREATE {database}.table_name
  LOCATION 'abfss://dev@xyz.dfs.core.windows.net/hkay/project_name/'
  SELECT * FROM {database}.table_name_temp
  WHERE 1=0
""")
英文:

I am working on a project in which I want to create a SQL table and save it a project path. Here is how my project path looks like this.

abfss://dev@xyz.dfs.core.windows.net/hkay/project_name/

When I use the below SQL code in databricks, it saves the data in this folder instead of the project path given above. I tried using location {project_path} inside create statement but it failed due to wrong syntax.

spark.sql(f"""
  CREATE OR REPLACE TABLE  {database}.table_name
  SELECT * FROM {database}.table_name_temp
  WHERE 1=0
""")

This creates a folder in abfss://dev@xyz.dfs.core.windows.net/hkay/ outside the project path which I don't want. Any idea?

Edit 1:

This is what I was using. Not sure if the syntax is correct.

spark.sql(f"""
  CREATE {database}.table_name
  LOCATION 'abfss://dev@xyz.dfs.core.windows.net/hkay/project_name/'
  SELECT * FROM {database}.table_name_temp
  WHERE 1=0
""")

答案1

得分: 1

如果您只想创建一个具有与另一个表相同结构的空表格,那么您需要使用略有不同的语法(参见文档)- 请注意AS子句:

CREATE database.table_name
  USING delta
  LOCATION 'abfss://dev@xyz.dfs.core.windows.net/hkay/project_name/'
  AS SELECT * FROM database.table_name_temp LIMIT 0
英文:

If you just want to create an empty table with the structure as another table, then you need to use slightly different syntax (see docs) - note the AS clause:

CREATE database.table_name
  USING delta
  LOCATION 'abfss://dev@xyz.dfs.core.windows.net/hkay/project_name/'
  AS SELECT * FROM database.table_name_temp LIMIT 0

huangapple
  • 本文由 发表于 2023年2月10日 03:58:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403826.html
匿名

发表评论

匿名网友

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

确定