From c18c6801effdb9809c8a5be70a4f01330744d5d7 Mon Sep 17 00:00:00 2001 From: Chris Nauroth Date: Fri, 25 Mar 2016 09:50:58 -0700 Subject: [PATCH] HDFS-10202. ozone : Add key commands to CLI. Contributed by Anu Engineer. --- .../hadoop/ozone/web/ozShell/Shell.java | 107 ++++++++++++++- .../web/ozShell/bucket/InfoBucketHandler.java | 2 +- .../ozShell/bucket/UpdateBucketHandler.java | 98 ++++++++++++++ .../web/ozShell/keys/DeleteKeyHandler.java | 95 ++++++++++++++ .../ozone/web/ozShell/keys/GetKeyHandler.java | 122 ++++++++++++++++++ .../web/ozShell/keys/InfoKeyHandler.java | 93 +++++++++++++ .../web/ozShell/keys/ListKeyHandler.java | 105 +++++++++++++++ .../ozone/web/ozShell/keys/PutKeyHandler.java | 110 ++++++++++++++++ 8 files changed, 724 insertions(+), 8 deletions(-) create mode 100644 hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/bucket/UpdateBucketHandler.java create mode 100644 hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/DeleteKeyHandler.java create mode 100644 hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/GetKeyHandler.java create mode 100644 hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/InfoKeyHandler.java create mode 100644 hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/ListKeyHandler.java create mode 100644 hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/PutKeyHandler.java diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/Shell.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/Shell.java index 6fc42a0751e..7a8dd4df994 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/Shell.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/Shell.java @@ -23,9 +23,16 @@ import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.ozone.web.exceptions.OzoneException; +import org.apache.hadoop.ozone.web.ozShell.bucket.UpdateBucketHandler; +import org.apache.hadoop.ozone.web.ozShell.keys.DeleteKeyHandler; +import org.apache.hadoop.ozone.web.ozShell.keys.GetKeyHandler; +import org.apache.hadoop.ozone.web.ozShell.keys.InfoKeyHandler; +import org.apache.hadoop.ozone.web.ozShell.keys.ListKeyHandler; +import org.apache.hadoop.ozone.web.ozShell.keys.PutKeyHandler; import org.apache.hadoop.ozone.web.ozShell.volume.CreateVolumeHandler; import org.apache.hadoop.ozone.web.ozShell.volume.DeleteVolumeHandler; import org.apache.hadoop.ozone.web.ozShell.volume.InfoVolumeHandler; @@ -72,6 +79,15 @@ public class Shell extends Configured implements Tool { public static final String INFO_BUCKET = "infoBucket"; public static final String ADD_ACLS = "addAcl"; public static final String REMOVE_ACLS = "removeAcl"; + // TODO : Support versioning and StorageType for buckets + + //Object related command line arguments + public static final String PUT_KEY = "putKey"; + public static final String GET_KEY = "getKey"; + public static final String INFO_KEY = "infoKey"; + public static final String DELETE_KEY = "deleteKey"; + public static final String LIST_KEY = "listKey"; + public static final String FILE = "file"; /** * Main for the ozShell Command handling. @@ -118,6 +134,7 @@ public class Shell extends Configured implements Tool { Options opts = new Options(); addVolumeCommands(opts); addBucketCommands(opts); + addKeyCommands(opts); return opts; } @@ -131,8 +148,14 @@ public class Shell extends Configured implements Tool { */ private CommandLine parseArgs(String[] argv, Options opts) throws org.apache.commons.cli.ParseException { - BasicParser parser = new BasicParser(); - return parser.parse(opts, argv); + try { + BasicParser parser = new BasicParser(); + return parser.parse(opts, argv); + } catch (ParseException ex) { + System.out.printf(ex.getMessage()); + } + + return null; } @@ -195,10 +218,8 @@ public class Shell extends Configured implements Tool { */ private void addBucketCommands(Options opts) { Option createBucket = new Option(CREATE_BUCKET, true, - "creates a bucket in a given volume.\n" + - "\t For example : hdfs oz " + - "-createBucket " + - ""); + "creates a bucket in a given volume." + + "For example: hdfs oz -createBucket "); opts.addOption(createBucket); Option infoBucket = @@ -210,9 +231,56 @@ public class Shell extends Configured implements Tool { opts.addOption(deleteBucket); Option listBucket = - new Option(LIST_BUCKET, true, "Lists the buckets in a volume."); + new Option(LIST_BUCKET, true, "lists the buckets in a volume."); opts.addOption(listBucket); + Option updateBucket = + new Option(UPDATE_BUCKET, true, "allows changing bucket attributes.\n" + + " For example: hdfs oz -updateBucket " + + "-addAcl user:frodo:rw"); + opts.addOption(updateBucket); + + Option addAcl = + new Option(ADD_ACLS, true, "allows user to add acls to a bucket."); + opts.addOption(addAcl); + + Option removeAcl = + new Option(REMOVE_ACLS, true, "allows user to remove acls from a " + + "bucket."); + opts.addOption(removeAcl); + + } + + /** + * All key commands. + * + * @param opts - options + */ + private void addKeyCommands(Options opts) { + Option putKey = + new Option(PUT_KEY, true, "creates or overwrites an existing key"); + opts.addOption(putKey); + + Option deleteKey = + new Option(DELETE_KEY, true, "deletes an existing key"); + opts.addOption(deleteKey); + + Option infoKey = + new Option(INFO_KEY, true, "returns information about an existing key"); + opts.addOption(infoKey); + + Option listKey = + new Option(LIST_KEY, true, "list all keys in a given bucket"); + opts.addOption(listKey); + + Option getKey = + new Option(GET_KEY, true, "Gets a specific key from ozone server."); + opts.addOption(getKey); + + Option fileArgument = + new Option(FILE, true, "Data file path"); + opts.addOption(fileArgument); + } /** @@ -269,6 +337,31 @@ public class Shell extends Configured implements Tool { handler = new ListBucketHandler(); } + if(cmd.hasOption(Shell.UPDATE_BUCKET)){ + handler = new UpdateBucketHandler(); + } + + //Key Functions + + if(cmd.hasOption(Shell.PUT_KEY)) { + handler = new PutKeyHandler(); + } + + if(cmd.hasOption(Shell.DELETE_KEY)) { + handler = new DeleteKeyHandler(); + } + + if(cmd.hasOption(Shell.INFO_KEY)) { + handler = new InfoKeyHandler(); + } + + if(cmd.hasOption(Shell.LIST_KEY)) { + handler = new ListKeyHandler(); + } + + if(cmd.hasOption(Shell.GET_KEY)) { + handler = new GetKeyHandler(); + } if (handler != null) { handler.execute(cmd); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/bucket/InfoBucketHandler.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/bucket/InfoBucketHandler.java index 6694768968f..e930cba1e5a 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/bucket/InfoBucketHandler.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/bucket/InfoBucketHandler.java @@ -64,7 +64,7 @@ public class InfoBucketHandler extends Handler { if (path.getNameCount() < 2) { throw new OzoneClientException( - "volume and bucket name required in delete Bucket"); + "volume and bucket name required in info Bucket"); } volumeName = path.getName(0).toString(); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/bucket/UpdateBucketHandler.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/bucket/UpdateBucketHandler.java new file mode 100644 index 00000000000..c79cfffe237 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/bucket/UpdateBucketHandler.java @@ -0,0 +1,98 @@ +/* + * 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.hadoop.ozone.web.ozShell.bucket; + +import org.apache.commons.cli.CommandLine; +import org.apache.hadoop.ozone.web.client.OzoneBucket; +import org.apache.hadoop.ozone.web.client.OzoneClientException; +import org.apache.hadoop.ozone.web.client.OzoneVolume; +import org.apache.hadoop.ozone.web.exceptions.OzoneException; +import org.apache.hadoop.ozone.web.ozShell.Handler; +import org.apache.hadoop.ozone.web.ozShell.Shell; +import org.codehaus.jackson.map.ObjectMapper; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Allows users to add and remove acls and from a bucket. + */ +public class UpdateBucketHandler extends Handler { + private String volumeName; + private String bucketName; + private String rootName; + + @Override + protected void execute(CommandLine cmd) + throws IOException, OzoneException, URISyntaxException { + if (!cmd.hasOption(Shell.UPDATE_BUCKET)) { + throw new OzoneClientException( + "Incorrect call : updateBucket is missing"); + } + + String ozoneURIString = cmd.getOptionValue(Shell.UPDATE_BUCKET); + URI ozoneURI = verifyURI(ozoneURIString); + Path path = Paths.get(ozoneURI.getPath()); + + if (path.getNameCount() < 2) { + throw new OzoneClientException( + "volume and bucket name required in update bucket"); + } + + volumeName = path.getName(0).toString(); + bucketName = path.getName(1).toString(); + + if (cmd.hasOption(Shell.VERBOSE)) { + System.out.printf("Volume Name : %s%n", volumeName); + System.out.printf("Bucket Name : %s%n", bucketName); + } + + if (cmd.hasOption(Shell.RUNAS)) { + rootName = "hdfs"; + } else { + rootName = System.getProperty("user.name"); + } + + client.setEndPointURI(ozoneURI); + client.setUserAuth(rootName); + + OzoneVolume vol = client.getVolume(volumeName); + if (cmd.hasOption(Shell.ADD_ACLS)) { + String aclString = cmd.getOptionValue(Shell.ADD_ACLS); + String[] aclArray = aclString.split(","); + vol.addAcls(bucketName, aclArray); + } + + if (cmd.hasOption(Shell.REMOVE_ACLS)) { + String aclString = cmd.getOptionValue(Shell.REMOVE_ACLS); + String[] aclArray = aclString.split(","); + vol.removeAcls(bucketName, aclArray); + } + + OzoneBucket bucket = vol.getBucket(bucketName); + + ObjectMapper mapper = new ObjectMapper(); + Object json = + mapper.readValue(bucket.getBucketInfo().toJsonString(), Object.class); + System.out.printf("%s%n", mapper.writerWithDefaultPrettyPrinter() + .writeValueAsString(json)); + } +} diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/DeleteKeyHandler.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/DeleteKeyHandler.java new file mode 100644 index 00000000000..a75b3317829 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/DeleteKeyHandler.java @@ -0,0 +1,95 @@ +/* + * 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.hadoop.ozone.web.ozShell.keys; + +import org.apache.commons.cli.CommandLine; +import org.apache.hadoop.ozone.web.client.OzoneBucket; +import org.apache.hadoop.ozone.web.client.OzoneClientException; +import org.apache.hadoop.ozone.web.client.OzoneVolume; +import org.apache.hadoop.ozone.web.exceptions.OzoneException; +import org.apache.hadoop.ozone.web.ozShell.Handler; +import org.apache.hadoop.ozone.web.ozShell.Shell; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Executes Delete Key. + */ +public class DeleteKeyHandler extends Handler { + private String userName; + private String volumeName; + private String bucketName; + private String keyName; + + /** + * Executes the Client Calls. + * + * @param cmd - CommandLine + * @throws IOException + * @throws OzoneException + * @throws URISyntaxException + */ + @Override + protected void execute(CommandLine cmd) + throws IOException, OzoneException, URISyntaxException { + if (!cmd.hasOption(Shell.DELETE_KEY)) { + throw new OzoneClientException("Incorrect call : deleteKey is missing"); + } + + + if (cmd.hasOption(Shell.USER)) { + userName = cmd.getOptionValue(Shell.USER); + } else { + userName = System.getProperty("user.name"); + } + + + String ozoneURIString = cmd.getOptionValue(Shell.DELETE_KEY); + URI ozoneURI = verifyURI(ozoneURIString); + Path path = Paths.get(ozoneURI.getPath()); + if (path.getNameCount() < 3) { + throw new OzoneClientException( + "volume/bucket/key name required in deleteKey"); + } + + volumeName = path.getName(0).toString(); + bucketName = path.getName(1).toString(); + keyName = path.getName(2).toString(); + + + if (cmd.hasOption(Shell.VERBOSE)) { + System.out.printf("Volume Name : %s%n", volumeName); + System.out.printf("Bucket Name : %s%n", bucketName); + System.out.printf("Key Name : %s%n", keyName); + } + + client.setEndPointURI(ozoneURI); + client.setUserAuth(userName); + + + OzoneVolume vol = client.getVolume(volumeName); + OzoneBucket bucket = vol.createBucket(bucketName); + bucket.deleteKey(keyName); + + } +} diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/GetKeyHandler.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/GetKeyHandler.java new file mode 100644 index 00000000000..f0b207fae95 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/GetKeyHandler.java @@ -0,0 +1,122 @@ +/* + * 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.hadoop.ozone.web.ozShell.keys; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.hadoop.ozone.web.client.OzoneBucket; +import org.apache.hadoop.ozone.web.client.OzoneClientException; +import org.apache.hadoop.ozone.web.client.OzoneVolume; +import org.apache.hadoop.ozone.web.exceptions.OzoneException; +import org.apache.hadoop.ozone.web.ozShell.Handler; +import org.apache.hadoop.ozone.web.ozShell.Shell; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Gets an existing key. + */ +public class GetKeyHandler extends Handler { + private String userName; + private String volumeName; + private String bucketName; + private String keyName; + + + /** + * Executes the Client Calls. + * + * @param cmd - CommandLine + * + * @throws IOException + * @throws OzoneException + * @throws URISyntaxException + */ + @Override + protected void execute(CommandLine cmd) + throws IOException, OzoneException, URISyntaxException { + if (!cmd.hasOption(Shell.GET_KEY)) { + throw new OzoneClientException("Incorrect call : getKey is missing"); + } + + if (!cmd.hasOption(Shell.FILE)) { + throw new OzoneClientException( + "get key needs a file path to download to"); + } + + if (cmd.hasOption(Shell.USER)) { + userName = cmd.getOptionValue(Shell.USER); + } else { + userName = System.getProperty("user.name"); + } + + + String ozoneURIString = cmd.getOptionValue(Shell.GET_KEY); + URI ozoneURI = verifyURI(ozoneURIString); + Path path = Paths.get(ozoneURI.getPath()); + if (path.getNameCount() < 3) { + throw new OzoneClientException( + "volume/bucket/key name required in putKey"); + } + + volumeName = path.getName(0).toString(); + bucketName = path.getName(1).toString(); + keyName = path.getName(2).toString(); + + + if (cmd.hasOption(Shell.VERBOSE)) { + System.out.printf("Volume Name : %s%n", volumeName); + System.out.printf("Bucket Name : %s%n", bucketName); + System.out.printf("Key Name : %s%n", keyName); + } + + + String fileName = cmd.getOptionValue(Shell.FILE); + Path dataFilePath = Paths.get(fileName); + File dataFile = new File(fileName); + + + if (dataFile.exists()) { + throw new OzoneClientException(fileName + + "exists. Download will overwrite an " + + "existing file. Aborting."); + } + + client.setEndPointURI(ozoneURI); + client.setUserAuth(userName); + + + OzoneVolume vol = client.getVolume(volumeName); + OzoneBucket bucket = vol.getBucket(bucketName); + bucket.getKey(keyName, dataFilePath); + + if(cmd.hasOption(Shell.VERBOSE)) { + FileInputStream stream = new FileInputStream(dataFile); + String hash = DigestUtils.md5Hex(stream); + System.out.printf("Downloaded file hash : %s%n", hash); + } + + } +} diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/InfoKeyHandler.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/InfoKeyHandler.java new file mode 100644 index 00000000000..ac77f61d738 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/InfoKeyHandler.java @@ -0,0 +1,93 @@ +/* + * 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.hadoop.ozone.web.ozShell.keys; + +import org.apache.commons.cli.CommandLine; +import org.apache.hadoop.ozone.web.client.OzoneClientException; +import org.apache.hadoop.ozone.web.exceptions.OzoneException; +import org.apache.hadoop.ozone.web.ozShell.Handler; +import org.apache.hadoop.ozone.web.ozShell.Shell; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Executes Info Object. + */ +public class InfoKeyHandler extends Handler { + private String userName; + private String volumeName; + private String bucketName; + private String keyName; + + /** + * Executes the Client Calls. + * + * @param cmd - CommandLine + * @throws IOException + * @throws OzoneException + * @throws URISyntaxException + */ + @Override + protected void execute(CommandLine cmd) + throws IOException, OzoneException, URISyntaxException { + if (!cmd.hasOption(Shell.INFO_KEY)) { + throw new OzoneClientException("Incorrect call : infoKey is missing"); + } + + + if (cmd.hasOption(Shell.USER)) { + userName = cmd.getOptionValue(Shell.USER); + } else { + userName = System.getProperty("user.name"); + } + + + String ozoneURIString = cmd.getOptionValue(Shell.INFO_KEY); + URI ozoneURI = verifyURI(ozoneURIString); + Path path = Paths.get(ozoneURI.getPath()); + if (path.getNameCount() < 3) { + throw new OzoneClientException( + "volume/bucket/key name required in infoKey"); + } + + volumeName = path.getName(0).toString(); + bucketName = path.getName(1).toString(); + keyName = path.getName(2).toString(); + + + if (cmd.hasOption(Shell.VERBOSE)) { + System.out.printf("Volume Name : %s%n", volumeName); + System.out.printf("Bucket Name : %s%n", bucketName); + System.out.printf("Key Name : %s%n", keyName); + } + + client.setEndPointURI(ozoneURI); + client.setUserAuth(userName); + + +// OzoneVolume vol = client.getVolume(volumeName); +// OzoneBucket bucket = vol.createBucket(bucketName); + + throw new OzoneClientException("Not supported yet"); + } +} diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/ListKeyHandler.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/ListKeyHandler.java new file mode 100644 index 00000000000..5f04cd06de4 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/ListKeyHandler.java @@ -0,0 +1,105 @@ +/* + * 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.hadoop.ozone.web.ozShell.keys; + +import org.apache.commons.cli.CommandLine; +import org.apache.hadoop.ozone.web.client.OzoneBucket; +import org.apache.hadoop.ozone.web.client.OzoneClientException; +import org.apache.hadoop.ozone.web.client.OzoneKey; +import org.apache.hadoop.ozone.web.client.OzoneVolume; +import org.apache.hadoop.ozone.web.exceptions.OzoneException; +import org.apache.hadoop.ozone.web.ozShell.Handler; +import org.apache.hadoop.ozone.web.ozShell.Shell; +import org.codehaus.jackson.map.ObjectMapper; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +/** + * Executes List Keys. + */ +public class ListKeyHandler extends Handler { + private String userName; + private String volumeName; + private String bucketName; + + /** + * Executes the Client Calls. + * + * @param cmd - CommandLine + * @throws IOException + * @throws OzoneException + * @throws URISyntaxException + */ + @Override + protected void execute(CommandLine cmd) + throws IOException, OzoneException, URISyntaxException { + + if (!cmd.hasOption(Shell.LIST_KEY)) { + throw new OzoneClientException("Incorrect call : listKey is missing"); + } + + String ozoneURIString = cmd.getOptionValue(Shell.LIST_KEY); + URI ozoneURI = verifyURI(ozoneURIString); + Path path = Paths.get(ozoneURI.getPath()); + if (path.getNameCount() < 2) { + throw new OzoneClientException("volume/bucket is required in listKey"); + } + + volumeName = path.getName(0).toString(); + bucketName = path.getName(1).toString(); + + + if (cmd.hasOption(Shell.VERBOSE)) { + System.out.printf("Volume Name : %s%n", volumeName); + System.out.printf("bucket Name : %s%n", bucketName); + } + + if (cmd.hasOption(Shell.USER)) { + userName = cmd.getOptionValue(Shell.USER); + } else { + userName = System.getProperty("user.name"); + } + + + client.setEndPointURI(ozoneURI); + client.setUserAuth(userName); + + + OzoneVolume vol = client.getVolume(volumeName); + OzoneBucket bucket = vol.getBucket(bucketName); + List keys = bucket.listKeys(); + + ObjectMapper mapper = new ObjectMapper(); + + + for (OzoneKey key : keys) { + Object json = + mapper.readValue(key.getObjectInfo().toJsonString(), Object.class); + System.out.printf("%s%n", mapper.writerWithDefaultPrettyPrinter() + .writeValueAsString(json)); + } + + } + +} diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/PutKeyHandler.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/PutKeyHandler.java new file mode 100644 index 00000000000..513787dfdeb --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/PutKeyHandler.java @@ -0,0 +1,110 @@ +/* + * 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.hadoop.ozone.web.ozShell.keys; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.hadoop.ozone.web.client.OzoneBucket; +import org.apache.hadoop.ozone.web.client.OzoneClientException; +import org.apache.hadoop.ozone.web.client.OzoneVolume; +import org.apache.hadoop.ozone.web.exceptions.OzoneException; +import org.apache.hadoop.ozone.web.ozShell.Handler; +import org.apache.hadoop.ozone.web.ozShell.Shell; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Puts a file into an ozone bucket. + */ +public class PutKeyHandler extends Handler { + private String userName; + private String volumeName; + private String bucketName; + private String keyName; + + /** + * Executes the Client Calls. + * + * @param cmd - CommandLine + * @throws IOException + * @throws OzoneException + * @throws URISyntaxException + */ + @Override + protected void execute(CommandLine cmd) + throws IOException, OzoneException, URISyntaxException { + if (!cmd.hasOption(Shell.PUT_KEY)) { + throw new OzoneClientException("Incorrect call : putKey is missing"); + } + + if (!cmd.hasOption(Shell.FILE)) { + throw new OzoneClientException("put key needs a file to put"); + } + + if (cmd.hasOption(Shell.USER)) { + userName = cmd.getOptionValue(Shell.USER); + } else { + userName = System.getProperty("user.name"); + } + + String ozoneURIString = cmd.getOptionValue(Shell.PUT_KEY); + URI ozoneURI = verifyURI(ozoneURIString); + Path path = Paths.get(ozoneURI.getPath()); + if (path.getNameCount() < 3) { + throw new OzoneClientException( + "volume/bucket/key name required in putKey"); + } + + volumeName = path.getName(0).toString(); + bucketName = path.getName(1).toString(); + keyName = path.getName(2).toString(); + + + if (cmd.hasOption(Shell.VERBOSE)) { + System.out.printf("Volume Name : %s%n", volumeName); + System.out.printf("Bucket Name : %s%n", bucketName); + System.out.printf("Key Name : %s%n", keyName); + } + + + String fileName = cmd.getOptionValue(Shell.FILE); + File dataFile = new File(fileName); + + if (cmd.hasOption(Shell.VERBOSE)) { + FileInputStream stream = new FileInputStream(dataFile); + String hash = DigestUtils.md5Hex(stream); + System.out.printf("File Hash : %s%n", hash); + stream.close(); + } + + client.setEndPointURI(ozoneURI); + client.setUserAuth(userName); + + OzoneVolume vol = client.getVolume(volumeName); + OzoneBucket bucket = vol.getBucket(bucketName); + bucket.putKey(keyName, dataFile); + } + +}