Search in sources :

Example 1 with PartitionValuesResponse

use of org.apache.hadoop.hive.metastore.api.PartitionValuesResponse in project hive by apache.

the class TestListPartitions method testListPartitionValues.

/**
 * Testing listPartitionValues(PartitionValuesRequest) ->
 *         get_partition_values(PartitionValuesRequest).
 * @throws Exception
 */
@Test
public void testListPartitionValues() throws Exception {
    List<List<String>> testValues = createTable4PartColsParts(client);
    List<FieldSchema> partitionSchema = Lists.newArrayList(new FieldSchema("yyyy", "string", ""), new FieldSchema("mm", "string", ""));
    PartitionValuesRequest request = new PartitionValuesRequest(DB_NAME, TABLE_NAME, partitionSchema);
    PartitionValuesResponse response = client.listPartitionValues(request);
    assertCorrectPartitionValuesResponse(testValues, response);
}
Also used : PartitionValuesRequest(org.apache.hadoop.hive.metastore.api.PartitionValuesRequest) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) PartitionValuesResponse(org.apache.hadoop.hive.metastore.api.PartitionValuesResponse) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 2 with PartitionValuesResponse

use of org.apache.hadoop.hive.metastore.api.PartitionValuesResponse in project hive by apache.

the class ObjectStore method getDistinctValuesForPartitionsNoTxn.

