Search in sources :

Example 6 with SchemaVersion

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

the class ObjectStore method getLatestSchemaVersion.

@Override
public SchemaVersion getLatestSchemaVersion(ISchemaName schemaName) throws MetaException {
    boolean committed = false;
    Query query = null;
    try {
        openTransaction();
        String name = normalizeIdentifier(schemaName.getSchemaName());
        String dbName = normalizeIdentifier(schemaName.getDbName());
        query = pm.newQuery(MSchemaVersion.class, "iSchema.name == schemaName && iSchema.db.name == dbName");
        query.declareParameters("java.lang.String schemaName, java.lang.String dbName");
        query.setUnique(true);
        query.setOrdering("version descending");
        query.setRange(0, 1);
        MSchemaVersion mSchemaVersion = (MSchemaVersion) query.execute(name, dbName);
        pm.retrieve(mSchemaVersion);
        if (mSchemaVersion != null) {
            pm.retrieveAll(mSchemaVersion.getCols());
            if (mSchemaVersion.getSerDe() != null)
                pm.retrieve(mSchemaVersion.getSerDe());
        }
        SchemaVersion version = mSchemaVersion == null ? null : convertToSchemaVersion(mSchemaVersion);
        committed = commitTransaction();
        return version;
    } finally {
        rollbackAndCleanup(committed, query);
    }
}
Also used : MSchemaVersion(org.apache.hadoop.hive.metastore.model.MSchemaVersion) Query(javax.jdo.Query) SchemaVersion(org.apache.hadoop.hive.metastore.api.SchemaVersion) MSchemaVersion(org.apache.hadoop.hive.metastore.model.MSchemaVersion)

Example 7 with SchemaVersion

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

the class ObjectStore method getSchemaVersion.

@Override
public SchemaVersion getSchemaVersion(SchemaVersionDescriptor version) throws MetaException {
    boolean committed = false;
    try {
        openTransaction();
        SchemaVersion schemaVersion = convertToSchemaVersion(getMSchemaVersion(version.getSchema().getDbName(), version.getSchema().getSchemaName(), version.getVersion()));
        committed = commitTransaction();
        return schemaVersion;
    } finally {
        if (!committed)
            rollbackTransaction();
        ;
    }
}
Also used : SchemaVersion(org.apache.hadoop.hive.metastore.api.SchemaVersion) MSchemaVersion(org.apache.hadoop.hive.metastore.model.MSchemaVersion)

Example 8 with SchemaVersion

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

the class SchemaVersionBuilder method build.

public SchemaVersion build() throws MetaException {
    if (schemaName == null || dbName == null || version < 0) {
        throw new MetaException("You must provide the database name, schema name, and schema version");
    }
    SchemaVersion schemaVersion = new SchemaVersion(new ISchemaName(dbName, schemaName), version, createdAt, getCols());
    if (state != null)
        schemaVersion.setState(state);
    if (description != null)
        schemaVersion.setDescription(description);
    if (schemaText != null)
        schemaVersion.setSchemaText(schemaText);
    if (fingerprint != null)
        schemaVersion.setFingerprint(fingerprint);
    if (name != null)
        schemaVersion.setName(name);
    schemaVersion.setSerDe(buildSerde());
    return schemaVersion;
}
Also used : SchemaVersion(org.apache.hadoop.hive.metastore.api.SchemaVersion) ISchemaName(org.apache.hadoop.hive.metastore.api.ISchemaName) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Example 9 with SchemaVersion

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

the class TestHiveMetaStoreSchemaMethods method schemaQuery.

