Search in sources :

Example 11 with DatabaseBuilder

use of org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder 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 12 with DatabaseBuilder

use of org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder in project hive by apache.

the class TestHiveMetaStoreSchemaMethods method addSchemaVersionOtherDb.

@Test
public void addSchemaVersionOtherDb() throws TException {
    String dbName = "other_db_for_schema_version";
    Database db = new DatabaseBuilder().setName(dbName).build();
    client.createDatabase(db);
    String schemaName = uniqueSchemaName();
    int version = 1;
    ISchema schema = new ISchemaBuilder().setDbName(dbName).setSchemaType(SchemaType.AVRO).setName(schemaName).build();
    client.createISchema(schema);
    String description = "very descriptive";
    String schemaText = "this should look like json, but oh well";
    String fingerprint = "this should be an md5 string";
    String versionName = "why would I name a version?";
    long creationTime = 10;
    String serdeName = "serde_for_schema37";
    String serializer = "org.apache.hadoop.hive.metastore.test.Serializer";
    String deserializer = "org.apache.hadoop.hive.metastore.test.Deserializer";
    String serdeDescription = "how do you describe a serde?";
    SchemaVersion schemaVersion = new SchemaVersionBuilder().versionOf(schema).setVersion(version).addCol("a", ColumnType.INT_TYPE_NAME).addCol("b", ColumnType.FLOAT_TYPE_NAME).setCreatedAt(creationTime).setState(SchemaVersionState.INITIATED).setDescription(description).setSchemaText(schemaText).setFingerprint(fingerprint).setName(versionName).setSerdeName(serdeName).setSerdeSerializerClass(serializer).setSerdeDeserializerClass(deserializer).setSerdeDescription(serdeDescription).build();
    client.addSchemaVersion(schemaVersion);
    schemaVersion = client.getSchemaVersion(dbName, schemaName, version);
    Assert.assertNotNull(schemaVersion);
    Assert.assertEquals(schemaName, schemaVersion.getSchema().getSchemaName());
    Assert.assertEquals(dbName, schemaVersion.getSchema().getDbName());
    Assert.assertEquals(version, schemaVersion.getVersion());
    Assert.assertEquals(creationTime, schemaVersion.getCreatedAt());
    Assert.assertEquals(SchemaVersionState.INITIATED, schemaVersion.getState());
    Assert.assertEquals(description, schemaVersion.getDescription());
    Assert.assertEquals(schemaText, schemaVersion.getSchemaText());
    Assert.assertEquals(fingerprint, schemaVersion.getFingerprint());
    Assert.assertEquals(versionName, schemaVersion.getName());
    Assert.assertEquals(serdeName, schemaVersion.getSerDe().getName());
    Assert.assertEquals(serializer, schemaVersion.getSerDe().getSerializerClass());
    Assert.assertEquals(deserializer, schemaVersion.getSerDe().getDeserializerClass());
    Assert.assertEquals(serdeDescription, schemaVersion.getSerDe().getDescription());
    Assert.assertEquals(2, schemaVersion.getColsSize());
    List<FieldSchema> cols = schemaVersion.getCols();
    Collections.sort(cols);
    Assert.assertEquals("a", cols.get(0).getName());
    Assert.assertEquals(ColumnType.INT_TYPE_NAME, cols.get(0).getType());
    Assert.assertEquals("b", cols.get(1).getName());
    Assert.assertEquals(ColumnType.FLOAT_TYPE_NAME, cols.get(1).getType());
    Assert.assertEquals(1, (int) preEvents.get(PreEventContext.PreEventType.READ_SCHEMA_VERSION));
    client.dropSchemaVersion(dbName, schemaName, version);
    try {
        client.getSchemaVersion(dbName, schemaName, version);
        Assert.fail();
    } catch (NoSuchObjectException e) {
    // all good
    }
}
Also used : DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) ISchemaBuilder(org.apache.hadoop.hive.metastore.client.builder.ISchemaBuilder) SchemaVersion(org.apache.hadoop.hive.metastore.api.SchemaVersion) ISchema(org.apache.hadoop.hive.metastore.api.ISchema) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) Database(org.apache.hadoop.hive.metastore.api.Database) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) SchemaVersionBuilder(org.apache.hadoop.hive.metastore.client.builder.SchemaVersionBuilder) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 13 with DatabaseBuilder

use of org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder in project hive by apache.

