rdblue commented on a change in pull request #688: [ISSUE #672] Add 
SupportsNamespaces for 'HadoopCatalog' and 'HiveCatalog'
URL: https://github.com/apache/incubator-iceberg/pull/688#discussion_r362064541
 
 

 ##########
 File path: hive/src/main/java/org/apache/iceberg/hive/HiveCatalog.java
 ##########
 @@ -155,14 +164,155 @@ public void renameTable(TableIdentifier from, 
TableIdentifier to) {
       throw new org.apache.iceberg.exceptions.AlreadyExistsException("Table 
already exists: %s", to);
 
     } catch (TException e) {
-      throw new RuntimeException("Failed to rename " + from.toString() + " to 
" + to.toString(), e);
+      throw new RuntimeException("Failed to rename " + from + " to " + to, e);
 
     } catch (InterruptedException e) {
       Thread.currentThread().interrupt();
       throw new RuntimeException("Interrupted in call to rename", e);
     }
   }
 
+  @Override
+  public void createNamespace(Namespace namespace, ImmutableMap<String, 
String> meta) {
+    Preconditions.checkArgument(
+        !namespace.isEmpty(),
+        "Cannot create namespace with invalid name: %s", namespace);
+    Preconditions.checkArgument(
+        namespace.levels().length == 1,
+        "Cannot support multi part namespace in Hive MetaStore: %s", 
namespace);
+
+    try {
+      clients.run(client -> {
+        client.createDatabase(convertToDatabase(namespace, meta));
+        return null;
+      });
+
+    } catch (AlreadyExistsException e) {
+      throw new org.apache.iceberg.exceptions.AlreadyExistsException(e, 
"Namespace '%s' already exists!",
+            namespace);
+
+    } catch (TException e) {
+      throw new RuntimeException("Failed to create namespace " + namespace + " 
in Hive MataStore", e);
+
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+      throw new RuntimeException(
+          "Interrupted in call to createDatabase(name) " + namespace + " in 
Hive MataStore", e);
+    }
+  }
+
+  @Override
+  public List<Namespace> listNamespaces(Namespace namespace) {
+    if (namespace.levels().length > 1) {
+      throw new NoSuchNamespaceException("Namespace does not exist: %s", 
namespace);
+    }
+
+    try {
+      return namespace.isEmpty() ? clients.run(
+          HiveMetaStoreClient::getAllDatabases)
+          .stream()
+          .map(Namespace::of)
+          .collect(Collectors.toList()) : Stream.of(Namespace.empty())
+          .collect(Collectors.toList());
+
+    } catch (TException e) {
+      throw new RuntimeException("Failed to list all namespace: " + namespace 
+ " in Hive MataStore",  e);
+
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+      throw new RuntimeException(
+          "Interrupted in call to getAllDatabases()" + namespace + " in Hive 
MataStore", e);
+    }
+  }
+
+  @Override
+  public boolean dropNamespace(Namespace namespace) {
+    if (namespace.levels().length != 1) {
+      throw new NoSuchNamespaceException("Namespace does not exist: %s", 
namespace);
+    }
+
+    try {
+      clients.run(client -> {
+        client.dropDatabase(namespace.level(0),
+            false /* deleteData */,
+            false /* ignoreUnknownDb */,
+            true /* cascade */);
+        return null;
+      });
+
+      return true;
+
+    } catch (NoSuchObjectException e) {
+      throw new NoSuchNamespaceException(e, "Namespace '%s' does not exist!", 
namespace);
+
+    } catch (TException e) {
+      throw new RuntimeException("Failed to drop namespace " + namespace + " 
in Hive MataStore", e);
+
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+      throw new RuntimeException(
+          "Interrupted in call to drop dropDatabase(name)" + namespace + " in 
Hive MataStore", e);
+    }
+  }
+
+  @Override
+  public boolean alterNamespace(Namespace namespace, ImmutableMap<String, 
String> meta) {
+    if (meta.containsKey("location")) {
+      throw new UnsupportedOperationException("Does not support change 
'location' in HiveCatalog: " + namespace);
+    }
+
+    if (meta.containsKey("name")) {
+      throw new UnsupportedOperationException("Does not support change 'name' 
in HiveCatalog: " + namespace);
+    }
+
+    Map<String, String> newMeta = Maps.newHashMap();
+    newMeta.putAll(loadNamespaceMetadata(namespace));
+    newMeta.putAll(meta);
+
+    try {
+      clients.run(client -> {
+        client.alterDatabase(namespace.toString(), 
convertToDatabase(namespace, newMeta));
+        return null;
+      });
+
+      return true;
+
+    } catch (NoSuchObjectException | UnknownDBException e) {
+      throw new NoSuchNamespaceException(e, "Namespace does not exist: %s", 
namespace);
+
+    } catch (TException e) {
+      throw new RuntimeException(
+          "Failed to list namespace under namespace: " + namespace + " in Hive 
MataStore", e);
+
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+      throw new RuntimeException("Interrupted in call to getDatabase(name)" + 
namespace + " in Hive MataStore", e);
+    }
+  }
+
+  @Override
+  public Map<String, String> loadNamespaceMetadata(Namespace namespace) {
+    if (namespace.levels().length != 1) {
+      throw new NoSuchNamespaceException("Namespace does not exist: %s", 
namespace);
+    }
+
+    try {
+      Database database = clients.run(client -> 
client.getDatabase(namespace.toString()));
+      return convertToMetadata(database);
+
+    } catch (NoSuchObjectException | UnknownDBException e) {
+      throw new NoSuchNamespaceException(e, "Namespace does not exist: %s", 
namespace);
+
+    } catch (TException e) {
+      throw new RuntimeException("Failed to list namespace under namespace: " 
+ namespace + " in Hive MataStore", e);
+
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+      throw new RuntimeException(
+          "Interrupted in call to getDatabase(name)" + namespace + " in Hive 
MataStore", e);
 
 Review comment:
   Nit: missing space between `getDatabase(name)` and `namespace` in the error 
message. You may find it easier to avoid these problems by using 
`String.format`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to