Java Code Examples for org.apache.hadoop.hive.metastore.HiveMetaStoreClient#getDatabase()

The following examples show how to use org.apache.hadoop.hive.metastore.HiveMetaStoreClient#getDatabase() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HiveServer2CoreTest.java    From beeju with Apache License 2.0 6 votes vote down vote up
@Test
public void dropDatabase() throws Exception {
  HiveServer2Core server = setupServer();
  String databaseName = "Another_DB";

  server.getCore().createDatabase(databaseName);
  try (Connection connection = DriverManager.getConnection(server.getJdbcConnectionUrl());
      Statement statement = connection.createStatement()) {
    String dropHql = String.format("DROP DATABASE %s", databaseName);
    statement.execute(dropHql);
  }

  HiveMetaStoreClient client = server.getCore().newClient();
  try {
    client.getDatabase(databaseName);
    fail(String.format("Database %s was not deleted", databaseName));
  } catch (NoSuchObjectException e) {
    // expected
  } finally {
    client.close();
  }
  server.shutdown();
}
 
Example 2
Source File: WaggleDanceIntegrationTest.java    From waggle-dance with Apache License 2.0 6 votes vote down vote up
@Test
public void createDatabaseUsingManualAndWhitelistingUpdatesConfig() throws Exception {
  runner = WaggleDanceRunner
      .builder(configLocation)
      .primary("primary", localServer.getThriftConnectionUri(),
          AccessControlType.READ_AND_WRITE_AND_CREATE_ON_DATABASE_WHITELIST)
      .build();

  runWaggleDance(runner);

  HiveMetaStoreClient proxy = getWaggleDanceClient();
  proxy.createDatabase(new Database("newDB", "", new File(localWarehouseUri, "newDB").toURI().toString(), null));
  Database newDB = proxy.getDatabase("newDB");
  assertNotNull(newDB);

  // Should be allowed due to newly loaded write privileges
  proxy.alterDatabase("newDB", new Database(newDB));

  Federations federations = stopServerAndGetConfiguration();
  PrimaryMetaStore metaStore = federations.getPrimaryMetaStore();
  assertThat(metaStore.getWritableDatabaseWhiteList().size(), is(1));
  assertThat(metaStore.getWritableDatabaseWhiteList().get(0), is("newdb"));
  // Mapped databases should still map everything
  assertThat(metaStore.getMappedDatabases(), is(nullValue()));
}
 
Example 3
Source File: WaggleDanceIntegrationTest.java    From waggle-dance with Apache License 2.0 6 votes vote down vote up
@Test
public void createDatabaseDatabaseUsingPrefixAndWhitelistingUpdates() throws Exception {
  runner = WaggleDanceRunner
      .builder(configLocation)
      .databaseResolution(DatabaseResolution.PREFIXED)
      .primary("primary", localServer.getThriftConnectionUri(),
          AccessControlType.READ_AND_WRITE_AND_CREATE_ON_DATABASE_WHITELIST)
      .build();

  runWaggleDance(runner);

  HiveMetaStoreClient proxy = getWaggleDanceClient();
  proxy.createDatabase(new Database("newDB", "", new File(localWarehouseUri, "newDB").toURI().toString(), null));
  Database newDB = proxy.getDatabase("newDB");
  assertNotNull(newDB);

  // Should be allowed due to newly loaded write privileges
  proxy.alterDatabase("newDB", new Database(newDB));

  Federations federations = stopServerAndGetConfiguration();
  PrimaryMetaStore metaStore = federations.getPrimaryMetaStore();
  assertThat(metaStore.getWritableDatabaseWhiteList().size(), is(1));
  assertThat(metaStore.getWritableDatabaseWhiteList().get(0), is("newdb"));
  // Mapped databases should still map everything
  assertThat(metaStore.getMappedDatabases(), is(nullValue()));
}
 
Example 4
Source File: BeejuCoreTest.java    From beeju with Apache License 2.0 5 votes vote down vote up
@Test
public void createDatabase() throws Exception {
  String databaseName = "Another_DB";

  defaultCore.createDatabase(databaseName);
  HiveMetaStoreClient client = defaultCore.newClient();
  Database db = client.getDatabase(databaseName);
  client.close();

  assertThat(db, is(notNullValue()));
  assertThat(db.getName(), is(databaseName.toLowerCase()));
  assertThat(db.getLocationUri(), is(String.format("file:%s/%s", defaultCore.tempDir(), databaseName)));
}
 
Example 5
Source File: WaggleDanceIntegrationTest.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
@Test
public void readWriteCreateAllowed() throws Exception {
  String writableDatabase = "writable_db";

  localServer.createDatabase(writableDatabase);

  runner = WaggleDanceRunner
      .builder(configLocation)
      .primary("primary", localServer.getThriftConnectionUri(), AccessControlType.READ_AND_WRITE_AND_CREATE)
      .build();

  runWaggleDance(runner);

  HiveMetaStoreClient proxy = getWaggleDanceClient();
  // create rights
  proxy.createDatabase(new Database("newDB", "", new File(localWarehouseUri, "newDB").toURI().toString(), null));
  Database newDB = proxy.getDatabase("newDB");
  assertNotNull(newDB);

  // read rights
  Table localTable = localServer.client().getTable(LOCAL_DATABASE, LOCAL_TABLE);
  Table waggledLocalTable = proxy.getTable(LOCAL_DATABASE, LOCAL_TABLE);
  assertThat(waggledLocalTable, is(localTable));

  // write rights
  proxy.dropTable(LOCAL_DATABASE, LOCAL_TABLE);
  try {
    proxy.getTable(LOCAL_DATABASE, LOCAL_TABLE);
    fail("Should get NoSuchObjectException");
  } catch (NoSuchObjectException e) {
    // Local table should be allowed to drop, so it now no longer exists and we get an appropriate exception
  }
}
 
