You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2020/04/14 02:32:52 UTC

[GitHub] [incubator-iceberg] XiaokunDing commented on a change in pull request #919: [ISSUE #672] Add SupportsNamespaces on HiveCatalog and HadoopCatalog

XiaokunDing commented on a change in pull request #919: [ISSUE #672] Add SupportsNamespaces on HiveCatalog and HadoopCatalog
URL: https://github.com/apache/incubator-iceberg/pull/919#discussion_r407829869
 
 

 ##########
 File path: core/src/main/java/org/apache/iceberg/hadoop/HadoopCatalog.java
 ##########
 @@ -200,7 +210,101 @@ public void renameTable(TableIdentifier from, TableIdentifier to) {
   }
 
   @Override
-  public void close() throws IOException {
+  public void createNamespace(Namespace namespace, ImmutableMap<String, String> meta) {
+    Preconditions.checkArgument(
+        !namespace.isEmpty(),
+        "Cannot create namespace with invalid name: %s", namespace);
+    if (!meta.isEmpty()) {
+      throw new UnsupportedOperationException("Cannot create namespace " + namespace + " : metadata is not supported");
+    }
+
+    Path nsPath = new Path(warehouseLocation, SLASH.join(namespace.levels()));
+    FileSystem fs = Util.getFs(nsPath, conf);
+
+    try {
+      if (isNamespace(fs, nsPath)) {
+        throw new AlreadyExistsException("Namespace '%s' already exists!", namespace);
+      }
+
+      fs.mkdirs(nsPath);
+
+    } catch (IOException e) {
+      throw new RuntimeIOException(e, "Create namespace failed: %s", namespace);
+    }
   }
 
+  @Override
+  public List<Namespace> listNamespaces(Namespace namespace) {
+    Path nsPath = new Path(SLASH.join(warehouseLocation, SLASH.join(namespace.levels())));
+    FileSystem fs = Util.getFs(nsPath, conf);
+
+    if (!isNamespace(fs, nsPath)) {
+      throw new NoSuchNamespaceException("Namespace does not exist: %s", namespace);
+    }
+
+    try {
+      return Stream.of(fs.listStatus(nsPath))
+        .map(FileStatus::getPath)
+        .filter(path -> isNamespace(fs, path))
+        .map(path -> append(namespace, path.getName()))
+        .collect(Collectors.toList());
+    } catch (IOException ioe) {
+      throw new RuntimeIOException(ioe, "Failed to list namespace under: %s", namespace);
+    }
+  }
+
+  @Override
+  public boolean dropNamespace(Namespace namespace) {
+    Path nsPath = new Path(SLASH.join(warehouseLocation, SLASH.join(namespace.levels())));
+    FileSystem fs = Util.getFs(nsPath, conf);
+
+    try {
+      if (!isNamespace(fs, nsPath) || namespace.isEmpty()) {
+        throw new NoSuchNamespaceException("Namespace does not exist: %s", namespace);
+      }
+
+      return fs.delete(nsPath, true /* recursive */);
+
+    } catch (IOException e) {
+      throw new RuntimeIOException(e, "Namespace delete failed: %s", namespace);
+    }
+  }
+
+  @Override
+  public boolean alterNamespace(Namespace namespace, NamespaceChange... changes) {
+    throw new UnsupportedOperationException(
+        "Cannot alter namespace " + namespace + " : alterNamespace is not supported");
+  }
+
+  @Override
+  public Map<String, String> loadNamespaceMetadata(Namespace namespace) {
+    Path nsPath = new Path(SLASH.join(warehouseLocation, SLASH.join(namespace.levels())));
+    FileSystem fs = Util.getFs(nsPath, conf);
+
+    if (!isNamespace(fs, nsPath) || namespace.isEmpty()) {
+      throw new NoSuchNamespaceException("Namespace does not exist: %s", namespace);
+    }
+
+    return ImmutableMap.of("location", nsPath.toString());
+  }
+
+  private boolean isNamespace(FileSystem fs, Path path) {
+    Path metadataPath = new Path(path, "metadata");
+    try {
+      return fs.isDirectory(path) && !(fs.exists(metadataPath) && fs.isDirectory(metadataPath) &&
+          (fs.listStatus(metadataPath, TABLE_FILTER).length >= 1));
+
+    } catch (IOException ioe) {
+      throw new RuntimeIOException(ioe, "Failed to list namespace info: %s ", path);
+    }
+  }
+  static Namespace append(Namespace ns, String name) {
 
 Review comment:
   OK

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org