Skip to content

Commit 0a8cc0e

Browse files
committed
add lance-hive catalog
1 parent 734b96a commit 0a8cc0e

11 files changed

Lines changed: 436 additions & 180 deletions

File tree

‎amoro-ams/src/main/java/org/apache/amoro/server/catalog/CatalogBuilder.java‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public class CatalogBuilder {
6868
TableFormat.MIXED_ICEBERG,
6969
TableFormat.MIXED_HIVE,
7070
TableFormat.PAIMON,
71-
TableFormat.HUDI),
71+
TableFormat.HUDI,
72+
TableFormat.LANCE),
7273
CATALOG_TYPE_AMS,
7374
Sets.newHashSet(TableFormat.ICEBERG, TableFormat.MIXED_ICEBERG));
7475

@@ -86,7 +87,7 @@ public static ServerCatalog buildServerCatalog(
8687
"Table format %s is not supported for metastore type: %s",
8788
tableFormats,
8889
type);
89-
checkRestLanceExclusivity(type, tableFormats);
90+
checkLanceExclusivity(type, tableFormats);
9091

9192
switch (type) {
9293
case CATALOG_TYPE_HADOOP:
@@ -104,13 +105,14 @@ public static ServerCatalog buildServerCatalog(
104105
}
105106

106107
/** Enforced on both catalog creation and metadata updates. */
107-
static void checkRestLanceExclusivity(String type, Set<TableFormat> tableFormats) {
108+
static void checkLanceExclusivity(String type, Set<TableFormat> tableFormats) {
109+
boolean lanceExclusiveType = CATALOG_TYPE_REST.equals(type) || CATALOG_TYPE_HIVE.equals(type);
108110
Preconditions.checkState(
109-
!(CATALOG_TYPE_REST.equals(type)
111+
!(lanceExclusiveType
110112
&& tableFormats.contains(TableFormat.LANCE)
111113
&& tableFormats.size() > 1),
112-
"REST catalog serves a single protocol per uri,"
113-
+ " Lance cannot be combined with other table formats: %s",
114+
"Lance cannot be combined with other table formats in a %s catalog: %s",
115+
type,
114116
tableFormats);
115117
}
116118
}

‎amoro-ams/src/main/java/org/apache/amoro/server/catalog/DefaultCatalogManager.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,10 @@ private void validateCatalogUpdate(CatalogMeta oldMeta, CatalogMeta newMeta) {
276276
throw new IllegalMetadataException("Cannot update catalog type");
277277
}
278278

279-
// A REST catalog serves a single protocol per uri, so Lance cannot be combined with other
280-
// table formats. Reject the update before it is persisted.
279+
// Lance cannot mix with other table formats in REST/Hive catalogs; reject before persisting.
281280
String type = newMeta.getCatalogType();
282281
Set<TableFormat> tableFormats = CatalogUtil.tableFormats(newMeta);
283-
CatalogBuilder.checkRestLanceExclusivity(type, tableFormats);
282+
CatalogBuilder.checkLanceExclusivity(type, tableFormats);
284283
}
285284

286285
private void disposeCatalog(String name) {

‎amoro-ams/src/main/java/org/apache/amoro/server/dashboard/controller/CatalogController.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ public class CatalogController {
217217
CatalogDescriptor.of(CATALOG_TYPE_REST, STORAGE_CONFIGS_VALUE_TYPE_HADOOP, LANCE));
218218
VALIDATE_CATALOGS.add(
219219
CatalogDescriptor.of(CATALOG_TYPE_REST, STORAGE_CONFIGS_VALUE_TYPE_OSS, LANCE));
220+
221+
VALIDATE_CATALOGS.add(
222+
CatalogDescriptor.of(CATALOG_TYPE_HIVE, STORAGE_CONFIGS_VALUE_TYPE_S3, LANCE));
223+
VALIDATE_CATALOGS.add(
224+
CatalogDescriptor.of(CATALOG_TYPE_HIVE, STORAGE_CONFIGS_VALUE_TYPE_OSS, LANCE));
225+
VALIDATE_CATALOGS.add(
226+
CatalogDescriptor.of(CATALOG_TYPE_HIVE, STORAGE_CONFIGS_VALUE_TYPE_LOCAL, LANCE));
220227
}
221228

222229
private final PlatformFileManager platformFileInfoService;

‎amoro-common/src/main/java/org/apache/amoro/utils/CatalogUtil.java‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,9 @@ public static Map<String, String> mergeCatalogPropertiesToTable(
121121

122122
/** Build {@link TableMetaStore} from catalog meta. */
123123
public static TableMetaStore buildMetaStore(CatalogMeta catalogMeta) {
124-
// REST + Lance manages storage/auth server-side, so a client-side TableMetaStore is
125-
// unnecessary. Set a local configuration so build() returns a disabled-auth store.
126-
if (CatalogMetaProperties.CATALOG_TYPE_REST.equalsIgnoreCase(catalogMeta.getCatalogType())
127-
&& isLanceOnly(catalogMeta)) {
128-
return TableMetaStore.builder().withConfiguration(new Configuration()).build();
124+
// Lance over REST/Hive catalog authenticates on its own; EMPTY is a disabled-auth store.
125+
if (isLanceManagedExternally(catalogMeta)) {
126+
return TableMetaStore.EMPTY;
129127
}
130128

131129
// load storage configs
@@ -222,9 +220,25 @@ && isLanceOnly(catalogMeta)) {
222220
return builder.build();
223221
}
224222

225-
private static boolean isLanceOnly(CatalogMeta catalogMeta) {
223+
private static boolean isLanceManagedExternally(CatalogMeta catalogMeta) {
226224
Set<TableFormat> formats = tableFormats(catalogMeta);
227-
return formats.size() == 1 && formats.contains(TableFormat.LANCE);
225+
if (formats.size() != 1 || !formats.contains(TableFormat.LANCE)) {
226+
return false;
227+
}
228+
229+
String type = catalogMeta.getCatalogType();
230+
Map<String, String> authConfigs = catalogMeta.getAuthConfigs();
231+
if (CatalogMetaProperties.CATALOG_TYPE_HIVE.equalsIgnoreCase(type)
232+
&& authConfigs != null
233+
&& CatalogMetaProperties.AUTH_CONFIGS_VALUE_TYPE_KERBEROS.equalsIgnoreCase(
234+
authConfigs.get(CatalogMetaProperties.AUTH_CONFIGS_KEY_TYPE))) {
235+
throw new IllegalArgumentException(
236+
"Kerberos auth is not supported for Lance catalogs backed by a Hive metastore: "
237+
+ catalogMeta.getCatalogName());
238+
}
239+
240+
return CatalogMetaProperties.CATALOG_TYPE_REST.equalsIgnoreCase(type)
241+
|| CatalogMetaProperties.CATALOG_TYPE_HIVE.equalsIgnoreCase(type);
228242
}
229243

230244
public static TableIdentifier tableId(TableMeta tableMeta) {

‎amoro-format-lance/pom.xml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,41 @@
7171
<artifactId>lance-namespace-apache-client</artifactId>
7272
<version>0.8.0</version>
7373
</dependency>
74+
75+
<!-- Lance namespace Hive 3.x metastore implementation -->
76+
<dependency>
77+
<groupId>org.lance</groupId>
78+
<artifactId>lance-namespace-hive3</artifactId>
79+
<version>0.4.1</version>
80+
<exclusions>
81+
<exclusion>
82+
<groupId>org.lance</groupId>
83+
<artifactId>lance-namespace-core</artifactId>
84+
</exclusion>
85+
<exclusion>
86+
<groupId>io.netty</groupId>
87+
<artifactId>netty</artifactId>
88+
</exclusion>
89+
<exclusion>
90+
<groupId>io.netty</groupId>
91+
<artifactId>netty-all</artifactId>
92+
</exclusion>
93+
<!-- amoro-common already provides orc 1.9.7 (nohive) and hadoop-client-api, whose
94+
unshaded 1.5.1 / 2.x counterparts only add duplicate org.apache.hadoop classes. -->
95+
<exclusion>
96+
<groupId>org.apache.orc</groupId>
97+
<artifactId>orc-core</artifactId>
98+
</exclusion>
99+
<exclusion>
100+
<groupId>org.apache.hadoop</groupId>
101+
<artifactId>hadoop-common</artifactId>
102+
</exclusion>
103+
<exclusion>
104+
<groupId>org.apache.hadoop</groupId>
105+
<artifactId>hadoop-hdfs</artifactId>
106+
</exclusion>
107+
</exclusions>
108+
</dependency>
74109
<dependency>
75110
<groupId>org.apache.iceberg</groupId>
76111
<artifactId>iceberg-aws</artifactId>

‎amoro-format-lance/src/main/java/org/apache/amoro/formats/lance/AbstractLanceCatalog.java‎

Lines changed: 148 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,102 @@
1818

1919
package org.apache.amoro.formats.lance;
2020

21+
import org.apache.amoro.AlreadyExistsException;
2122
import org.apache.amoro.AmoroTable;
2223
import org.apache.amoro.FormatCatalog;
2324
import org.apache.amoro.NoSuchDatabaseException;
2425
import org.apache.amoro.NoSuchTableException;
2526
import org.apache.amoro.table.TableIdentifier;
27+
import org.apache.arrow.util.Preconditions;
28+
import org.apache.commons.lang3.StringUtils;
2629
import org.lance.Dataset;
2730
import org.lance.namespace.LanceNamespace;
31+
import org.lance.namespace.errors.InvalidInputException;
32+
import org.lance.namespace.errors.NamespaceAlreadyExistsException;
33+
import org.lance.namespace.errors.NamespaceNotFoundException;
2834
import org.lance.namespace.errors.TableNotFoundException;
35+
import org.lance.namespace.model.CreateNamespaceRequest;
36+
import org.lance.namespace.model.DropNamespaceRequest;
2937
import org.lance.namespace.model.DropTableRequest;
38+
import org.lance.namespace.model.ListNamespacesRequest;
39+
import org.lance.namespace.model.ListNamespacesResponse;
40+
import org.lance.namespace.model.ListTablesRequest;
41+
import org.lance.namespace.model.ListTablesResponse;
42+
import org.lance.namespace.model.NamespaceExistsRequest;
3043
import org.lance.namespace.model.TableExistsRequest;
3144

45+
import java.util.ArrayList;
3246
import java.util.Collections;
47+
import java.util.LinkedHashSet;
3348
import java.util.List;
49+
import java.util.Map;
50+
import java.util.Set;
3451

3552
/** Base implementation of {@link FormatCatalog} for Lance. */
3653
public abstract class AbstractLanceCatalog implements FormatCatalog {
3754

55+
protected static final String URI_PROPERTY = "uri";
56+
57+
private static final String CATALOG_PROPERTY = "catalog-name";
58+
3859
protected final String catalogName;
3960
protected final LanceNamespace namespace;
61+
private final List<String> catalogPrefix;
4062

4163
protected AbstractLanceCatalog(String catalogName, LanceNamespace namespace) {
64+
this(catalogName, namespace, Collections.emptyList());
65+
}
66+
67+
protected AbstractLanceCatalog(
68+
String catalogName, LanceNamespace namespace, List<String> catalogPrefix) {
4269
this.catalogName = catalogName;
4370
this.namespace = namespace;
71+
this.catalogPrefix = catalogPrefix;
72+
}
73+
74+
protected static List<String> catalogPrefix(
75+
Map<String, String> catalogProperties, String defaultCatalog) {
76+
String catalog = catalogProperties == null ? null : catalogProperties.get(CATALOG_PROPERTY);
77+
if (StringUtils.isEmpty(catalog)) {
78+
catalog = defaultCatalog;
79+
}
80+
return StringUtils.isEmpty(catalog)
81+
? Collections.emptyList()
82+
: Collections.singletonList(catalog);
4483
}
4584

4685
/** Composes the namespace table id used to reference a single table. */
47-
protected abstract List<String> tableId(String database, String table);
86+
protected List<String> tableId(String database, String table) {
87+
return concat(catalogPrefix, database, table);
88+
}
4889

4990
/** Composes the namespace id used to list tables of a database. */
50-
protected abstract List<String> tableIdForListTables(String database);
91+
protected List<String> namespaceId(String database) {
92+
return concat(catalogPrefix, database);
93+
}
94+
95+
protected static List<String> concat(List<String> prefix, String... parts) {
96+
List<String> id = new ArrayList<>(prefix);
97+
Collections.addAll(id, parts);
98+
return id;
99+
}
100+
101+
protected static void validateProperties(Map<String, String> properties) {
102+
Preconditions.checkArgument(
103+
properties != null && !properties.isEmpty(), "Catalog properties must be set.");
104+
}
105+
106+
protected AmoroTable<?> openTable(String database, String table) {
107+
namespace.tableExists(new TableExistsRequest().id(tableId(database, table)));
108+
Dataset dataset =
109+
Dataset.open().namespaceClient(namespace).tableId(tableId(database, table)).build();
110+
return new LanceTable(
111+
TableIdentifier.of(catalogName, database, table), dataset, Collections.emptyMap());
112+
}
113+
114+
protected void dropInNamespace(String database, String table, boolean purge) {
115+
namespace.dropTable(new DropTableRequest().id(tableId(database, table)));
116+
}
51117

52118
@Override
53119
public boolean tableExists(String database, String table) {
@@ -58,44 +124,111 @@ public boolean tableExists(String database, String table) {
58124
try {
59125
namespace.tableExists(new TableExistsRequest().id(tableId(database, table)));
60126
return true;
61-
} catch (TableNotFoundException e) {
127+
} catch (TableNotFoundException | InvalidInputException e) {
62128
return false;
63129
}
64130
}
65131

66132
@Override
67-
public AmoroTable<?> loadTable(String database, String tableName) {
68-
if (!tableExists(database, tableName)) {
69-
throw new NoSuchTableException("Table: " + database + "." + tableName + " does not exist");
70-
}
133+
public AmoroTable<?> loadTable(String database, String table) {
134+
validateDatabase(database);
71135

72-
TableIdentifier identifier = TableIdentifier.of(catalogName, database, tableName);
73-
Dataset dataset =
74-
Dataset.open().namespaceClient(namespace).tableId(tableId(database, tableName)).build();
75-
return new LanceTable(identifier, dataset, Collections.emptyMap());
136+
try {
137+
return openTable(database, table);
138+
} catch (TableNotFoundException | InvalidInputException e) {
139+
throw new NoSuchTableException("Table: " + database + "." + table + " does not exist", e);
140+
}
76141
}
77142

78143
@Override
79144
public boolean dropTable(String database, String table, boolean purge) {
80145
validateDatabase(database);
81146

82147
try {
83-
namespace.dropTable(new DropTableRequest().id(tableId(database, table)));
148+
dropInNamespace(database, table, purge);
84149
return true;
85-
} catch (TableNotFoundException e) {
150+
} catch (TableNotFoundException | InvalidInputException e) {
86151
return false;
87152
}
88153
}
89154

90155
@Override
91-
public abstract List<String> listTables(String database);
156+
public List<String> listTables(String database) {
157+
validateDatabase(database);
158+
159+
Set<String> tables = new LinkedHashSet<>();
160+
String pageToken = null;
161+
do {
162+
ListTablesRequest request = new ListTablesRequest().id(namespaceId(database));
163+
if (pageToken != null) {
164+
request.pageToken(pageToken);
165+
}
166+
167+
ListTablesResponse response = namespace.listTables(request);
168+
if (response == null) {
169+
break;
170+
}
171+
if (response.getTables() != null) {
172+
tables.addAll(response.getTables());
173+
}
174+
pageToken = response.getPageToken();
175+
} while (StringUtils.isNotEmpty(pageToken));
176+
177+
return new ArrayList<>(tables);
178+
}
92179

93180
@Override
94-
public abstract List<String> listDatabases();
181+
public List<String> listDatabases() {
182+
Set<String> namespaces = new LinkedHashSet<>();
183+
String pageToken = null;
184+
do {
185+
ListNamespacesRequest request = new ListNamespacesRequest().id(catalogPrefix);
186+
if (pageToken != null) {
187+
request.pageToken(pageToken);
188+
}
189+
190+
ListNamespacesResponse response = namespace.listNamespaces(request);
191+
if (response == null) {
192+
break;
193+
}
194+
if (response.getNamespaces() != null) {
195+
namespaces.addAll(response.getNamespaces());
196+
}
197+
198+
pageToken = response.getPageToken();
199+
} while (StringUtils.isNotEmpty(pageToken));
200+
201+
return new ArrayList<>(namespaces);
202+
}
95203

96204
protected void validateDatabase(String database) {
97205
if (!databaseExists(database)) {
98206
throw new NoSuchDatabaseException("Database: " + database + " does not exist");
99207
}
100208
}
209+
210+
@Override
211+
public boolean databaseExists(String database) {
212+
try {
213+
namespace.namespaceExists(new NamespaceExistsRequest().id(namespaceId(database)));
214+
return true;
215+
} catch (NamespaceNotFoundException e) {
216+
return false;
217+
}
218+
}
219+
220+
@Override
221+
public void createDatabase(String database) {
222+
try {
223+
namespace.createNamespace(new CreateNamespaceRequest().id(namespaceId(database)));
224+
} catch (NamespaceAlreadyExistsException e) {
225+
throw new AlreadyExistsException("Database: " + database + " already exists", e);
226+
}
227+
}
228+
229+
@Override
230+
public void dropDatabase(String database) {
231+
validateDatabase(database);
232+
namespace.dropNamespace(new DropNamespaceRequest().id(namespaceId(database)));
233+
}
101234
}

‎amoro-format-lance/src/main/java/org/apache/amoro/formats/lance/LanceCatalogFactory.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public FormatCatalog create(
4242
if (CatalogMetaProperties.CATALOG_TYPE_REST.equals(metastoreType)) {
4343
return new LanceRestCatalog(catalogName, properties);
4444
}
45+
if (CatalogMetaProperties.CATALOG_TYPE_HIVE.equals(metastoreType)) {
46+
return new LanceHiveCatalog(catalogName, properties, metaStore);
47+
}
4548
return new LanceDirectoryV1Catalog(catalogName, properties);
4649
}
4750

0 commit comments

Comments
 (0)