* exist, and will create families if they don't exist.
*/
public void createOrMigrateSchema(String tableName,
String entitySchemaString, boolean createTableAndFamilies) {
String entityName = getEntityNameFromSchemaString(entitySchemaString);
AvroEntitySchema entitySchema = parser
.parseEntitySchema(entitySchemaString);
AvroKeySchema keySchema = parser.parseKeySchema(entitySchemaString);
if (schemaManager.hasManagedSchema(tableName, entityName)) {
KeySchema currentKeySchema = schemaManager.getKeySchema(tableName,
entityName);
if (!keySchema.equals(currentKeySchema)) {
String msg = "Migrating schema with different keys. Current: "
+ currentKeySchema.getRawSchema() + " New: "
+ keySchema.getRawSchema();
LOG.error(msg);
throw new ValidationException(msg);
}
if (!schemaManager.hasSchemaVersion(tableName, entityName, entitySchema)) {
LOG.info("Migrating Schema: (" + tableName + ", " + entityName + ")");
schemaManager.migrateSchema(tableName, entityName, entitySchemaString);
} else {
// don't set createTableAndFamilies to false, becasue we may still need
// to update the table to support what exists in the meta store.
LOG.info("Schema hasn't changed, not migrating: (" + tableName + ", "
+ entityName + ")");
}
} else {
LOG.info("Creating Schema: (" + tableName + ", " + entityName + ")");
parser.parseEntitySchema(entitySchemaString).getColumnMappingDescriptor()
.getRequiredColumnFamilies();
schemaManager.createSchema(tableName, entityName, entitySchemaString,
"org.kitesdk.data.hbase.avro.AvroKeyEntitySchemaParser",
"org.kitesdk.data.hbase.avro.AvroKeySerDe",
"org.kitesdk.data.hbase.avro.AvroEntitySerDe");
}
if (createTableAndFamilies) {
try {
if (!hbaseAdmin.tableExists(tableName)) {
HTableDescriptor desc = new HTableDescriptor(tableName);
desc.addFamily(new HColumnDescriptor(Constants.SYS_COL_FAMILY));
desc.addFamily(new HColumnDescriptor(Constants.OBSERVABLE_COL_FAMILY));
for (String columnFamily : entitySchema.getColumnMappingDescriptor()
.getRequiredColumnFamilies()) {
desc.addFamily(new HColumnDescriptor(columnFamily));
}
hbaseAdmin.createTable(desc);
} else {
Set<String> familiesToAdd = entitySchema.getColumnMappingDescriptor()
.getRequiredColumnFamilies();
familiesToAdd.add(new String(Constants.SYS_COL_FAMILY));
familiesToAdd.add(new String(Constants.OBSERVABLE_COL_FAMILY));
HTableDescriptor desc = hbaseAdmin.getTableDescriptor(tableName
.getBytes());