Example 6
Source File: WaggleDanceIntegrationTest.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
@Test
public void readWriteCreateAllowedPrefixed() throws Exception {
  String writableDatabase = "writable_db";

  localServer.createDatabase(writableDatabase);

  runner = WaggleDanceRunner
      .builder(configLocation)
      .databaseResolution(DatabaseResolution.PREFIXED)
      .primary("primary", localServer.getThriftConnectionUri(), AccessControlType.READ_AND_WRITE_AND_CREATE)
      .build();

  runWaggleDance(runner);

  HiveMetaStoreClient proxy = getWaggleDanceClient();
  // create rights
  proxy.createDatabase(new Database("newDB", "", new File(localWarehouseUri, "newDB").toURI().toString(), null));
  Database newDB = proxy.getDatabase("newDB");
  assertNotNull(newDB);

  // read rights
  Table localTable = localServer.client().getTable(LOCAL_DATABASE, LOCAL_TABLE);
  Table waggledLocalTable = proxy.getTable(LOCAL_DATABASE, LOCAL_TABLE);
  assertThat(waggledLocalTable, is(localTable));

  // write rights
  proxy.dropTable(LOCAL_DATABASE, LOCAL_TABLE);
  try {
    proxy.getTable(LOCAL_DATABASE, LOCAL_TABLE);
    fail("Should get NoSuchObjectException");
  } catch (NoSuchObjectException e) {
    // Local table should be allowed to drop, so it now no longer exists and we get an appropriate exception
  }
}
 
Example 7
Source File: WaggleDanceIntegrationTest.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
@Test
public void databaseWhitelisting() throws Exception {
  String writableDatabase = "writable_db";

  localServer.createDatabase(writableDatabase);

  runner = WaggleDanceRunner
      .builder(configLocation)
      .primary("primary", localServer.getThriftConnectionUri(), READ_AND_WRITE_ON_DATABASE_WHITELIST,
          writableDatabase)
      .build();

  runWaggleDance(runner);

  HiveMetaStoreClient proxy = getWaggleDanceClient();
  Database readOnlyDB = proxy.getDatabase(LOCAL_DATABASE);
  assertNotNull(readOnlyDB);
  Database writableDB = proxy.getDatabase(writableDatabase);
  assertNotNull(writableDB);
  try {
    proxy.alterDatabase(LOCAL_DATABASE, new Database(readOnlyDB));
    fail("Alter DB should not be allowed");
  } catch (MetaException e) {
    // expected
    proxy.reconnect();
  }
  proxy.alterDatabase(writableDatabase, new Database(writableDB));
}
 
Example 8
Source File: WaggleDanceIntegrationTest.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
@Test
public void doesNotOverwriteConfigOnShutdownManualMode() throws Exception {
  // Note a similar test for PREFIX is not required
  runner = WaggleDanceRunner
      .builder(configLocation)
      .databaseResolution(DatabaseResolution.MANUAL)
      .overwriteConfigOnShutdown(false)
      .primary("primary", localServer.getThriftConnectionUri(),
          AccessControlType.READ_AND_WRITE_AND_CREATE_ON_DATABASE_WHITELIST)
      .federate(SECONDARY_METASTORE_NAME, remoteServer.getThriftConnectionUri(), REMOTE_DATABASE)
      .build();

  runWaggleDance(runner);

  HiveMetaStoreClient proxy = getWaggleDanceClient();
  proxy.createDatabase(new Database("newDB", "", new File(localWarehouseUri, "newDB").toURI().toString(), null));
  Database newDB = proxy.getDatabase("newDB");
  assertNotNull(newDB);

  Federations federations = stopServerAndGetConfiguration();

  PrimaryMetaStore primaryMetastore = federations.getPrimaryMetaStore();
  List<String> writableDatabases = primaryMetastore.getWritableDatabaseWhiteList();
  assertThat(writableDatabases.size(), is(0));

  // Double check federated metastores
  List<FederatedMetaStore> federatedMetastores = federations.getFederatedMetaStores();
  assertThat(federatedMetastores.size(), is(1));

  List<String> mappedDatabases = federatedMetastores.get(0).getMappedDatabases();
  assertThat(mappedDatabases.size(), is(1));
  assertThat(mappedDatabases.get(0), is(REMOTE_DATABASE));
}
 
Example 9
Source File: TestAuthorizingObjectStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
/**
 * The configuration "sentry.metastore.service.users" is for user who has all
 * access to metadata, and the value should be case-sensitive.
 * 
 * @throws Exception
 */
@Test
public void testPrivilegesForUserNameCaseSensitive() throws Exception {
  // The value of "sentry.metastore.service.users" is "accessAllMetaUser", and
  // the value is case-sensitive.
  // The input name is "ACCESSALLMEATAUSER", and the client should has no
  // access to metadata.
  HiveMetaStoreClient client = context.getMetaStoreClient(userWithoutAccess.toUpperCase());
  try {
    client.getDatabase(dbName1);
    fail("NoSuchObjectException should have been thrown");
  } catch (NoSuchObjectException noe) {
    // ignore, just make sure the authorization is failed.
  }
}