Esempio n. 1
0
  @Test
  public void testBasicDDLCommands() throws Exception {
    String db = "testdb";
    String tableOne = "testTable1";
    String tableTwo = "testTable2";
    HCatClient client = HCatClient.create(new Configuration(hcatConf));
    client.dropDatabase(db, true, HCatClient.DropDBMode.CASCADE);

    HCatCreateDBDesc dbDesc = HCatCreateDBDesc.create(db).ifNotExists(false).build();
    client.createDatabase(dbDesc);
    List<String> dbNames = client.listDatabaseNamesByPattern("*");
    assertTrue(dbNames.contains("default"));
    assertTrue(dbNames.contains(db));

    HCatDatabase testDb = client.getDatabase(db);
    assertTrue(testDb.getComment() == null);
    assertTrue(testDb.getProperties().size() == 0);
    String warehouseDir =
        System.getProperty(ConfVars.METASTOREWAREHOUSE.varname, "/user/hive/warehouse");
    assertTrue(testDb.getLocation().equals("file:" + warehouseDir + "/" + db + ".db"));
    ArrayList<HCatFieldSchema> cols = new ArrayList<HCatFieldSchema>();
    cols.add(new HCatFieldSchema("id", Type.INT, "id comment"));
    cols.add(new HCatFieldSchema("value", Type.STRING, "value comment"));
    HCatCreateTableDesc tableDesc =
        HCatCreateTableDesc.create(db, tableOne, cols).fileFormat("rcfile").build();
    client.createTable(tableDesc);
    HCatTable table1 = client.getTable(db, tableOne);
    assertTrue(table1.getInputFileFormat().equalsIgnoreCase(RCFileInputFormat.class.getName()));
    assertTrue(table1.getOutputFileFormat().equalsIgnoreCase(RCFileOutputFormat.class.getName()));
    assertTrue(table1.getSerdeLib().equalsIgnoreCase(ColumnarSerDe.class.getName()));
    assertTrue(table1.getCols().equals(cols));
    // Since "ifexists" was not set to true, trying to create the same table
    // again
    // will result in an exception.
    try {
      client.createTable(tableDesc);
    } catch (HCatException e) {
      assertTrue(e.getMessage().contains("AlreadyExistsException while creating table."));
    }

    client.dropTable(db, tableOne, true);
    HCatCreateTableDesc tableDesc2 = HCatCreateTableDesc.create(db, tableTwo, cols).build();
    client.createTable(tableDesc2);
    HCatTable table2 = client.getTable(db, tableTwo);
    assertTrue(table2.getInputFileFormat().equalsIgnoreCase(TextInputFormat.class.getName()));
    assertTrue(
        table2.getOutputFileFormat().equalsIgnoreCase(IgnoreKeyTextOutputFormat.class.getName()));
    assertTrue(
        table2
            .getLocation()
            .equalsIgnoreCase("file:" + warehouseDir + "/" + db + ".db/" + tableTwo));
    client.close();
  }
Esempio n. 2
0
  @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().equalsIgnoreCase("file:/tmp/" + dbName));
    client.close();
  }
Esempio n. 3
0
  @Test
  public void testObjectNotFoundException() throws Exception {
    try {

      HCatClient client = HCatClient.create(new Configuration(hcatConf));
      String dbName = "testObjectNotFoundException_DBName";
      String tableName = "testObjectNotFoundException_TableName";
      client.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE);

      try { // Test that fetching a non-existent db-name yields ObjectNotFound.
        client.getDatabase(dbName);
        assertTrue("Expected ObjectNotFoundException.", false);
      } catch (Exception exception) {
        LOG.info("Got exception: ", exception);
        assertTrue(
            "Expected ObjectNotFoundException. Got:" + exception.getClass(),
            exception instanceof ObjectNotFoundException);
      }

      client.createDatabase(HCatCreateDBDesc.create(dbName).build());

      try { // Test that fetching a non-existent table-name yields ObjectNotFound.
        client.getTable(dbName, tableName);
        assertTrue("Expected ObjectNotFoundException.", false);
      } catch (Exception exception) {
        LOG.info("Got exception: ", exception);
        assertTrue(
            "Expected ObjectNotFoundException. Got:" + exception.getClass(),
            exception instanceof ObjectNotFoundException);
      }

      String partitionColumn = "part";

      List<HCatFieldSchema> columns = Arrays.asList(new HCatFieldSchema("col", Type.STRING, ""));
      ArrayList<HCatFieldSchema> partitionColumns =
          new ArrayList<HCatFieldSchema>(
              Arrays.asList(new HCatFieldSchema(partitionColumn, Type.STRING, "")));
      client.createTable(
          HCatCreateTableDesc.create(dbName, tableName, columns)
              .partCols(partitionColumns)
              .build());

      Map<String, String> partitionSpec = new HashMap<String, String>();
      partitionSpec.put(partitionColumn, "foobar");
      try { // Test that fetching a non-existent partition yields ObjectNotFound.
        client.getPartition(dbName, tableName, partitionSpec);
        assertTrue("Expected ObjectNotFoundException.", false);
      } catch (Exception exception) {
        LOG.info("Got exception: ", exception);
        assertTrue(
            "Expected ObjectNotFoundException. Got:" + exception.getClass(),
            exception instanceof ObjectNotFoundException);
      }

      client.addPartition(
          HCatAddPartitionDesc.create(dbName, tableName, "", partitionSpec).build());

      // Test that listPartitionsByFilter() returns an empty-set, if the filter selects no
      // partitions.
      assertEquals(
          "Expected empty set of partitions.",
          0,
          client.listPartitionsByFilter(dbName, tableName, partitionColumn + " < 'foobar'").size());

      try { // Test that listPartitionsByFilter() throws HCatException if the partition-key is
        // incorrect.
        partitionSpec.put("NonExistentKey", "foobar");
        client.getPartition(dbName, tableName, partitionSpec);
        assertTrue("Expected HCatException.", false);
      } catch (Exception exception) {
        LOG.info("Got exception: ", exception);
        assertTrue(
            "Expected HCatException. Got:" + exception.getClass(),
            exception instanceof HCatException);
        assertFalse(
            "Did not expect ObjectNotFoundException.",
            exception instanceof ObjectNotFoundException);
      }

    } catch (Throwable t) {
      LOG.error("Unexpected exception!", t);
      assertTrue("Unexpected exception! " + t.getMessage(), false);
    }
  }