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.statement;
22  
23  import org.apache.hadoop.hbase.client.tableindexed.IndexSpecification;
24  import org.apache.hadoop.hbase.hbql.client.ExecutionResults;
25  import org.apache.hadoop.hbase.hbql.client.HBqlException;
26  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  public class DescribeIndexForTableStatement extends TableStatement implements ConnectionStatement {
30  
31      private final String indexName;
32  
33      public DescribeIndexForTableStatement(final String indexName, final String tableName) {
34          super(null, tableName);
35          this.indexName = indexName;
36      }
37  
38      private String getIndexName() {
39          return this.indexName;
40      }
41  
42      protected ExecutionResults execute(final HConnectionImpl conn) throws HBqlException {
43          return executeDescribe(conn, this.getIndexName(), this.getTableName());
44      }
45  
46      static ExecutionResults executeDescribe(final HConnectionImpl conn,
47                                              final String indexName,
48                                              final String tableName) throws HBqlException {
49  
50          final ExecutionResults retval = new ExecutionResults();
51  
52          if (!conn.tableExists(tableName)) {
53              retval.out.println("Table not found: " + tableName);
54          }
55          else {
56  
57              final IndexSpecification index = conn.getIndexForTable(indexName, tableName);
58  
59              if (index == null) {
60                  retval.out.println("Index " + indexName + " not found for table " + tableName);
61              }
62              else {
63                  retval.out.println("Index: " + index.getIndexId());
64                  final byte[][] columns = index.getIndexedColumns();
65                  for (final byte[] column : columns)
66                      retval.out.println("Index key: " + Bytes.toString(column));
67  
68                  final byte[][] otherColumns = index.getAdditionalColumns();
69                  if (otherColumns.length > 0) {
70                      retval.out.println("Additional columns in index: ");
71                      for (final byte[] column : otherColumns)
72                          retval.out.println("    " + Bytes.toString(column));
73                  }
74                  else {
75                      retval.out.println("No additional columns in index.");
76                  }
77              }
78          }
79          retval.out.flush();
80          return retval;
81      }
82  
83      public static String usage() {
84          return "DESCRIBE INDEX index_name ON TABLE table_name";
85      }
86  }