View Javadoc

1   /*
2    * Copyright (c) 2011.  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.HColumnDescriptor;
24  import org.apache.hadoop.hbase.HTableDescriptor;
25  import org.apache.hadoop.hbase.client.tableindexed.IndexSpecification;
26  import org.apache.hadoop.hbase.client.tableindexed.IndexedTableDescriptor;
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.impl.HConnectionImpl;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  import java.util.Collection;
33  
34  public class DescribeTableStatement extends TableStatement {
35  
36      public DescribeTableStatement(final String tableName) {
37          super(null, tableName);
38      }
39  
40      protected ExecutionResults execute(final HConnectionImpl conn) throws HBqlException {
41  
42          final HTableDescriptor tableDesc = conn.getHTableDescriptor(this.getTableName());
43  
44          final ExecutionResults retval = new ExecutionResults();
45          retval.out.println("Table name: " + tableDesc.getNameAsString());
46          retval.out.println("\nFamilies:");
47          for (final HColumnDescriptor columnDesc : tableDesc.getFamilies()) {
48              retval.out.println("  " + columnDesc.getNameAsString()
49                                 + "\n    Max versions: " + columnDesc.getMaxVersions()
50                                 + "\n    TTL: " + columnDesc.getTimeToLive()
51                                 + "\n    Block size: " + columnDesc.getBlocksize()
52                                 + "\n    Compression: " + columnDesc.getCompression().getName()
53                                 + "\n    Compression type: " + columnDesc.getCompressionType().getName()
54                                 + "\n    Block cache enabled: " + columnDesc.isBlockCacheEnabled()
55                                 + "\n    Bloomfilter type: " + columnDesc.getBloomFilterType().name()
56                                 + "\n    In memory: " + columnDesc.isInMemory()
57                                 + "\n");
58          }
59  
60          final IndexedTableDescriptor indexDesc = conn.newIndexedTableDescriptor(this.getTableName());
61          final Collection<IndexSpecification> indexes = indexDesc.getIndexes();
62          if (indexes.isEmpty()) {
63              retval.out.println("No table indexes.");
64          }
65          else {
66              retval.out.println("Table indexes:");
67              for (final IndexSpecification index : indexes) {
68                  retval.out.println("  " + index.getIndexId());
69                  final byte[][] columns = index.getIndexedColumns();
70                  for (final byte[] column : columns)
71                      retval.out.println("    " + Bytes.toString(column));
72              }
73          }
74          retval.out.flush();
75          return retval;
76      }
77  
78      public static String usage() {
79          return "DESCRIBE TABLE table_name";
80      }
81  }