Exemplo n.º 1
0
 @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();
 }
Exemplo n.º 2
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();
  }