Database db = new Database();
db.setName(dbName);
client.createDatabase(db);
ArrayList<FieldSchema> invCols = new ArrayList<FieldSchema>(2);
invCols.add(new FieldSchema("n-ame", serdeConstants.STRING_TYPE_NAME, ""));
invCols.add(new FieldSchema("in.come", serdeConstants.INT_TYPE_NAME, ""));
Table tbl = new Table();
tbl.setDbName(dbName);
tbl.setTableName(invTblName);
StorageDescriptor sd = new StorageDescriptor();
tbl.setSd(sd);
sd.setCols(invCols);
sd.setCompressed(false);
sd.setNumBuckets(1);
sd.setParameters(new HashMap<String, String>());
sd.getParameters().put("test_param_1", "Use this for comments etc");
sd.setBucketCols(new ArrayList<String>(2));
sd.getBucketCols().add("name");
sd.setSerdeInfo(new SerDeInfo());
sd.getSerdeInfo().setName(tbl.getTableName());
sd.getSerdeInfo().setParameters(new HashMap<String, String>());
sd.getSerdeInfo().getParameters().put(
org.apache.hadoop.hive.serde.serdeConstants.SERIALIZATION_FORMAT, "1");
boolean failed = false;
try {
client.createTable(tbl);
} catch (InvalidObjectException ex) {
failed = true;
}
if (!failed) {
assertTrue("Able to create table with invalid name: " + invTblName,
false);
}
// create an invalid table which has wrong column type
ArrayList<FieldSchema> invColsInvType = new ArrayList<FieldSchema>(2);
invColsInvType.add(new FieldSchema("name", serdeConstants.STRING_TYPE_NAME, ""));
invColsInvType.add(new FieldSchema("income", "xyz", ""));
tbl.setTableName(tblName);
tbl.getSd().setCols(invColsInvType);
boolean failChecker = false;
try {
client.createTable(tbl);
} catch (InvalidObjectException ex) {
failChecker = true;
}
if (!failChecker) {
assertTrue("Able to create table with invalid column type: " + invTblName,
false);
}
ArrayList<FieldSchema> cols = new ArrayList<FieldSchema>(2);
cols.add(new FieldSchema("name", serdeConstants.STRING_TYPE_NAME, ""));
cols.add(new FieldSchema("income", serdeConstants.INT_TYPE_NAME, ""));
// create a valid table
tbl.setTableName(tblName);
tbl.getSd().setCols(cols);
client.createTable(tbl);