- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2023 12:04 AM
@Daniel Bedrenko :
You can create Unity Catalog tables and views via Terraform using the
databricks_sql_script resource. With this resource, you can define the SQL script that creates the table or view and run it via Terraform. Here is an example of how you can create a view using this resource:
resource "databricks_sql_script" "example_view" {
database = "example_db"
content = "CREATE VIEW example_view AS SELECT * FROM example_table WHERE id > 10;"
}In this example, we are creating a view called example_view in the example_db database, and the view selects all rows from example_table where the id column is greater than 10.
Alternatively, if you have data defined in TF files that needs to be exposed to the Databricks workspace, you can use the databricks_notebook resource to create a Python or SQL notebook that includes the data and creates the view in the workspace. Here is an example of how you can create a Python notebook that creates a view:
resource "databricks_notebook" "example_notebook" {
name = "example_notebook"
language = "PYTHON"
content_base64 = base64encode(templatefile("example_notebook.py.tpl", {
example_data = var.example_data
}))
}
data "template_file" "example_notebook_py" {
template = <<EOF
# MAGIC %sql
from pyspark.sql.functions import *
from pyspark.sql.types import *
example_data = ${jsonencode(var.example_data)}
df = spark.createDataFrame(example_data, [
StructField("id", IntegerType(), True),
StructField("name", StringType(), True),
StructField("age", IntegerType(), True)
])
df.createOrReplaceTempView("example_view")
EOF
vars = {
example_data = var.example_data
}
}
variable "example_data" {
type = any
}
In this example, we are creating a Python notebook called example_notebook that takes in a variable called example_data, which contains the data that needs to be exposed to the workspace. The
content_base64 field contains the base64-encoded contents of the notebook, which is generated from a template file. The example_notebook.py.tpl template file contains the Python code that creates the view using the data from the example_data variable.
I hope this helps, and let me know if you have any further questions!