the class TestHiveMetaStoreSchemaMethods method iSchemaOtherDatabase.

@Test
public void iSchemaOtherDatabase() throws TException {
    String dbName = "other_db";
    Database db = new DatabaseBuilder().setName(dbName).build();
    client.createDatabase(db);
    String schemaName = uniqueSchemaName();
    String schemaGroup = "group1";
    String description = "This is a description";
    ISchema schema = new ISchemaBuilder().setSchemaType(SchemaType.AVRO).setName(schemaName).setDbName(dbName).setCompatibility(SchemaCompatibility.FORWARD).setValidationLevel(SchemaValidation.LATEST).setCanEvolve(false).setSchemaGroup(schemaGroup).setDescription(description).build();
    client.createISchema(schema);
    schema = client.getISchema(dbName, schemaName);
    Assert.assertEquals(SchemaType.AVRO, schema.getSchemaType());
    Assert.assertEquals(schemaName, schema.getName());
    Assert.assertEquals(dbName, schema.getDbName());
    Assert.assertEquals(SchemaCompatibility.FORWARD, schema.getCompatibility());
    Assert.assertEquals(SchemaValidation.LATEST, schema.getValidationLevel());
    Assert.assertFalse(schema.isCanEvolve());
    Assert.assertEquals(schemaGroup, schema.getSchemaGroup());
    Assert.assertEquals(description, schema.getDescription());
    schemaGroup = "new group";
    description = "new description";
    schema.setCompatibility(SchemaCompatibility.BOTH);
    schema.setValidationLevel(SchemaValidation.ALL);
    schema.setCanEvolve(true);
    schema.setSchemaGroup(schemaGroup);
    schema.setDescription(description);
    client.alterISchema(dbName, schemaName, schema);
    schema = client.getISchema(dbName, schemaName);
    Assert.assertEquals(SchemaType.AVRO, schema.getSchemaType());
    Assert.assertEquals(schemaName, schema.getName());
    Assert.assertEquals(dbName, schema.getDbName());
    Assert.assertEquals(SchemaCompatibility.BOTH, schema.getCompatibility());
    Assert.assertEquals(SchemaValidation.ALL, schema.getValidationLevel());
    Assert.assertTrue(schema.isCanEvolve());
    Assert.assertEquals(schemaGroup, schema.getSchemaGroup());
    Assert.assertEquals(description, schema.getDescription());
    client.dropISchema(dbName, schemaName);
    try {
        client.getISchema(dbName, schemaName);
        Assert.fail();
    } catch (NoSuchObjectException e) {
    // all good
    }
}
Also used : DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) ISchemaBuilder(org.apache.hadoop.hive.metastore.client.builder.ISchemaBuilder) ISchema(org.apache.hadoop.hive.metastore.api.ISchema) Database(org.apache.hadoop.hive.metastore.api.Database) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 14 with DatabaseBuilder

use of org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder in project hive by apache.

the class TestHiveMetaStoreSchemaMethods method mapSerdeToSchemaVersionOtherDb.

