View Javadoc

1   /*
2    * Copyright (c) 2010.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.hbql;
22  
23  import jline.ArgumentCompletor;
24  import jline.ConsoleReader;
25  import jline.SimpleCompletor;
26  import org.apache.hadoop.hbase.HBaseConfiguration;
27  import org.apache.hadoop.hbase.hbql.client.ExecutionResults;
28  import org.apache.hadoop.hbase.hbql.client.HBqlException;
29  import org.apache.hadoop.hbase.hbql.client.HConnectionManager;
30  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
31  import org.apache.hadoop.hbase.hbql.impl.Utils;
32  import org.apache.hadoop.hbase.hbql.statement.ImportStatement;
33  import org.apache.hadoop.hbase.hbql.statement.VersionStatement;
34  import org.apache.hadoop.hbase.hbql.util.Lists;
35  import org.apache.hadoop.hbase.hbql.util.Maps;
36  
37  import java.io.IOException;
38  import java.io.PrintWriter;
39  import java.util.Arrays;
40  import java.util.LinkedList;
41  import java.util.List;
42  import java.util.Map;
43  
44  public class Console {
45  
46      private static HConnectionImpl conn = null;
47      private static boolean processCommandLine = true;
48      private static HBaseConfiguration config = null;
49  
50      public static void main(String[] args) throws HBqlException, IOException {
51  
52          if (args != null && args.length > 0) {
53  
54              final List<String> argList = new LinkedList<String>();
55              argList.addAll(Arrays.asList(args));
56  
57              while (argList.size() > 0) {
58                  if (!processArg(argList))
59                      break;
60              }
61          }
62  
63          if (processCommandLine)
64              processCommandLineInput();
65      }
66  
67      private static void usage() {
68  
69          System.out.println("Usage: java " + Console.class.getName() + " [-options]");
70          System.out.println("\t\t(comand line usage");
71          System.out.println("   or  java " + Console.class.getName() + " [-options] [file_names]");
72          System.out.println("\t\t(executes the statements in the space-separated file names)");
73          System.out.println("\nwhere options include:");
74          System.out.println("\t-usage                print this message");
75          System.out.println("\t-version              print version info and exit");
76          System.out.println("\t-" + HConnectionImpl.MASTER + "=value   set " + HConnectionImpl.MASTER + " value");
77      }
78  
79      private static boolean processArg(final List<String> argList) throws HBqlException {
80  
81          final String option = argList.remove(0);
82  
83          if (option.equals("-usage")) {
84              processCommandLine = false;
85              usage();
86              return true;
87          }
88  
89          if (option.equals("-version")) {
90              processCommandLine = false;
91              final VersionStatement version = new VersionStatement();
92              final ExecutionResults results = version.execute();
93              System.out.print(results);
94              return true;
95          }
96  
97          if (option.startsWith("-" + HConnectionImpl.MASTER)) {
98              final String[] vals = option.split("=");
99              if (vals.length != 2) {
100                 processCommandLine = false;
101                 System.out.println("Incorrect syntax: " + option);
102                 usage();
103                 return false;
104             }
105             else {
106                 config = HConnectionImpl.getHBaseConfiguration(vals[1]);
107                 return true;
108             }
109         }
110 
111         if (option.startsWith("-")) {
112             processCommandLine = false;
113             System.out.println("Unknown option: " + option);
114             usage();
115             return false;
116         }
117         else {
118             // Assume that an arg without "-" prefix is a filename
119             processCommandLine = false;
120             final ImportStatement importStmt = new ImportStatement(option);
121             final ExecutionResults results = importStmt.evaluatePredicateAndExecute(getConnection());
122             System.out.print(results);
123             return results.hadSuccess();
124         }
125     }
126 
127     private synchronized static HConnectionImpl getConnection() throws HBqlException {
128 
129         if (conn == null)
130             conn = (HConnectionImpl)HConnectionManager.newConnection(config);
131 
132         return conn;
133     }
134 
135     private static void processCommandLineInput() throws IOException, HBqlException {
136 
137         final List<SimpleCompletor> completors = Lists.newArrayList();
138         completors.add(new SimpleCompletor(new String[]{"select", "insert", "create", "table", "mapping",
139                                                         "describe", "drop", "enable", "disable", "show",
140                                                         "executor", "pool"}));
141 
142         final ConsoleReader reader = new ConsoleReader();
143         reader.setBellEnabled(false);
144         reader.setUseHistory(true);
145         //reader.setDebug(new PrintWriter(new FileWriter("writer.debug", true)));
146         reader.addCompletor(new ArgumentCompletor(completors));
147 
148         final PrintWriter out = new PrintWriter(System.out);
149 
150         StringBuilder stmtBuffer = new StringBuilder();
151         boolean continuation = false;
152 
153         final Map<Boolean, String> prompts = Maps.newHashMap();
154         prompts.put(Boolean.FALSE, "HBql> ");
155         prompts.put(Boolean.TRUE, "> ");
156 
157         while (true) {
158 
159             final String line = reader.readLine(prompts.get(continuation));
160 
161             if (line == null || line.toLowerCase().startsWith("quit") || line.toLowerCase().startsWith("exit"))
162                 break;
163 
164             if (Utils.isValidString(line)) {
165                 stmtBuffer.append(line);
166 
167                 continuation = !line.trim().endsWith(";");
168                 if (!continuation) {
169                     final String sql = stmtBuffer.toString();
170                     stmtBuffer = new StringBuilder();
171                     ImportStatement.processInput(out, getConnection(), sql);
172                 }
173             }
174         }
175     }
176 }