Python Azure Databricks create delta table exception: no transaction log present

Viewed 340

I am using Azure Databricks and I am trying to create a Delta table.

Python code:

delta_save_path = "/mnt/ops/test/alerts"
try: 
  sqlContext.sql("CREATE TABLE ops.test_alerts USING DELTA LOCATION '" + delta_save_path + "'")
except Exception as e:
  print(str(e))

Error message:

You are trying to create an external table `ops`.`test_alerts`
from `/mnt/ops/test/alerts` using Databricks Delta, but there is no transaction log present at
`/mnt/ops/test/alerts/_delta_log`. Check the upstream job to make sure that it is writing using
format("delta") and that the path is the root of the table.

What am I doing wrong?

2 Answers

The syntax that you're using is when you want to create a table from the existing data. But it looks like that you're creating an empty table, so in this case you need to provide a schema for your table, like this (schema is fictious):

CREATE TABLE ops.test_alerts (
  id int,
  metric_nam string,
  timestamp timestamp
) USING DELTA 
LOCATION '<some location>'

See CREATE TABLE documentation for more details

delta_save_path = "/mnt/ops/test/alerts"

df = spark.read.format("csv").option("header", "false").load(delta_save_path)

df.write.format("delta").mode("overwrite").saveAsTable("ops.test_alerts")
Related