@Test
public void mapSerdeToSchemaVersionOtherDb() throws TException {
    String dbName = "map_other_db";
    Database db = new DatabaseBuilder().setName(dbName).build();
    client.createDatabase(db);
    ISchema schema = new ISchemaBuilder().setSchemaType(SchemaType.AVRO).setDbName(dbName).setName(uniqueSchemaName()).build();
    client.createISchema(schema);
    // Create schema with no serde, then map it
    SchemaVersion schemaVersion = new SchemaVersionBuilder().versionOf(schema).setVersion(1).addCol("x", ColumnType.BOOLEAN_TYPE_NAME).build();
    client.addSchemaVersion(schemaVersion);
    SerDeInfo serDeInfo = new SerDeInfo(uniqueSerdeName(), "lib", Collections.emptyMap());
    client.addSerDe(serDeInfo);
    client.mapSchemaVersionToSerde(dbName, schema.getName(), schemaVersion.getVersion(), serDeInfo.getName());
    schemaVersion = client.getSchemaVersion(dbName, schema.getName(), schemaVersion.getVersion());
    Assert.assertEquals(serDeInfo.getName(), schemaVersion.getSerDe().getName());
    // Create schema with a serde, then remap it
    String serDeName = uniqueSerdeName();
    schemaVersion = new SchemaVersionBuilder().versionOf(schema).setVersion(2).addCol("x", ColumnType.BOOLEAN_TYPE_NAME).setSerdeName(serDeName).setSerdeLib("x").build();
    client.addSchemaVersion(schemaVersion);
    schemaVersion = client.getSchemaVersion(dbName, schema.getName(), 2);
    Assert.assertEquals(serDeName, schemaVersion.getSerDe().getName());
    serDeInfo = new SerDeInfo(uniqueSerdeName(), "y", Collections.emptyMap());
    client.addSerDe(serDeInfo);
    client.mapSchemaVersionToSerde(dbName, schema.getName(), 2, serDeInfo.getName());
    schemaVersion = client.getSchemaVersion(dbName, schema.getName(), 2);
    Assert.assertEquals(serDeInfo.getName(), schemaVersion.getSerDe().getName());
}
Also used : DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) ISchemaBuilder(org.apache.hadoop.hive.metastore.client.builder.ISchemaBuilder) SchemaVersion(org.apache.hadoop.hive.metastore.api.SchemaVersion) ISchema(org.apache.hadoop.hive.metastore.api.ISchema) SerDeInfo(org.apache.hadoop.hive.metastore.api.SerDeInfo) Database(org.apache.hadoop.hive.metastore.api.Database) SchemaVersionBuilder(org.apache.hadoop.hive.metastore.client.builder.SchemaVersionBuilder) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 15 with DatabaseBuilder

use of org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder in project hive by apache.

the class SmokeTest method runTest.

private void runTest(IMetaStoreClient client) throws TException {
    LOG.info("Starting smoke test");
    File dbDir = new File(System.getProperty("java.io.tmpdir"), "internal_smoke_test");
    if (!dbDir.mkdir()) {
        throw new RuntimeException("Unable to create direcotory " + dbDir.getAbsolutePath());
    }
    dbDir.deleteOnExit();
    LOG.info("Going to create database " + dbName);
    Database db = new DatabaseBuilder().setName(dbName).setLocation(dbDir.getAbsolutePath()).build();
    client.createDatabase(db);
    LOG.info("Going to create table " + tableName);
    Table table = new TableBuilder().setDbName(db).setTableName(tableName).addCol("col1", ColumnType.INT_TYPE_NAME).addCol("col2", ColumnType.TIMESTAMP_TYPE_NAME).addPartCol("pcol1", ColumnType.STRING_TYPE_NAME).build();
    client.createTable(table);
    LOG.info("Going to create partition with value " + partValue);
    Partition part = new PartitionBuilder().fromTable(table).addValue("val1").build();
    client.add_partition(part);
    LOG.info("Going to list the partitions");
    List<Partition> parts = client.listPartitions(dbName, tableName, (short) -1);
    LOG.info("Fetched: { " + parts.toString() + "}");
    LOG.info("Going to drop database");
    client.dropDatabase(dbName, true, false, true);
    LOG.info("Completed smoke test");
}
Also used : DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) Partition(org.apache.hadoop.hive.metastore.api.Partition) Table(org.apache.hadoop.hive.metastore.api.Table) PartitionBuilder(org.apache.hadoop.hive.metastore.client.builder.PartitionBuilder) Database(org.apache.hadoop.hive.metastore.api.Database) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) File(java.io.File)

Aggregations

DatabaseBuilder (org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder)31 Database (org.apache.hadoop.hive.metastore.api.Database)26 Test (org.junit.Test)12 TableBuilder (org.apache.hadoop.hive.metastore.client.builder.TableBuilder)9 Before (org.junit.Before)9 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)8 Partition (org.apache.hadoop.hive.metastore.api.Partition)6 Table (org.apache.hadoop.hive.metastore.api.Table)6 PartitionBuilder (org.apache.hadoop.hive.metastore.client.builder.PartitionBuilder)6 ISchema (org.apache.hadoop.hive.metastore.api.ISchema)5 ISchemaBuilder (org.apache.hadoop.hive.metastore.client.builder.ISchemaBuilder)5 SchemaVersion (org.apache.hadoop.hive.metastore.api.SchemaVersion)4 SchemaVersionBuilder (org.apache.hadoop.hive.metastore.client.builder.SchemaVersionBuilder)4 MetastoreUnitTest (org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest)3 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)2 ListenerEvent (org.apache.hadoop.hive.metastore.events.ListenerEvent)2 File (java.io.File)1