-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support JDBC catalog in Iceberg connector
- Loading branch information
Showing
29 changed files
with
1,739 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,6 @@ public enum CatalogType | |
HIVE_METASTORE, | ||
GLUE, | ||
REST, | ||
JDBC, | ||
/**/; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...-iceberg/src/main/java/io/trino/plugin/iceberg/catalog/jdbc/IcebergJdbcCatalogModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.plugin.iceberg.catalog.jdbc; | ||
|
||
import com.google.inject.Binder; | ||
import com.google.inject.Provides; | ||
import com.google.inject.Scopes; | ||
import com.google.inject.Singleton; | ||
import io.airlift.configuration.AbstractConfigurationAwareModule; | ||
import io.trino.plugin.iceberg.catalog.IcebergTableOperationsProvider; | ||
import io.trino.plugin.iceberg.catalog.TrinoCatalogFactory; | ||
|
||
import static io.airlift.configuration.ConfigBinder.configBinder; | ||
import static org.weakref.jmx.guice.ExportBinder.newExporter; | ||
|
||
public class IcebergJdbcCatalogModule | ||
extends AbstractConfigurationAwareModule | ||
{ | ||
@Override | ||
protected void setup(Binder binder) | ||
{ | ||
configBinder(binder).bindConfig(IcebergJdbcConfig.class); | ||
binder.bind(IcebergTableOperationsProvider.class).to(IcebergJdbcTableOperationsProvider.class).in(Scopes.SINGLETON); | ||
newExporter(binder).export(IcebergTableOperationsProvider.class).withGeneratedName(); | ||
binder.bind(TrinoCatalogFactory.class).to(TrinoJdbcCatalogFactory.class).in(Scopes.SINGLETON); | ||
binder.bind(TrinoJdbcCatalogFactory.class); | ||
newExporter(binder).export(TrinoJdbcCatalogFactory.class).withGeneratedName(); | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
public static IcebergJdbcClient createIcebergJdbcClient(IcebergJdbcConfig config) | ||
{ | ||
return new IcebergJdbcClient( | ||
new IcebergJdbcConnectionFactory(config.getConnectionUrl()), | ||
config.getCatalogName()); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...n/trino-iceberg/src/main/java/io/trino/plugin/iceberg/catalog/jdbc/IcebergJdbcClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.plugin.iceberg.catalog.jdbc; | ||
|
||
import org.apache.iceberg.exceptions.CommitFailedException; | ||
import org.jdbi.v3.core.Handle; | ||
import org.jdbi.v3.core.Jdbi; | ||
|
||
import java.util.Optional; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class IcebergJdbcClient | ||
{ | ||
private final IcebergJdbcConnectionFactory connectionFactory; | ||
private final String catalogName; | ||
|
||
public IcebergJdbcClient(IcebergJdbcConnectionFactory connectionFactory, String catalogName) | ||
{ | ||
this.connectionFactory = requireNonNull(connectionFactory, "connectionFactory is null"); | ||
this.catalogName = requireNonNull(catalogName, "catalogName is null"); | ||
} | ||
|
||
public void createTable(String schemaName, String tableName, String metadataLocation) | ||
{ | ||
try (Handle handle = Jdbi.open(connectionFactory)) { | ||
handle.createUpdate("" + | ||
"INSERT INTO iceberg_tables " + | ||
"(catalog_name, table_namespace, table_name, metadata_location, previous_metadata_location) " + | ||
"VALUES (:catalog, :schema, :table, :metadata_location, null)") | ||
.bind("catalog", catalogName) | ||
.bind("schema", schemaName) | ||
.bind("table", tableName) | ||
.bind("metadata_location", metadataLocation) | ||
.execute(); | ||
} | ||
} | ||
|
||
public void alterTable(String schemaName, String tableName, String newMetadataLocation, String previousMetadataLocation) | ||
{ | ||
try (Handle handle = Jdbi.open(connectionFactory)) { | ||
int updatedRecords = handle.createUpdate("" + | ||
"UPDATE iceberg_tables " + | ||
"SET metadata_location = :metadata_location, previous_metadata_location = :previous_metadata_location " + | ||
"WHERE catalog_name = :catalog AND table_namespace = :schema AND table_name = :table AND metadata_location = :previous_metadata_location") | ||
.bind("metadata_location", newMetadataLocation) | ||
.bind("previous_metadata_location", previousMetadataLocation) | ||
.bind("catalog", catalogName) | ||
.bind("schema", schemaName) | ||
.bind("table", tableName) | ||
.execute(); | ||
if (updatedRecords != 1) { | ||
throw new CommitFailedException("Failed to update table due to concurrent updates"); | ||
} | ||
} | ||
} | ||
|
||
public Optional<String> getMetadataLocation(String schemaName, String tableName) | ||
{ | ||
try (Handle handle = Jdbi.open(connectionFactory)) { | ||
return handle.createQuery("" + | ||
"SELECT metadata_location " + | ||
"FROM iceberg_tables " + | ||
"WHERE catalog_name = :catalog AND table_namespace = :schema AND table_name = :table") | ||
.bind("catalog", catalogName) | ||
.bind("schema", schemaName) | ||
.bind("table", tableName) | ||
.mapTo(String.class) | ||
.findOne(); | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...n/trino-iceberg/src/main/java/io/trino/plugin/iceberg/catalog/jdbc/IcebergJdbcConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.plugin.iceberg.catalog.jdbc; | ||
|
||
import io.airlift.configuration.Config; | ||
import io.airlift.configuration.ConfigDescription; | ||
import io.airlift.configuration.ConfigSecuritySensitive; | ||
|
||
import javax.validation.constraints.NotEmpty; | ||
|
||
public class IcebergJdbcConfig | ||
{ | ||
private String connectionUrl; | ||
private String catalogName; | ||
private String defaultWarehouseDir; | ||
|
||
public String getConnectionUrl() | ||
{ | ||
return connectionUrl; | ||
} | ||
|
||
@Config("iceberg.jdbc-catalog.connection-url") | ||
@ConfigDescription("The URI to connect to the JDBC server") | ||
@ConfigSecuritySensitive | ||
public IcebergJdbcConfig setConnectionUrl(String connectionUrl) | ||
{ | ||
this.connectionUrl = connectionUrl; | ||
return this; | ||
} | ||
|
||
@NotEmpty | ||
public String getCatalogName() | ||
{ | ||
return catalogName; | ||
} | ||
|
||
@Config("iceberg.jdbc-catalog.catalog-name") | ||
@ConfigDescription("Iceberg JDBC metastore catalog name") | ||
public IcebergJdbcConfig setCatalogName(String catalogName) | ||
{ | ||
this.catalogName = catalogName; | ||
return this; | ||
} | ||
|
||
@NotEmpty | ||
public String getDefaultWarehouseDir() | ||
{ | ||
return defaultWarehouseDir; | ||
} | ||
|
||
@Config("iceberg.jdbc-catalog.default-warehouse-dir") | ||
@ConfigDescription("The default warehouse directory to use for JDBC") | ||
public IcebergJdbcConfig setDefaultWarehouseDir(String defaultWarehouseDir) | ||
{ | ||
this.defaultWarehouseDir = defaultWarehouseDir; | ||
return this; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...berg/src/main/java/io/trino/plugin/iceberg/catalog/jdbc/IcebergJdbcConnectionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.plugin.iceberg.catalog.jdbc; | ||
|
||
import org.jdbi.v3.core.ConnectionFactory; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class IcebergJdbcConnectionFactory | ||
implements ConnectionFactory | ||
{ | ||
private final String connectionUrl; | ||
|
||
public IcebergJdbcConnectionFactory(String connectionUrl) | ||
{ | ||
this.connectionUrl = requireNonNull(connectionUrl, "connectionUrl is null"); | ||
} | ||
|
||
@Override | ||
public Connection openConnection() | ||
throws SQLException | ||
{ | ||
Connection connection = DriverManager.getConnection(connectionUrl); | ||
checkState(connection != null, "Driver returned null connection, make sure the connection URL is valid"); | ||
return connection; | ||
} | ||
} |
Oops, something went wrong.