public class DeltaTableBuilder
extends Object
Builder to specify how to create / replace a Delta table. You must specify the table name or the path before executing the builder. You can specify the table columns, the partitioning columns, the location of the data, the table comment and the property, and how you want to create / replace the Delta table.
After executing the builder, an instance of DeltaTable
is returned.
Scala example to create a Delta table with generated columns, using the table name:
val table: DeltaTable = DeltaTable.create()
.tableName("testTable")
.addColumn("c1", dataType = "INT", nullable = false)
.addColumn(
DeltaTable.columnBuilder("c2")
.dataType("INT")
.generatedAlwaysAs("c1 + 10")
.build()
)
.addColumn(
DeltaTable.columnBuilder("c3")
.dataType("INT")
.comment("comment")
.nullable(true)
.build()
)
.partitionedBy("c1", "c2")
.execute()
Scala example to create a delta table using the location:
val table: DeltaTable = DeltaTable.createIfNotExists(spark)
.location("/foo/`bar`")
.addColumn("c1", dataType = "INT", nullable = false)
.addColumn(
DeltaTable.columnBuilder(spark, "c2")
.dataType("INT")
.generatedAlwaysAs("c1 + 10")
.build()
)
.addColumn(
DeltaTable.columnBuilder(spark, "c3")
.dataType("INT")
.comment("comment")
.nullable(true)
.build()
)
.partitionedBy("c1", "c2")
.execute()
Java Example to replace a table:
DeltaTable table = DeltaTable.replace()
.tableName("db.table")
.addColumn("c1", "INT", false)
.addColumn(
DeltaTable.columnBuilder("c2")
.dataType("INT")
.generatedAlwaysBy("c1 + 10")
.build()
)
.execute();
Modifier and Type | Method and Description |
---|---|
DeltaTableBuilder |
addColumn(String colName,
org.apache.spark.sql.types.DataType dataType)
:: Evolving ::
|
DeltaTableBuilder |
addColumn(String colName,
org.apache.spark.sql.types.DataType dataType,
boolean nullable)
:: Evolving ::
|
DeltaTableBuilder |
addColumn(String colName,
String dataType)
:: Evolving ::
|
DeltaTableBuilder |
addColumn(String colName,
String dataType,
boolean nullable)
:: Evolving ::
|
DeltaTableBuilder |
addColumn(org.apache.spark.sql.types.StructField col)
:: Evolving ::
|
DeltaTableBuilder |
addColumns(org.apache.spark.sql.types.StructType cols)
:: Evolving ::
|
DeltaTableBuilder |
clusterBy(scala.collection.Seq<String> colNames)
:: Evolving ::
|
DeltaTableBuilder |
clusterBy(String... colNames)
:: Evolving ::
|
DeltaTableBuilder |
comment(String comment)
:: Evolving ::
|
DeltaTable |
execute()
:: Evolving ::
|
DeltaTableBuilder |
location(String location)
:: Evolving ::
|
DeltaTableBuilder |
partitionedBy(scala.collection.Seq<String> colNames)
:: Evolving ::
|
DeltaTableBuilder |
partitionedBy(String... colNames)
:: Evolving ::
|
DeltaTableBuilder |
property(String key,
String value)
:: Evolving ::
|
DeltaTableBuilder |
tableName(String identifier)
:: Evolving ::
|
public DeltaTableBuilder addColumn(String colName, String dataType)
Specify a column.
colName
- string the column namedataType
- string the DDL data typepublic DeltaTableBuilder addColumn(String colName, org.apache.spark.sql.types.DataType dataType)
Specify a column.
colName
- string the column namedataType
- dataType the DDL data typepublic DeltaTableBuilder addColumn(String colName, String dataType, boolean nullable)
Specify a column.
colName
- string the column namedataType
- string the DDL data typenullable
- boolean whether the column is nullablepublic DeltaTableBuilder addColumn(String colName, org.apache.spark.sql.types.DataType dataType, boolean nullable)
Specify a column.
colName
- string the column namedataType
- dataType the DDL data typenullable
- boolean whether the column is nullablepublic DeltaTableBuilder addColumn(org.apache.spark.sql.types.StructField col)
Specify a column.
col
- structField the column structpublic DeltaTableBuilder addColumns(org.apache.spark.sql.types.StructType cols)
Specify columns with an existing schema.
cols
- structType the existing schema for columnspublic DeltaTableBuilder clusterBy(String... colNames)
Specify the columns to cluster the output on the file system.
Note: This should only include table columns already defined in schema.
colNames
- string* column names for clusteringpublic DeltaTableBuilder clusterBy(scala.collection.Seq<String> colNames)
Specify the columns to cluster the output on the file system.
Note: This should only include table columns already defined in schema.
colNames
- string* column names for clusteringpublic DeltaTableBuilder comment(String comment)
Specify the table comment to describe the table.
comment
- string table commentpublic DeltaTable execute()
Execute the command to create / replace a Delta table and returns a instance of DeltaTable
.
public DeltaTableBuilder location(String location)
Specify the path to the directory where table data is stored, which could be a path on distributed storage.
location
- string the data locationpublic DeltaTableBuilder partitionedBy(String... colNames)
Specify the columns to partition the output on the file system.
Note: This should only include table columns already defined in schema.
colNames
- string* column names for partitioningpublic DeltaTableBuilder partitionedBy(scala.collection.Seq<String> colNames)
Specify the columns to partition the output on the file system.
Note: This should only include table columns already defined in schema.
colNames
- string* column names for partitioningpublic DeltaTableBuilder property(String key, String value)
Specify a key-value pair to tag the table definition.
key
- string the table property keyvalue
- string the table property valuepublic DeltaTableBuilder tableName(String identifier)
Specify the table name, optionally qualified with a database name [database_name.] table_name
identifier
- string the table name