private PartitionValuesResponse getDistinctValuesForPartitionsNoTxn(String dbName, String tableName, List<FieldSchema> cols, boolean applyDistinct, boolean ascending, long maxParts) throws MetaException {
    try {
        openTransaction();
        Query q = pm.newQuery("select partitionName from org.apache.hadoop.hive.metastore.model.MPartition " + "where table.database.name == t1 && table.tableName == t2 ");
        q.declareParameters("java.lang.String t1, java.lang.String t2");
        /*
      if (ascending) {
        q.setOrdering("partitionName ascending");
      } else {
        q.setOrdering("partitionName descending");
      }
*/
        if (maxParts > 0) {
            q.setRange(0, maxParts);
        }
        StringBuilder partValuesSelect = new StringBuilder(256);
        if (applyDistinct) {
            partValuesSelect.append("DISTINCT ");
        }
        List<FieldSchema> partitionKeys = getTable(dbName, tableName).getPartitionKeys();
        for (FieldSchema key : cols) {
            partValuesSelect.append(extractPartitionKey(key, partitionKeys)).append(", ");
        }
        partValuesSelect.setLength(partValuesSelect.length() - 2);
        LOG.info("Columns to be selected from Partitions: {}", partValuesSelect);
        q.setResult(partValuesSelect.toString());
        PartitionValuesResponse response = new PartitionValuesResponse();
        response.setPartitionValues(new ArrayList<PartitionValuesRow>());
        if (cols.size() > 1) {
            List<Object[]> results = (List<Object[]>) q.execute(dbName, tableName);
            for (Object[] row : results) {
                PartitionValuesRow rowResponse = new PartitionValuesRow();
                for (Object columnValue : row) {
                    rowResponse.addToRow((String) columnValue);
                }
                response.addToPartitionValues(rowResponse);
            }
        } else {
            List<Object> results = (List<Object>) q.execute(dbName, tableName);
            for (Object row : results) {
                PartitionValuesRow rowResponse = new PartitionValuesRow();
                rowResponse.addToRow((String) row);
                response.addToPartitionValues(rowResponse);
            }
        }
        q.closeAll();
        return response;
    } finally {
        commitTransaction();
    }
}
Also used : PartitionValuesRow(org.apache.hadoop.hive.metastore.api.PartitionValuesRow) Query(javax.jdo.Query) MFieldSchema(org.apache.hadoop.hive.metastore.model.MFieldSchema) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) PartitionValuesResponse(org.apache.hadoop.hive.metastore.api.PartitionValuesResponse) LinkedList(java.util.LinkedList) MStringList(org.apache.hadoop.hive.metastore.model.MStringList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with PartitionValuesResponse

use of org.apache.hadoop.hive.metastore.api.PartitionValuesResponse in project hive by apache.

the class ObjectStore method listPartitionValues.

@Override
public PartitionValuesResponse listPartitionValues(String dbName, String tableName, List<FieldSchema> cols, boolean applyDistinct, String filter, boolean ascending, List<FieldSchema> order, long maxParts) throws MetaException {
    dbName = dbName.toLowerCase().trim();
    tableName = tableName.toLowerCase().trim();
    try {
        if (filter == null || filter.isEmpty()) {
            PartitionValuesResponse response = getDistinctValuesForPartitionsNoTxn(dbName, tableName, cols, applyDistinct, ascending, maxParts);
            LOG.info("Number of records fetched: {}", response.getPartitionValues().size());
            return response;
        } else {
            PartitionValuesResponse response = extractPartitionNamesByFilter(dbName, tableName, filter, cols, ascending, applyDistinct, maxParts);
            if (response != null && response.getPartitionValues() != null) {
                LOG.info("Number of records fetched with filter: {}", response.getPartitionValues().size());
            }
            return response;
        }
    } catch (Exception t) {
        LOG.error("Exception in ORM", t);
        throw new MetaException("Error retrieving partition values: " + t);
    } finally {
    }
}
Also used : PartitionValuesResponse(org.apache.hadoop.hive.metastore.api.PartitionValuesResponse) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) InvalidInputException(org.apache.hadoop.hive.metastore.api.InvalidInputException) InvalidOperationException(org.apache.hadoop.hive.metastore.api.InvalidOperationException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) IOException(java.io.IOException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) JDOCanRetryException(javax.jdo.JDOCanRetryException) InvalidPartitionException(org.apache.hadoop.hive.metastore.api.InvalidPartitionException) UnknownPartitionException(org.apache.hadoop.hive.metastore.api.UnknownPartitionException) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) JDOException(javax.jdo.JDOException) MissingTableException(org.datanucleus.store.rdbms.exceptions.MissingTableException) SQLException(java.sql.SQLException) UnknownDBException(org.apache.hadoop.hive.metastore.api.UnknownDBException) TException(org.apache.thrift.TException) JDODataStoreException(javax.jdo.JDODataStoreException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) UnknownTableException(org.apache.hadoop.hive.metastore.api.UnknownTableException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Example 4 with PartitionValuesResponse

use of org.apache.hadoop.hive.metastore.api.PartitionValuesResponse in project hive by apache.

the class ObjectStore method extractPartitionNamesByFilter.

private PartitionValuesResponse extractPartitionNamesByFilter(String dbName, String tableName, String filter, List<FieldSchema> cols, boolean ascending, boolean applyDistinct, long maxParts) throws MetaException, NoSuchObjectException {
    LOG.info("Database: {} Table: {} filter: \"{}\" cols: {}", dbName, tableName, filter, cols);
    List<String> partitionNames = null;
    List<Partition> partitions = null;
    Table tbl = getTable(dbName, tableName);
    try {
        // Get partitions by name - ascending or descending
        partitionNames = getPartitionNamesByFilter(dbName, tableName, filter, ascending, maxParts);
    } catch (MetaException e) {
        LOG.warn("Querying by partition names failed, trying out with partition objects, filter: {}", filter);
    }
    if (partitionNames == null) {
        partitions = getPartitionsByFilter(dbName, tableName, filter, (short) maxParts);
    }
    if (partitions != null) {
        partitionNames = new ArrayList<String>(partitions.size());
        for (Partition partition : partitions) {
            // Check for NULL's just to be safe
            if (tbl.getPartitionKeys() != null && partition.getValues() != null) {
                partitionNames.add(Warehouse.makePartName(tbl.getPartitionKeys(), partition.getValues()));
            }
        }
    }
    if (partitionNames == null && partitions == null) {
        throw new MetaException("Cannot obtain list of partitions by filter:\"" + filter + "\" for " + dbName + ":" + tableName);
    }
    if (!ascending) {
        Collections.sort(partitionNames, Collections.reverseOrder());
    }
    // Return proper response
    PartitionValuesResponse response = new PartitionValuesResponse();
    response.setPartitionValues(new ArrayList<PartitionValuesRow>(partitionNames.size()));
    LOG.info("Converting responses to Partition values for items: {}", partitionNames.size());
    for (String partName : partitionNames) {
        ArrayList<String> vals = new ArrayList<String>(Collections.nCopies(tbl.getPartitionKeys().size(), null));
        PartitionValuesRow row = new PartitionValuesRow();
        Warehouse.makeValsFromName(partName, vals);
        for (String value : vals) {
            row.addToRow(value);
        }
        response.addToPartitionValues(row);
    }
    return response;
}
Also used : MPartition(org.apache.hadoop.hive.metastore.model.MPartition) Partition(org.apache.hadoop.hive.metastore.api.Partition) PartitionValuesRow(org.apache.hadoop.hive.metastore.api.PartitionValuesRow) MVersionTable(org.apache.hadoop.hive.metastore.model.MVersionTable) Table(org.apache.hadoop.hive.metastore.api.Table) MTable(org.apache.hadoop.hive.metastore.model.MTable) PartitionValuesResponse(org.apache.hadoop.hive.metastore.api.PartitionValuesResponse) ArrayList(java.util.ArrayList) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Aggregations

PartitionValuesResponse (org.apache.hadoop.hive.metastore.api.PartitionValuesResponse)4 ArrayList (java.util.ArrayList)3 List (java.util.List)2 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)2 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)2 PartitionValuesRow (org.apache.hadoop.hive.metastore.api.PartitionValuesRow)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)1 LinkedList (java.util.LinkedList)1 JDOCanRetryException (javax.jdo.JDOCanRetryException)1 JDODataStoreException (javax.jdo.JDODataStoreException)1 JDOException (javax.jdo.JDOException)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 Query (javax.jdo.Query)1 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)1 AlreadyExistsException (org.apache.hadoop.hive.metastore.api.AlreadyExistsException)1 InvalidInputException (org.apache.hadoop.hive.metastore.api.InvalidInputException)1 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)1 InvalidOperationException (org.apache.hadoop.hive.metastore.api.InvalidOperationException)1