private HCatClient sourceMetaStore() throws HCatException { return HCatClient.create(new Configuration(hcatConf)); }
@Test public void testCreateTableLike() throws Exception { HCatClient client = HCatClient.create(new Configuration(hcatConf)); String tableName = "tableone"; String cloneTable = "tabletwo"; client.dropTable(null, tableName, true); client.dropTable(null, cloneTable, true); ArrayList<HCatFieldSchema> cols = new ArrayList<HCatFieldSchema>(); cols.add(new HCatFieldSchema("id", Type.INT, "id columns")); cols.add(new HCatFieldSchema("value", Type.STRING, "id columns")); HCatCreateTableDesc tableDesc = HCatCreateTableDesc .create(null, tableName, cols).fileFormat("rcfile").build(); client.createTable(tableDesc); // create a new table similar to previous one. client.createTableLike(null, tableName, cloneTable, true, false, null); List<String> tables = client.listTableNamesByPattern(null, "table*"); assertTrue(tables.size() == 2); client.close(); }
@Test public void testDatabaseLocation() throws Exception { HCatClient client = HCatClient.create(new Configuration(hcatConf)); String dbName = "locationDB"; client.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE); HCatCreateDBDesc dbDesc = HCatCreateDBDesc.create(dbName) .ifNotExists(true).location("/tmp/" + dbName).build(); client.createDatabase(dbDesc); HCatDatabase newDB = client.getDatabase(dbName); assertTrue(newDB.getLocation().matches(".*/tmp/" + dbName)); client.close(); }
@Test public void testGetMessageBusTopicName() throws Exception { try { HCatClient client = HCatClient.create(new Configuration(hcatConf)); String dbName = "testGetMessageBusTopicName_DBName"; String tableName = "testGetMessageBusTopicName_TableName"; client.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE); client.createDatabase(HCatCreateDBDesc.create(dbName).build()); String messageBusTopicName = "MY.topic.name"; Map<String, String> tableProperties = new HashMap<String, String>(1); tableProperties.put(HCatConstants.HCAT_MSGBUS_TOPIC_NAME, messageBusTopicName); client.createTable(HCatCreateTableDesc.create(dbName, tableName, Arrays.asList(new HCatFieldSchema("foo", Type.STRING, ""))).tblProps(tableProperties).build()); assertEquals("MessageBus topic-name doesn't match!", messageBusTopicName, client.getMessageBusTopicName(dbName, tableName)); client.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE); client.close(); } catch (Exception exception) { LOG.error("Unexpected exception.", exception); assertTrue("Unexpected exception:" + exception.getMessage(), false); } }
@Test public void testTransportFailure() throws Exception { HCatClient client = HCatClient.create(new Configuration(hcatConf)); boolean isExceptionCaught = false; assertEquals("Unexpected exception type.", HCatException.class, exp.getClass()); client = HCatClient.create(new Configuration(hcatConf)); String newName = "goodTable"; client.dropTable(null, newName, true);
@Test public void testUpdateTableSchema() throws Exception { try { HCatClient client = HCatClient.create(new Configuration(hcatConf)); final String dbName = "testUpdateTableSchema_DBName"; final String tableName = "testUpdateTableSchema_TableName"; client.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE); client.createDatabase(HCatCreateDBDesc.create(dbName).build()); List<HCatFieldSchema> oldSchema = Arrays.asList(new HCatFieldSchema("foo", Type.INT, ""), new HCatFieldSchema("bar", Type.STRING, "")); client.createTable(HCatCreateTableDesc.create(dbName, tableName, oldSchema).build()); List<HCatFieldSchema> newSchema = Arrays.asList(new HCatFieldSchema("completely", Type.DOUBLE, ""), new HCatFieldSchema("new", Type.STRING, ""), new HCatFieldSchema("fields", Type.STRING, "")); client.updateTableSchema(dbName, tableName, newSchema); assertArrayEquals(newSchema.toArray(), client.getTable(dbName, tableName).getCols().toArray()); client.dropDatabase(dbName, false, HCatClient.DropDBMode.CASCADE); } catch (Exception exception) { LOG.error("Unexpected exception.", exception); assertTrue("Unexpected exception: " + exception.getMessage(), false); } }
private HCatClient targetMetaStore() throws HCatException { return HCatClient.create(new Configuration(replicationTargetHCatConf)); }
@Test public void testRenameTable() throws Exception { HCatClient client = HCatClient.create(new Configuration(hcatConf)); String tableName = "temptable"; String newName = "mytable"; client.dropTable(null, tableName, true); client.dropTable(null, newName, true); ArrayList<HCatFieldSchema> cols = new ArrayList<HCatFieldSchema>(); cols.add(new HCatFieldSchema("id", Type.INT, "id columns")); cols.add(new HCatFieldSchema("value", Type.STRING, "id columns")); HCatCreateTableDesc tableDesc = HCatCreateTableDesc .create(null, tableName, cols).fileFormat("rcfile").build(); client.createTable(tableDesc); client.renameTable(null, tableName, newName); try { client.getTable(null, tableName); } catch (HCatException exp) { assertTrue("Unexpected exception message: " + exp.getMessage(), exp.getMessage().contains("NoSuchObjectException while fetching table")); } HCatTable newTable = client.getTable(null, newName); assertTrue(newTable != null); assertTrue(newTable.getTableName().equals(newName)); client.close(); }
@Test public void testPartitionSchema() throws Exception { try { HCatClient client = HCatClient.create(new Configuration(hcatConf)); final String dbName = "myDb"; final String tableName = "myTable"; client.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE); client.createDatabase(HCatCreateDBDesc.create(dbName).build()); List<HCatFieldSchema> columnSchema = Arrays.asList(new HCatFieldSchema("foo", Type.INT, ""), new HCatFieldSchema("bar", Type.STRING, "")); List<HCatFieldSchema> partitionSchema = Arrays.asList(new HCatFieldSchema("dt", Type.STRING, ""), new HCatFieldSchema("grid", Type.STRING, "")); client.createTable(HCatCreateTableDesc.create(dbName, tableName, columnSchema).partCols(partitionSchema).build()); HCatTable table = client.getTable(dbName, tableName); List<HCatFieldSchema> partitionColumns = table.getPartCols(); assertArrayEquals("Didn't get expected partition-schema back from the HCatTable.", partitionSchema.toArray(), partitionColumns.toArray()); client.dropDatabase(dbName, false, HCatClient.DropDBMode.CASCADE); } catch (Exception unexpected) { LOG.error("Unexpected exception!", unexpected); assertTrue("Unexpected exception! " + unexpected.getMessage(), false); } }
private long renewToken(Token token, String metaStoreURI, String hiveMetaStorePrincipal) { HCatClient hcatClient = null; if (UserGroupInformation.isSecurityEnabled()) { try { String tokenStr = token.encodeToUrlString(); HiveConf hcatConf = createHiveConf(metaStoreURI, hiveMetaStorePrincipal); LOG.debug("renewing delegation tokens for principal={}", hiveMetaStorePrincipal); hcatClient = HCatClient.create(hcatConf); Long expiryTime = hcatClient.renewDelegationToken(tokenStr); LOG.info("Renewed delegation token. new expiryTime={}", expiryTime); return expiryTime; } catch (Exception ex) { throw new RuntimeException("Failed to renew delegation tokens.", ex); } finally { if (hcatClient != null) try { hcatClient.close(); } catch (HCatException e) { LOG.error(" Exception", e); } } } else { throw new RuntimeException("Security is not enabled for Hadoop"); } }
@Test public void testOtherFailure() throws Exception { HCatClient client = HCatClient.create(new Configuration(hcatConf)); String tableName = "Temptable"; boolean isExceptionCaught = false;
@Test public void testGetPartitionsWithPartialSpec() throws Exception { try { HCatClient client = HCatClient.create(new Configuration(hcatConf)); final String dbName = "myDb"; final String tableName = "myTable";
private Token<DelegationTokenIdentifier> getDelegationToken(HiveConf hcatConf, String metaStoreServicePrincipal, String topologySubmitterUser) throws IOException { LOG.info("Creating delegation tokens for principal={}", metaStoreServicePrincipal); HCatClient hcatClient = null; try { hcatClient = HCatClient.create(hcatConf); String delegationToken = hcatClient.getDelegationToken(topologySubmitterUser, metaStoreServicePrincipal); Token<DelegationTokenIdentifier> delegationTokenId = new Token<DelegationTokenIdentifier>(); delegationTokenId.decodeFromUrlString(delegationToken); DelegationTokenIdentifier d = new DelegationTokenIdentifier(); d.readFields(new DataInputStream(new ByteArrayInputStream( delegationTokenId.getIdentifier()))); LOG.info("Created Delegation Token for : " + d.getUser()); return delegationTokenId; } finally { if (hcatClient != null) hcatClient.close(); } }
/** * This test tests that a plain table instantiation matches what hive says an * empty table create should look like. * @throws Exception */ @Test public void testEmptyTableInstantiation() throws Exception { HCatClient client = HCatClient.create(new Configuration(hcatConf)); String dbName = "default"; String tblName = "testEmptyCreate"; ArrayList<HCatFieldSchema> cols = new ArrayList<HCatFieldSchema>(); cols.add(new HCatFieldSchema("id", Type.INT, "id comment")); cols.add(new HCatFieldSchema("value", Type.STRING, "value comment")); client.dropTable(dbName, tblName, true); // Create a minimalistic table client.createTable(HCatCreateTableDesc .create(new HCatTable(dbName, tblName).cols(cols), false) .build()); HCatTable tCreated = client.getTable(dbName, tblName); org.apache.hadoop.hive.metastore.api.Table emptyTable = Table.getEmptyTable(dbName, tblName); Map<String, String> createdProps = tCreated.getTblProps(); Map<String, String> emptyProps = emptyTable.getParameters(); mapEqualsContainedIn(emptyProps, createdProps); // Test sd params - we check that all the parameters in an empty table // are retained as-is. We may add beyond it, but not change values for // any parameters that hive defines for an empty table. Map<String, String> createdSdParams = tCreated.getSerdeParams(); Map<String, String> emptySdParams = emptyTable.getSd().getSerdeInfo().getParameters(); mapEqualsContainedIn(emptySdParams, createdSdParams); }
try { HCatClient client = HCatClient.create(new Configuration(hcatConf)); String dbName = "testObjectNotFoundException_DBName"; String tableName = "testObjectNotFoundException_TableName";
@BeforeClass public static void setUpBeforeClass() throws HCatException { client = HCatClient.create(new HiveConf()); ReplicationTask.resetFactory(EximReplicationTaskFactory.class); }
@Test public void testDropPartitionsWithPartialSpec() throws Exception { try { HCatClient client = HCatClient.create(new Configuration(hcatConf)); final String dbName = "myDb"; final String tableName = "myTable";
@BeforeClass public static void setUpBeforeClass() throws Exception { TestHCatClient.startMetaStoreServer(); hconf = TestHCatClient.getConf(); hconf.set(HiveConf.ConfVars.SEMANTIC_ANALYZER_HOOK.varname,""); hconf .setVar(HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER, "org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdHiveAuthorizerFactory"); TEST_PATH = System.getProperty("test.warehouse.dir","/tmp") + Path.SEPARATOR + TestCommands.class.getCanonicalName() + "-" + System.currentTimeMillis(); Path testPath = new Path(TEST_PATH); FileSystem fs = FileSystem.get(testPath.toUri(),hconf); fs.mkdirs(testPath); driver = DriverFactory.newDriver(hconf); SessionState.start(new CliSessionState(hconf)); client = HCatClient.create(hconf); }
@Test public void testDropTableException() throws Exception { HCatClient client = HCatClient.create(new Configuration(hcatConf)); String tableName = "tableToBeDropped"; boolean isExceptionCaught = false; client.dropTable(null, tableName, true); try { client.dropTable(null, tableName, false); } catch (Exception exp) { isExceptionCaught = true; assertTrue(exp instanceof HCatException); LOG.info("Drop Table Exception: " + exp.getCause()); } finally { client.close(); assertTrue("The expected exception was never thrown.", isExceptionCaught); } }
@Test public static void testCreate() throws HCatException { Table t = new Table(); t.setDbName("testdb"); t.setTableName("testtable"); NotificationEvent event = new NotificationEvent(0, (int)System.currentTimeMillis(), HCatConstants.HCAT_CREATE_TABLE_EVENT, msgFactory.buildCreateTableMessage(t).toString()); event.setDbName(t.getDbName()); event.setTableName(t.getTableName()); ReplicationTask.resetFactory(null); ReplicationTask rtask = ReplicationTask.create(HCatClient.create(new HiveConf()),new HCatNotificationEvent(event)); assertTrue("Provided factory instantiation should yield CreateTableReplicationTask", rtask instanceof CreateTableReplicationTask); ReplicationTask.resetFactory(NoopFactory.class); rtask = ReplicationTask.create(HCatClient.create(new HiveConf()),new HCatNotificationEvent(event)); assertTrue("Provided factory instantiation should yield NoopReplicationTask", rtask instanceof NoopReplicationTask); ReplicationTask.resetFactory(null); }