0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 package org.apache.hive.service.cli.operation;
0020
0021 import org.apache.hadoop.hive.metastore.IMetaStoreClient;
0022 import org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest;
0023 import org.apache.hadoop.hive.metastore.api.SQLPrimaryKey;
0024 import org.apache.hadoop.hive.serde2.thrift.Type;
0025 import org.apache.hive.service.cli.*;
0026 import org.apache.hive.service.cli.session.HiveSession;
0027
0028 import java.util.List;
0029
0030
0031
0032
0033
0034 public class GetPrimaryKeysOperation extends MetadataOperation {
0035
0036
0037
0038
0039
0040
0041
0042
0043 private static final TableSchema RESULT_SET_SCHEMA = new TableSchema()
0044 .addPrimitiveColumn("TABLE_CAT", Type.STRING_TYPE,
0045 "Table catalog (may be null)")
0046 .addPrimitiveColumn("TABLE_SCHEM", Type.STRING_TYPE,
0047 "Table schema (may be null)")
0048 .addPrimitiveColumn("TABLE_NAME", Type.STRING_TYPE,
0049 "Table name")
0050 .addPrimitiveColumn("COLUMN_NAME", Type.STRING_TYPE,
0051 "Column name")
0052 .addPrimitiveColumn("KEQ_SEQ", Type.INT_TYPE,
0053 "Sequence number within primary key")
0054 .addPrimitiveColumn("PK_NAME", Type.STRING_TYPE,
0055 "Primary key name (may be null)");
0056
0057 private final String catalogName;
0058 private final String schemaName;
0059 private final String tableName;
0060
0061 private final RowSet rowSet;
0062
0063 public GetPrimaryKeysOperation(HiveSession parentSession,
0064 String catalogName, String schemaName, String tableName) {
0065 super(parentSession, OperationType.GET_FUNCTIONS);
0066 this.catalogName = catalogName;
0067 this.schemaName = schemaName;
0068 this.tableName = tableName;
0069 this.rowSet = RowSetFactory.create(RESULT_SET_SCHEMA, getProtocolVersion(), false);
0070 }
0071
0072 @Override
0073 public void runInternal() throws HiveSQLException {
0074 setState(OperationState.RUNNING);
0075 try {
0076 IMetaStoreClient metastoreClient = getParentSession().getMetaStoreClient();
0077 PrimaryKeysRequest sqlReq = new PrimaryKeysRequest(schemaName, tableName);
0078 List<SQLPrimaryKey> pks = metastoreClient.getPrimaryKeys(sqlReq);
0079 if (pks == null) {
0080 return;
0081 }
0082 for (SQLPrimaryKey pk : pks) {
0083 rowSet.addRow(new Object[] {catalogName, pk.getTable_db(),
0084 pk.getTable_name(), pk.getColumn_name(), pk.getKey_seq(), pk.getPk_name()});
0085 }
0086 setState(OperationState.FINISHED);
0087 } catch (Exception e) {
0088 setState(OperationState.ERROR);
0089 throw new HiveSQLException(e);
0090 }
0091 }
0092
0093
0094
0095
0096
0097 @Override
0098 public TableSchema getResultSetSchema() throws HiveSQLException {
0099 assertState(OperationState.FINISHED);
0100 return RESULT_SET_SCHEMA;
0101 }
0102
0103
0104
0105
0106 @Override
0107 public RowSet getNextRowSet(FetchOrientation orientation, long maxRows) throws HiveSQLException {
0108 assertState(OperationState.FINISHED);
0109 validateDefaultFetchOrientation(orientation);
0110 if (orientation.equals(FetchOrientation.FETCH_FIRST)) {
0111 rowSet.setStartOffset(0);
0112 }
0113 return rowSet.extractSubset((int)maxRows);
0114 }
0115 }