View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  }