在Databricks中創(chuàng)建SQL表,您需要遵循以下步驟:
from pyspark.sql import SparkSession
# 創(chuàng)建一個(gè)Spark會話
spark = SparkSession.builder \
.appName("Create SQL Table in Databricks") \
.getOrCreate()
# 創(chuàng)建一個(gè)DataFrame
data = [("Alice", 1), ("Bob", 2), ("Cathy", 3)]
columns = ["name", "id"]
df = spark.createDataFrame(data, columns)
# 將DataFrame注冊為臨時(shí)表
df.createOrReplaceTempView("people")
# 使用SQL查詢創(chuàng)建一個(gè)新表
spark.sql("""
CREATE TABLE IF NOT EXISTS people_table
USING parquet
OPTIONS ('compression'='snappy')
AS SELECT * FROM people
""")
# 顯示新創(chuàng)建的表
spark.sql("SELECT * FROM people_table").show()
people_table
的SQL表,并使用people
臨時(shí)表中的數(shù)據(jù)填充它。spark.sql("DESCRIBE people_table").show()
spark.sql("SELECT * FROM people_table").show()
現(xiàn)在,您已經(jīng)在Databricks中創(chuàng)建了一個(gè)SQL表。請根據(jù)需要修改代碼示例以適應(yīng)您的數(shù)據(jù)和需求。