-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Adds support to the iceberg input source to read from Iceberg REST Catalogs. Co-authored-by: Atul Mohan <[email protected]>
- Loading branch information
1 parent
b7cc0bb
commit 0ae9988
Showing
9 changed files
with
235 additions
and
16 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
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
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
108 changes: 108 additions & 0 deletions
108
...d-iceberg-extensions/src/main/java/org/apache/druid/iceberg/input/RestIcebergCatalog.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,108 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.druid.iceberg.input; | ||
|
||
import com.fasterxml.jackson.annotation.JacksonInject; | ||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.druid.error.InvalidInput; | ||
import org.apache.druid.guice.annotations.Json; | ||
import org.apache.druid.iceberg.guice.HiveConf; | ||
import org.apache.druid.utils.DynamicConfigProviderUtils; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.iceberg.CatalogProperties; | ||
import org.apache.iceberg.catalog.Catalog; | ||
import org.apache.iceberg.catalog.SessionCatalog; | ||
import org.apache.iceberg.rest.HTTPClient; | ||
import org.apache.iceberg.rest.RESTCatalog; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.Map; | ||
|
||
/** | ||
* Catalog implementation for Iceberg REST catalogs. | ||
*/ | ||
public class RestIcebergCatalog extends IcebergCatalog | ||
{ | ||
public static final String TYPE_KEY = "rest"; | ||
|
||
@JsonProperty | ||
private final String catalogUri; | ||
|
||
@JsonProperty | ||
private final Map<String, String> catalogProperties; | ||
|
||
private final Configuration configuration; | ||
|
||
private Catalog restCatalog; | ||
|
||
@JsonCreator | ||
public RestIcebergCatalog( | ||
@JsonProperty("catalogUri") String catalogUri, | ||
@JsonProperty("catalogProperties") @Nullable | ||
Map<String, Object> catalogProperties, | ||
@JacksonInject @Json ObjectMapper mapper, | ||
@JacksonInject @HiveConf Configuration configuration | ||
) | ||
{ | ||
if (catalogUri == null) { | ||
throw InvalidInput.exception("catalogUri cannot be null"); | ||
} | ||
this.catalogUri = catalogUri; | ||
this.catalogProperties = DynamicConfigProviderUtils.extraConfigAndSetStringMap( | ||
catalogProperties, | ||
DRUID_DYNAMIC_CONFIG_PROVIDER_KEY, | ||
mapper | ||
); | ||
this.configuration = configuration; | ||
} | ||
|
||
@Override | ||
public Catalog retrieveCatalog() | ||
{ | ||
if (restCatalog == null) { | ||
restCatalog = setupCatalog(); | ||
} | ||
return restCatalog; | ||
} | ||
|
||
public String getCatalogUri() | ||
{ | ||
return catalogUri; | ||
} | ||
|
||
public Map<String, String> getCatalogProperties() | ||
{ | ||
return catalogProperties; | ||
} | ||
|
||
private RESTCatalog setupCatalog() | ||
{ | ||
RESTCatalog restCatalog = new RESTCatalog( | ||
SessionCatalog.SessionContext.createEmpty(), | ||
config -> HTTPClient.builder(config).uri(config.get(CatalogProperties.URI)).build() | ||
); | ||
restCatalog.setConf(configuration); | ||
catalogProperties.put(CatalogProperties.URI, catalogUri); | ||
restCatalog.initialize("rest", catalogProperties); | ||
return restCatalog; | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...ruid-iceberg-extensions/src/test/java/org/apache/druid/iceberg/input/RestCatalogTest.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,99 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.druid.iceberg.input; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.net.HttpHeaders; | ||
import com.sun.net.httpserver.HttpServer; | ||
import org.apache.druid.jackson.DefaultObjectMapper; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.iceberg.rest.RESTCatalog; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
import java.net.ServerSocket; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
|
||
public class RestCatalogTest | ||
{ | ||
private final ObjectMapper mapper = new DefaultObjectMapper(); | ||
private int port = 0; | ||
private HttpServer server = null; | ||
private ServerSocket serverSocket = null; | ||
|
||
@Before | ||
public void setup() throws Exception | ||
{ | ||
serverSocket = new ServerSocket(0); | ||
port = serverSocket.getLocalPort(); | ||
serverSocket.close(); | ||
server = HttpServer.create(new InetSocketAddress("localhost", port), 0); | ||
server.createContext( | ||
"/v1/config", // API for catalog fetchConfig which is invoked on catalog initialization | ||
(httpExchange) -> { | ||
String payload = "{}"; | ||
byte[] outputBytes = payload.getBytes(StandardCharsets.UTF_8); | ||
httpExchange.sendResponseHeaders(200, outputBytes.length); | ||
OutputStream os = httpExchange.getResponseBody(); | ||
httpExchange.getResponseHeaders().set(HttpHeaders.CONTENT_TYPE, "application/octet-stream"); | ||
httpExchange.getResponseHeaders().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(outputBytes.length)); | ||
httpExchange.getResponseHeaders().set(HttpHeaders.CONTENT_RANGE, "bytes 0"); | ||
os.write(outputBytes); | ||
os.close(); | ||
} | ||
); | ||
server.start(); | ||
} | ||
|
||
@Test | ||
public void testCatalogCreate() | ||
{ | ||
String catalogUri = "http://localhost:" + port; | ||
|
||
RestIcebergCatalog testRestCatalog = new RestIcebergCatalog( | ||
catalogUri, | ||
new HashMap<>(), | ||
mapper, | ||
new Configuration() | ||
); | ||
RESTCatalog innerCatalog = (RESTCatalog) testRestCatalog.retrieveCatalog(); | ||
|
||
Assert.assertEquals("rest", innerCatalog.name()); | ||
Assert.assertNotNull(innerCatalog.properties()); | ||
Assert.assertNotNull(testRestCatalog.getCatalogProperties()); | ||
Assert.assertEquals(testRestCatalog.getCatalogUri(), innerCatalog.properties().get("uri")); | ||
} | ||
@After | ||
public void tearDown() throws IOException | ||
{ | ||
if (server != null) { | ||
server.stop(0); | ||
} | ||
if (serverSocket != null) { | ||
serverSocket.close(); | ||
} | ||
} | ||
} |