Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions zookeeper-docs/src/main/resources/markdown/zookeeperCLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ ZooKeeper -server host:port cmd args
deleteall path
delquota [-n|-b|-N|-B] path
exit
export path filepath
Copy link
Copy Markdown
Contributor

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?

Suggested change
export path filepath
export [-s] [-w] path filepath

get [-s] [-w] path
getAcl [-s] path
getAllChildrenNumber path
getEphemerals path
history
import path filepath
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here:

Suggested change
import path filepath
import [-s] [-v version] path filepath

listquota path
ls [-s] [-w] [-R] path
printwatches on|off
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 here:
https://github.com/apache/zookeeper/blob/master/zookeeper-docs/src/main/resources/markdown/zookeeperCLI.md?plain=1#L206

```

## get
Get the data of the specific path

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public enum Command {
LS(LsCommand::new),
GET_ACL(GetAclCommand::new),
SET_ACL(SetAclCommand::new),
EXPORT(ExportCommand::new),
IMPORT(ImportCommand::new),
STAT(StatCommand::new),
SYNC(SyncCommand::new),
SET_QUOTA(SetQuotaCommand::new),
Expand Down
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();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rather use CliWrapperException here instead of CliException because CliWrapperException provides user-friendly error messages. Other commands are using this.

For example if znode does not exits now:

[zk: 127.0.0.1:2181(CONNECTED) 1] export /zookeeper/a my_data
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /zookeeper/a
[zk: 127.0.0.1:2181(CONNECTED) 2]

using CliWrapperException:

[zk: 127.0.0.1:2181(CONNECTED) 0] export /zookeeper/a my_data
Node does not exist: /zookeeper/a
[zk: 127.0.0.1:2181(CONNECTED) 1
Suggested change
throw new CliException(ex);
throw new CliWrapperException(ex);

}
data = (data == null) ? "null".getBytes() : data;
try {
Files.write(Paths.get(filepath), data, StandardOpenOption.CREATE);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 StandardOpenOption.CREATE will append to an existing file rather than replacing it.
If you export a znode twice to the same file, you get the data concatenated. Or if you export a znode to an existing file which has longer content, it will just replace some amount of the file content from the start.

Should we rather overwrite the file? If yes, we chould use CREATE + TRUNCATE_EXISTING, or simply use the default Files.write(path, data) which creates/truncates by 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");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For both commands we could use the 3 arg constructor:

Suggested change
super("import", "[-s] [-v version] path filepath");
super("import", "[-s] [-v version] path filepath", options);

This would include the formatted option descriptions in the generated help output:

zk: 127.0.0.1:2181(CONNECTED) 1] import
import [-s] [-v version] path filepath
 -s         stats
 -v <arg>   version

[zk: 127.0.0.1:2181(CONNECTED) 2] export
export [-s] [-w] path filepath
 -s   stats
 -w   watch

This is what we have as in this PR:

[zk: 127.0.0.1:2181(CONNECTED) 0] import
import [-s] [-v version] path filepath
[zk: 127.0.0.1:2181(CONNECTED) 1] export
export [-s] [-w] path filepath
[zk: 127.0.0.1:2181(CONNECTED) 2

}

@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;
}
}
Loading