@Test
public void schemaQuery() throws TException {
    String dbName = "schema_query_db";
    Database db = new DatabaseBuilder().setName(dbName).build();
    client.createDatabase(db);
    String schemaName1 = uniqueSchemaName();
    ISchema schema1 = new ISchemaBuilder().setSchemaType(SchemaType.AVRO).setDbName(dbName).setName(schemaName1).build();
    client.createISchema(schema1);
    String schemaName2 = uniqueSchemaName();
    ISchema schema2 = new ISchemaBuilder().setSchemaType(SchemaType.AVRO).setDbName(dbName).setName(schemaName2).build();
    client.createISchema(schema2);
    SchemaVersion schemaVersion1_1 = new SchemaVersionBuilder().versionOf(schema1).setVersion(1).addCol("alpha", ColumnType.BIGINT_TYPE_NAME).addCol("beta", ColumnType.DATE_TYPE_NAME).build();
    client.addSchemaVersion(schemaVersion1_1);
    SchemaVersion schemaVersion1_2 = new SchemaVersionBuilder().versionOf(schema1).setVersion(2).addCol("alpha", ColumnType.BIGINT_TYPE_NAME).addCol("beta", ColumnType.DATE_TYPE_NAME).addCol("gamma", ColumnType.BIGINT_TYPE_NAME, "namespace=x").build();
    client.addSchemaVersion(schemaVersion1_2);
    SchemaVersion schemaVersion2_1 = new SchemaVersionBuilder().versionOf(schema2).setVersion(1).addCol("ALPHA", ColumnType.SMALLINT_TYPE_NAME).addCol("delta", ColumnType.DOUBLE_TYPE_NAME).build();
    client.addSchemaVersion(schemaVersion2_1);
    SchemaVersion schemaVersion2_2 = new SchemaVersionBuilder().versionOf(schema2).setVersion(2).addCol("ALPHA", ColumnType.SMALLINT_TYPE_NAME).addCol("delta", ColumnType.DOUBLE_TYPE_NAME).addCol("epsilon", ColumnType.STRING_TYPE_NAME, "namespace=x").build();
    client.addSchemaVersion(schemaVersion2_2);
    // Query that should return nothing
    FindSchemasByColsRqst rqst = new FindSchemasByColsRqst();
    rqst.setColName("x");
    rqst.setColNamespace("y");
    rqst.setType("z");
    FindSchemasByColsResp rsp = client.getSchemaByCols(rqst);
    Assert.assertEquals(0, rsp.getSchemaVersionsSize());
    // Query that should fetch one column
    rqst = new FindSchemasByColsRqst();
    rqst.setColName("gamma");
    rsp = client.getSchemaByCols(rqst);
    Assert.assertEquals(1, rsp.getSchemaVersionsSize());
    Assert.assertEquals(schemaName1, rsp.getSchemaVersions().get(0).getSchema().getSchemaName());
    Assert.assertEquals(dbName, rsp.getSchemaVersions().get(0).getSchema().getDbName());
    Assert.assertEquals(2, rsp.getSchemaVersions().get(0).getVersion());
    // fetch 2 in same schema
    rqst = new FindSchemasByColsRqst();
    rqst.setColName("beta");
    rsp = client.getSchemaByCols(rqst);
    Assert.assertEquals(2, rsp.getSchemaVersionsSize());
    List<SchemaVersionDescriptor> results = new ArrayList<>(rsp.getSchemaVersions());
    Collections.sort(results);
    Assert.assertEquals(schemaName1, results.get(0).getSchema().getSchemaName());
    Assert.assertEquals(dbName, results.get(0).getSchema().getDbName());
    Assert.assertEquals(1, results.get(0).getVersion());
    Assert.assertEquals(schemaName1, results.get(1).getSchema().getSchemaName());
    Assert.assertEquals(dbName, results.get(1).getSchema().getDbName());
    Assert.assertEquals(2, results.get(1).getVersion());
    // fetch across schemas
    rqst = new FindSchemasByColsRqst();
    rqst.setColName("alpha");
    rsp = client.getSchemaByCols(rqst);
    Assert.assertEquals(4, rsp.getSchemaVersionsSize());
    results = new ArrayList<>(rsp.getSchemaVersions());
    Collections.sort(results);
    Assert.assertEquals(schemaName1, results.get(0).getSchema().getSchemaName());
    Assert.assertEquals(dbName, results.get(0).getSchema().getDbName());
    Assert.assertEquals(1, results.get(0).getVersion());
    Assert.assertEquals(schemaName1, results.get(1).getSchema().getSchemaName());
    Assert.assertEquals(dbName, results.get(1).getSchema().getDbName());
    Assert.assertEquals(2, results.get(1).getVersion());
    Assert.assertEquals(schemaName2, results.get(2).getSchema().getSchemaName());
    Assert.assertEquals(dbName, results.get(2).getSchema().getDbName());
    Assert.assertEquals(1, results.get(2).getVersion());
    Assert.assertEquals(schemaName2, results.get(3).getSchema().getSchemaName());
    Assert.assertEquals(dbName, results.get(3).getSchema().getDbName());
    Assert.assertEquals(2, results.get(3).getVersion());
    // fetch by namespace
    rqst = new FindSchemasByColsRqst();
    rqst.setColNamespace("namespace=x");
    rsp = client.getSchemaByCols(rqst);
    Assert.assertEquals(2, rsp.getSchemaVersionsSize());
    results = new ArrayList<>(rsp.getSchemaVersions());
    Collections.sort(results);
    Assert.assertEquals(schemaName1, results.get(0).getSchema().getSchemaName());
    Assert.assertEquals(dbName, results.get(0).getSchema().getDbName());
    Assert.assertEquals(2, results.get(0).getVersion());
    Assert.assertEquals(schemaName2, results.get(1).getSchema().getSchemaName());
    Assert.assertEquals(dbName, results.get(1).getSchema().getDbName());
    Assert.assertEquals(2, results.get(1).getVersion());
    // fetch by name and type
    rqst = new FindSchemasByColsRqst();
    rqst.setColName("alpha");
    rqst.setType(ColumnType.SMALLINT_TYPE_NAME);
    rsp = client.getSchemaByCols(rqst);
    Assert.assertEquals(2, rsp.getSchemaVersionsSize());
    results = new ArrayList<>(rsp.getSchemaVersions());
    Collections.sort(results);
    Assert.assertEquals(schemaName2, results.get(0).getSchema().getSchemaName());
    Assert.assertEquals(dbName, results.get(0).getSchema().getDbName());
    Assert.assertEquals(1, results.get(0).getVersion());
    Assert.assertEquals(schemaName2, results.get(1).getSchema().getSchemaName());
    Assert.assertEquals(dbName, results.get(1).getSchema().getDbName());
    Assert.assertEquals(2, results.get(1).getVersion());
    // Make sure matching name but wrong type doesn't return
    rqst = new FindSchemasByColsRqst();
    rqst.setColName("alpha");
    rqst.setType(ColumnType.STRING_TYPE_NAME);
    rsp = client.getSchemaByCols(rqst);
    Assert.assertEquals(0, rsp.getSchemaVersionsSize());
}
Also used : SchemaVersion(org.apache.hadoop.hive.metastore.api.SchemaVersion) ISchema(org.apache.hadoop.hive.metastore.api.ISchema) ArrayList(java.util.ArrayList) SchemaVersionDescriptor(org.apache.hadoop.hive.metastore.api.SchemaVersionDescriptor) FindSchemasByColsRqst(org.apache.hadoop.hive.metastore.api.FindSchemasByColsRqst) DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) ISchemaBuilder(org.apache.hadoop.hive.metastore.client.builder.ISchemaBuilder) Database(org.apache.hadoop.hive.metastore.api.Database) SchemaVersionBuilder(org.apache.hadoop.hive.metastore.client.builder.SchemaVersionBuilder) FindSchemasByColsResp(org.apache.hadoop.hive.metastore.api.FindSchemasByColsResp) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 10 with SchemaVersion

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

