use of org.apache.cassandra.thrift.KsDef in project brisk by riptano.
the class CassandraHiveMetaStoreTest method testAutoCreateFromKeyspace.
@Test
public void testAutoCreateFromKeyspace() throws Exception {
CassandraHiveMetaStore metaStore = new CassandraHiveMetaStore();
Configuration conf = buildConfiguration();
CassandraClientHolder clientHolder = new CassandraClientHolder(conf);
KsDef ksDef = setupOtherKeyspace(conf, "AutoCreatedFromKeyspace", false);
clientHolder.getClient().system_add_keyspace(ksDef);
conf.setBoolean("cassandra.autoCreateHiveSchema", true);
metaStore.setConf(conf);
Database foundDb = metaStore.getDatabase("AutoCreatedFromKeyspace");
assertNotNull(foundDb);
CfDef cf = new CfDef("AutoCreatedFromKeyspace", "OtherCf2");
cf.setKey_validation_class("UTF8Type");
cf.setComparator_type("UTF8Type");
clientHolder.getClient().set_keyspace("HiveMetaStore");
clientHolder.getClient().system_add_column_family(cf);
metaStore.getAllTables("AutoCreatedFromKeyspace");
assertNotNull(metaStore.getTable("AutoCreatedFromKeyspace", "OtherCf2"));
}
use of org.apache.cassandra.thrift.KsDef in project brisk by riptano.
the class SchemaManagerServiceTest method testCreateOnConfigWithMetaData.
@Test
public void testCreateOnConfigWithMetaData() throws Exception {
KsDef ksDef = setupOtherKeyspace(configuration, "ConfigCreatedKeyspaceMetaData", true);
cassandraClientHolder.getClient().system_add_keyspace(ksDef);
configuration.setBoolean("cassandra.autoCreateHiveSchema", true);
schemaManagerService.createKeyspaceSchemasIfNeeded();
List<KsDef> keyspaces = schemaManagerService.findUnmappedKeyspaces();
for (KsDef ks : keyspaces) {
if (StringUtils.equals(ks.name, "ConfigCreatedKeyspaceMetaData")) {
fail("keyspace not created by configuration");
}
}
Table table = cassandraHiveMetaStore.getTable("ConfigCreatedKeyspaceMetaData", "OtherCf1");
assertNotNull(table);
StorageDescriptor sd = table.getSd();
assertEquals(5, sd.getColsSize());
for (Iterator<FieldSchema> iterator = sd.getColsIterator(); iterator.hasNext(); ) {
FieldSchema fs = iterator.next();
if (StringUtils.equals(fs.getName(), "col_name_utf8"))
assertEquals("string", fs.getType());
if (StringUtils.equals(fs.getName(), "col_name_bytes"))
assertEquals("string", fs.getType());
if (StringUtils.equals(fs.getName(), "col_name_timeuuid"))
assertEquals("string", fs.getType());
if (StringUtils.equals(fs.getName(), "col_name_long"))
assertEquals("int", fs.getType());
if (StringUtils.equals(fs.getName(), "col_name_int"))
assertEquals("bigint", fs.getType());
}
}
use of org.apache.cassandra.thrift.KsDef in project coprhd-controller by CoprHD.
the class DbClientContext method alterKeyspaceWithThrift.
/**
* Update the keyspace definition using low-level thrift API
* This is to bypass the precheck logic in Astyanax that throws SchemaDisagreementException when there are
* unreachable nodes in the cluster. Refer to https://github.com/Netflix/astyanax/issues/443 for details.
*
* @param kd existing keyspace definition, could be null
* @param update new keyspace definition
* @return new schema version after the update
*/
private String alterKeyspaceWithThrift(KeyspaceDefinition kd, KeyspaceDefinition update) throws ConnectionException {
final KeyspaceTracerFactory ks = EmptyKeyspaceTracerFactory.getInstance();
ConnectionPool<Cassandra.Client> pool = (ConnectionPool<Cassandra.Client>) clusterContext.getConnectionPool();
final KsDef def = ((ThriftKeyspaceDefinitionImpl) update).getThriftKeyspaceDefinition();
if (kd != null) {
return pool.executeWithFailover(new AbstractOperationImpl<String>(ks.newTracer(CassandraOperationType.UPDATE_KEYSPACE)) {
@Override
public String internalExecute(Cassandra.Client client, ConnectionContext context) throws Exception {
return client.system_update_keyspace(def);
}
}, clusterContext.getAstyanaxConfiguration().getRetryPolicy().duplicate()).getResult();
} else {
return pool.executeWithFailover(new AbstractOperationImpl<String>(ks.newTracer(CassandraOperationType.ADD_KEYSPACE)) {
@Override
public String internalExecute(Cassandra.Client client, ConnectionContext context) throws Exception {
return client.system_add_keyspace(def);
}
}, clusterContext.getAstyanaxConfiguration().getRetryPolicy().duplicate()).getResult();
}
}
use of org.apache.cassandra.thrift.KsDef in project brisk by riptano.
the class SchemaManagerService method findUnmappedKeyspaces.
/**
* Returns a List of Keyspace definitions that are not yet created as 'databases'
* in the Hive meta store. The list of keyspaces required for brisk operation are ignored.
* @return
*/
public List<KsDef> findUnmappedKeyspaces() {
List<KsDef> defs;
try {
defs = cassandraClientHolder.getClient().describe_keyspaces();
for (Iterator<KsDef> iterator = defs.iterator(); iterator.hasNext(); ) {
KsDef ksDef = iterator.next();
String name = ksDef.name;
log.debug("Found ksDef name: {}", name);
if (StringUtils.indexOfAny(name, SYSTEM_KEYSPACES) > -1 || isKeyspaceMapped(name)) {
log.debug("REMOVING ksDef name from unmapped List: {}", name);
iterator.remove();
}
}
} catch (Exception ex) {
throw new CassandraHiveMetaStoreException("Could not retrieve unmapped keyspaces", ex);
}
return defs;
}
use of org.apache.cassandra.thrift.KsDef in project brisk by riptano.
the class SchemaManagerService method createMetaStoreIfNeeded.
/**
* Create the meta store keyspace if it does not already exist.
* @return true if the keyspace did no exist and the creation was successful. False if the
* keyspace already existed.
* @throws {@link CassandraHiveMetaStoreException} wrapping the underlying exception if we
* failed to create the keyspace.
*/
public boolean createMetaStoreIfNeeded() {
if (configuration.getBoolean("cassandra.skipMetaStoreCreate", false))
return false;
try {
cassandraClientHolder.applyKeyspace();
return false;
} catch (CassandraHiveMetaStoreException chmse) {
log.debug("Attempting to create meta store keyspace: First set_keyspace call failed. Sleeping.");
}
// Sleep a random amount of time to stagger ks creations on many nodes
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
}
// check again...
try {
cassandraClientHolder.applyKeyspace();
return false;
} catch (CassandraHiveMetaStoreException chmse) {
log.debug("Attempting to create meta store keyspace after sleep.");
}
CfDef cf = new CfDef(cassandraClientHolder.getKeyspaceName(), cassandraClientHolder.getColumnFamily());
cf.setKey_validation_class("UTF8Type");
cf.setComparator_type("UTF8Type");
KsDef ks = new KsDef(cassandraClientHolder.getKeyspaceName(), "org.apache.cassandra.locator.SimpleStrategy", Arrays.asList(cf));
ks.setStrategy_options(KSMetaData.optsWithRF(configuration.getInt(CassandraClientHolder.CONF_PARAM_REPLICATION_FACTOR, 1)));
try {
cassandraClientHolder.getClient().system_add_keyspace(ks);
return true;
} catch (Exception e) {
throw new CassandraHiveMetaStoreException("Could not create Hive MetaStore database: " + e.getMessage(), e);
}
}
Aggregations