1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.accumulo.core.util.shell.commands;
18
19 import java.io.File;
20 import java.util.TreeSet;
21
22 import org.apache.accumulo.core.client.TableNotFoundException;
23 import org.apache.accumulo.core.util.shell.Shell;
24 import org.apache.accumulo.core.util.shell.Shell.Command;
25 import org.apache.commons.cli.CommandLine;
26 import org.apache.commons.cli.MissingArgumentException;
27 import org.apache.commons.cli.Option;
28 import org.apache.commons.cli.Options;
29 import org.apache.commons.codec.binary.Base64;
30 import org.apache.hadoop.io.Text;
31
32 public class AddSplitsCommand extends Command {
33 private Option optSplitsFile, base64Opt;
34
35 public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws Exception {
36 final String tableName = OptUtil.getTableOpt(cl, shellState);
37 final boolean decode = cl.hasOption(base64Opt.getOpt());
38
39 final TreeSet<Text> splits = new TreeSet<Text>();
40
41 if (cl.hasOption(optSplitsFile.getOpt())) {
42 final String f = cl.getOptionValue(optSplitsFile.getOpt());
43
44 String line;
45 java.util.Scanner file = new java.util.Scanner(new File(f));
46 while (file.hasNextLine()) {
47 line = file.nextLine();
48 if (!line.isEmpty()) {
49 splits.add(decode ? new Text(Base64.decodeBase64(line.getBytes())) : new Text(line));
50 }
51 }
52 } else {
53 if (cl.getArgList().isEmpty()) {
54 throw new MissingArgumentException("No split points specified");
55 }
56 for (String s : cl.getArgs()) {
57 splits.add(new Text(s.getBytes(Shell.CHARSET)));
58 }
59 }
60
61 if (!shellState.getConnector().tableOperations().exists(tableName)) {
62 throw new TableNotFoundException(null, tableName, null);
63 }
64 shellState.getConnector().tableOperations().addSplits(tableName, splits);
65
66 return 0;
67 }
68
69 @Override
70 public String description() {
71 return "adds split points to an existing table";
72 }
73
74 @Override
75 public Options getOptions() {
76 final Options o = new Options();
77
78 optSplitsFile = new Option("sf", "splits-file", true, "file with a newline-separated list of rows to split the table with");
79 optSplitsFile.setArgName("filename");
80
81 base64Opt = new Option("b64", "base64encoded", false, "decode encoded split points (splits file only)");
82
83 o.addOption(OptUtil.tableOpt("name of the table to add split points to"));
84 o.addOption(optSplitsFile);
85 o.addOption(base64Opt);
86 return o;
87 }
88
89 @Override
90 public String usage() {
91 return getName() + " [<split>{ <split>} ]";
92 }
93
94 @Override
95 public int numArgs() {
96 return Shell.NO_FIXED_ARG_LENGTH_CHECK;
97 }
98 }