the class TestHiveMetaStoreSchemaMethods method createVersionOfNonExistentSchema.

@Test(expected = NoSuchObjectException.class)
public void createVersionOfNonExistentSchema() throws TException {
    SchemaVersion schemaVersion = new SchemaVersionBuilder().setSchemaName("noSchemaOfThisNameExists").setDbName(DEFAULT_DATABASE_NAME).setVersion(1).addCol("a", ColumnType.STRING_TYPE_NAME).build();
    client.addSchemaVersion(schemaVersion);
}
Also used : SchemaVersion(org.apache.hadoop.hive.metastore.api.SchemaVersion) SchemaVersionBuilder(org.apache.hadoop.hive.metastore.client.builder.SchemaVersionBuilder) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Aggregations

SchemaVersion (org.apache.hadoop.hive.metastore.api.SchemaVersion)24 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)19 SchemaVersionBuilder (org.apache.hadoop.hive.metastore.client.builder.SchemaVersionBuilder)19 Test (org.junit.Test)19 ISchema (org.apache.hadoop.hive.metastore.api.ISchema)16 ISchemaBuilder (org.apache.hadoop.hive.metastore.client.builder.ISchemaBuilder)16 ISchemaName (org.apache.hadoop.hive.metastore.api.ISchemaName)6 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)5 SchemaVersionDescriptor (org.apache.hadoop.hive.metastore.api.SchemaVersionDescriptor)5 Database (org.apache.hadoop.hive.metastore.api.Database)4 DatabaseBuilder (org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder)4 MSchemaVersion (org.apache.hadoop.hive.metastore.model.MSchemaVersion)4 ArrayList (java.util.ArrayList)3 Query (javax.jdo.Query)3 SerDeInfo (org.apache.hadoop.hive.metastore.api.SerDeInfo)3 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)2 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 FindSchemasByColsResp (org.apache.hadoop.hive.metastore.api.FindSchemasByColsResp)1