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_r360474687
##########
File path: core/src/main/java/org/apache/iceberg/hadoop/HadoopCatalog.java
##########
@@ -195,6 +205,109 @@ public void renameTable(TableIdentifier from,
TableIdentifier to) {
throw new UnsupportedOperationException("Cannot rename Hadoop tables");
}
+ @Override
+ public void createNamespace(Namespace namespace, Map<String, String> meta) {
+ Preconditions.checkArgument(!namespace.isEmpty(),
+ "Cannot create namespace with invalid name: %s", namespace.toString());
+ Preconditions.checkArgument(meta == null || meta.size() == 0,
+ "Hadoop Catalog not support namespace metaData: %s",
namespace.toString());
+
+ Path nsPath = new Path(SLASH.join(warehouseLocation,
SLASH.join(namespace.levels())));
+ FileSystem fs = Util.getFs(nsPath, conf);
+ try {
+ fs.mkdirs(nsPath);
+ } catch (IOException e) {
+ throw new RuntimeIOException("Create namespace failed: %s",
namespace.toString(), e.getMessage());
+ }
+ }
+
+ @Override
+ public List<Namespace> listNamespaces(Namespace namespace) {
+ // Preconditions.checkArgument(!namespace.isEmpty(), "Your Namespace must
be not empty!");
+ List<Namespace> namespaceList = new ArrayList<>();
+ String[] namespaces;
+ Path nsPath = new Path(SLASH.join(warehouseLocation,
SLASH.join(namespace.levels())));
+ FileSystem fs = Util.getFs(nsPath, conf);
+ try {
+ if (!fs.exists(nsPath) || !fs.isDirectory(nsPath)) {
+ throw new NoSuchNamespaceException("namespace does not exist: " +
namespace.toString());
+ }
+
+ List<String> pathList =
Stream.of(fs.listStatus(nsPath)).map(FileStatus::getPath)
+ .collect(Collectors.toList()).stream().filter(path ->
isNamespace(fs, path))
+ .map(Path::getName).collect(Collectors.toList());
+
+ for (String path : pathList) {
+ if (!namespace.isEmpty()) {
+ namespaces = Namespace.of(namespace.toString() + "." +
path).levels();
+ } else {
+ namespaces = new String[]{path};
+ }
+ namespaceList.add(Namespace.of(namespaces));
+ }
+ } catch (IOException ioe) {
+ throw new RuntimeException("Failed to list namespace under: " +
namespace.toString(), ioe);
+ }
+
+ return namespaceList;
+ }
+
+ @Override
+ public boolean dropNamespace(Namespace namespace, boolean cascade) {
+ if (!cascade) {
+ Preconditions.checkArgument(!namespace.isEmpty(),
+ "namespace does not exist: %s", namespace.toString());
+ Preconditions.checkArgument(listTables(namespace).size() == 0,
+ "This Namespace have tables, cannot drop it");
+ Preconditions.checkArgument(listNamespaces(namespace).size() == 0,
+ "This Namespace have sub Namespace, cannot drop it");
+ }
+ Path nsPath = new Path(SLASH.join(warehouseLocation,
SLASH.join(namespace.levels())));
+ FileSystem fs = Util.getFs(nsPath, conf);
+
+ try {
+ return fs.delete(nsPath, true);
+ } catch (IOException e) {
+ throw new RuntimeIOException("Namespace delete failed :",
namespace.toString(), e.getMessage());
+ }
+ }
+
+ @Override
+ public Map<String, String> loadNamespaceMetadata(Namespace namespace) {
+ Preconditions.checkArgument(!namespace.isEmpty(),
+ "namespace does not exist: %s", namespace.toString());
+ Path nsPath = new Path(SLASH.join(warehouseLocation,
SLASH.join(namespace.levels())));
+ Map<String, String> meta = new HashMap<>();
+ FileSystem fs = Util.getFs(nsPath, conf);
+ try {
+ if (!fs.exists(nsPath) || !fs.isDirectory(nsPath)) {
+ throw new NoSuchNamespaceException("namespace does not exist: " +
namespace.toString());
+ }
+ FileStatus info = fs.getFileStatus(nsPath);
+ meta.put("owner", info.getOwner());
+ meta.put("group", info.getGroup());
+ meta.put("path", info.getPath().toString());
+ meta.put("modification_time", Long.toString(info.getModificationTime()));
+ meta.put("block_size", Long.toString(info.getBlockSize()));
+ } catch (IOException ioe) {
+ throw new RuntimeException("Failed to list namespace info " + namespace,
ioe);
+ }
+ return meta;
+ }
+
+ private boolean isNamespace(FileSystem fs, Path path) {
+ Path metadataPath = new Path(path, "metadata");
+ try {
+ if (fs.isDirectory(path) && !(fs.exists(metadataPath) &&
fs.isDirectory(metadataPath) &&
+ (fs.listStatus(metadataPath, TABLE_FILTER).length >= 1))) {
+ return true;
+ }
+ } catch (IOException ioe) {
+ throw new RuntimeException("Failed to list namespace info " + path, ioe);
Review comment:
IOExceptions should be wrapped by `RuntimeIOException`
----------------------------------------------------------------
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]