-
Notifications
You must be signed in to change notification settings - Fork 7.3k
ZOOKEEPER-5037: Add export and import commands to the CLI #2375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -46,11 +46,13 @@ ZooKeeper -server host:port cmd args | |||||
| deleteall path | ||||||
| delquota [-n|-b|-N|-B] path | ||||||
| exit | ||||||
| export path filepath | ||||||
| get [-s] [-w] path | ||||||
| getAcl [-s] path | ||||||
| getAllChildrenNumber path | ||||||
| getEphemerals path | ||||||
| history | ||||||
| import path filepath | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here:
Suggested change
|
||||||
| listquota path | ||||||
| ls [-s] [-w] [-R] path | ||||||
| printwatches on|off | ||||||
|
|
@@ -194,8 +196,15 @@ Delete the quota under a path | |||||
| [zkshell: 4] delquota -N /c2 | ||||||
| [zkshell: 5] delquota -b /c3 | ||||||
| [zkshell: 6] delquota -B /c4 | ||||||
| ``` | ||||||
|
|
||||||
| ## export | ||||||
| Download the contents of a znode to an external file | ||||||
|
|
||||||
| ```bash | ||||||
| [zkshell: 1] export /zookeeper/config path/to/config.txt | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could add here what the options are doing, like what we have for |
||||||
| ``` | ||||||
|
|
||||||
| ## get | ||||||
| Get the data of the specific path | ||||||
|
|
||||||
|
|
@@ -285,6 +294,13 @@ Showing the history about the recent 11 commands that you have executed | |||||
| 7 - history | ||||||
| ``` | ||||||
|
|
||||||
| ## import | ||||||
| Upload the contents of an external file to a znode, replacing the znode's previous contents | ||||||
|
|
||||||
| ```bash | ||||||
| [zkshell: 1] import /zookeeper/config path/to/config.txt | ||||||
| ``` | ||||||
|
|
||||||
| ## listquota | ||||||
| Listing the quota of one path | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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.zookeeper.cli; | ||||||
|
|
||||||
| import org.apache.commons.cli.CommandLine; | ||||||
| import org.apache.commons.cli.CommandLineParser; | ||||||
| import org.apache.commons.cli.DefaultParser; | ||||||
| import org.apache.commons.cli.Options; | ||||||
| import org.apache.commons.cli.ParseException; | ||||||
| import org.apache.zookeeper.KeeperException; | ||||||
| import org.apache.zookeeper.data.Stat; | ||||||
|
|
||||||
| import java.io.IOException; | ||||||
| import java.nio.file.Files; | ||||||
| import java.nio.file.Paths; | ||||||
| import java.nio.file.StandardOpenOption; | ||||||
|
|
||||||
| /** | ||||||
| * export command for cli | ||||||
| */ | ||||||
| public class ExportCommand extends CliCommand { | ||||||
|
|
||||||
| private static Options options = new Options(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This field can be changed to be final. |
||||||
| private String args[]; | ||||||
| private CommandLine cl; | ||||||
|
|
||||||
| static { | ||||||
| options.addOption("s", false, "stats"); | ||||||
| options.addOption("w", false, "watch"); | ||||||
| } | ||||||
|
|
||||||
| public ExportCommand() { | ||||||
| super("export", "[-s] [-w] path filepath"); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public CliCommand parse(String[] cmdArgs) throws CliParseException { | ||||||
|
|
||||||
| CommandLineParser parser = new DefaultParser(); | ||||||
| try { | ||||||
| cl = parser.parse(options, cmdArgs); | ||||||
| } catch (ParseException ex) { | ||||||
| throw new CliParseException(ex); | ||||||
| } | ||||||
| args = cl.getArgs(); | ||||||
| if (args.length < 3) { | ||||||
| throw new CliParseException(getUsageStr()); | ||||||
| } | ||||||
|
|
||||||
| return this; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public boolean exec() throws CliException { | ||||||
| boolean watch = cl.hasOption("w"); | ||||||
| String path = args[1]; | ||||||
| String filepath = args[2]; | ||||||
| Stat stat = new Stat(); | ||||||
| byte data[]; | ||||||
| try { | ||||||
| data = zk.getData(path, watch, stat); | ||||||
| } catch (IllegalArgumentException ex) { | ||||||
| throw new MalformedPathException(ex.getMessage()); | ||||||
| } catch (KeeperException|InterruptedException ex) { | ||||||
| throw new CliException(ex); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should rather use For example if znode does not exits now: using
Suggested change
|
||||||
| } | ||||||
| data = (data == null) ? "null".getBytes() : data; | ||||||
| try { | ||||||
| Files.write(Paths.get(filepath), data, StandardOpenOption.CREATE); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should happen if the file already exists on disk? At the moment with only Should we rather overwrite the file? If yes, we chould use CREATE + TRUNCATE_EXISTING, or simply use the default |
||||||
| } catch (IOException ex) { | ||||||
| throw new CliException("Unable to write data to file \"" + filepath + "\"", ex); | ||||||
| } | ||||||
| if (cl.hasOption("s")) { | ||||||
| new StatPrinter(out).print(stat); | ||||||
| } | ||||||
| return watch; | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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.zookeeper.cli; | ||||||
|
|
||||||
| import org.apache.commons.cli.CommandLine; | ||||||
| import org.apache.commons.cli.CommandLineParser; | ||||||
| import org.apache.commons.cli.DefaultParser; | ||||||
| import org.apache.commons.cli.Options; | ||||||
| import org.apache.commons.cli.ParseException; | ||||||
| import org.apache.zookeeper.KeeperException; | ||||||
| import org.apache.zookeeper.data.Stat; | ||||||
|
|
||||||
| import java.io.IOException; | ||||||
| import java.nio.file.Files; | ||||||
| import java.nio.file.Paths; | ||||||
|
|
||||||
| /** | ||||||
| * import command for cli | ||||||
| */ | ||||||
| public class ImportCommand extends CliCommand { | ||||||
|
|
||||||
| private static Options options = new Options(); | ||||||
| private String[] args; | ||||||
| private CommandLine cl; | ||||||
|
|
||||||
| static { | ||||||
| options.addOption("s", false, "stats"); | ||||||
| options.addOption("v", true, "version"); | ||||||
| } | ||||||
|
|
||||||
| public ImportCommand() { | ||||||
| super("import", "[-s] [-v version] path filepath"); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For both commands we could use the 3 arg constructor:
Suggested change
This would include the formatted option descriptions in the generated help output: This is what we have as in this PR: |
||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public CliCommand parse(String[] cmdArgs) throws CliParseException { | ||||||
| CommandLineParser parser = new DefaultParser(); | ||||||
| try { | ||||||
| cl = parser.parse(options, cmdArgs); | ||||||
| } catch (ParseException ex) { | ||||||
| throw new CliParseException(ex); | ||||||
| } | ||||||
| args = cl.getArgs(); | ||||||
| if (args.length < 3) { | ||||||
| throw new CliParseException(getUsageStr()); | ||||||
| } | ||||||
|
|
||||||
| return this; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public boolean exec() throws CliException { | ||||||
| String path = args[1]; | ||||||
| String filepath = args[2]; | ||||||
| byte[] data; | ||||||
| try { | ||||||
| data = Files.readAllBytes(Paths.get(filepath)); | ||||||
| } catch (IOException ex) { | ||||||
| throw new CliException("Unable to read data from file \"" + filepath + "\"", ex); | ||||||
| } | ||||||
| int version; | ||||||
| if (cl.hasOption("v")) { | ||||||
| version = Integer.parseInt(cl.getOptionValue("v")); | ||||||
| } else { | ||||||
| version = -1; | ||||||
| } | ||||||
|
|
||||||
| try { | ||||||
| Stat stat = zk.setData(path, data, version); | ||||||
| if (cl.hasOption("s")) { | ||||||
| new StatPrinter(out).print(stat); | ||||||
| } | ||||||
| } catch (IllegalArgumentException ex) { | ||||||
| throw new MalformedPathException(ex.getMessage()); | ||||||
| } catch (KeeperException|InterruptedException ex) { | ||||||
| throw new CliWrapperException(ex); | ||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice that you extended the documentation! 👍
Should we also include the options like so?