/** Check if the keyspace exsts */ public boolean keySpaceExists( String keyspace ) { KeyspaceDefinition ksDef = cluster.describeKeyspace( keyspace ); return ksDef != null; }
@JsonIgnore public ByteBuffer getKeyNameBytes() { return keySerializer.toByteBuffer( keyName ); }
/** * Wraps "describe_thrift_version API call as this hits a static string in Cassandra. This is the most lightweight * way to assure that Hector is alive and talking to the cluster. * * @return true if we have a lit connection to the cluster. */ public boolean getIsCassandraAlive() { boolean isAlive = false; try { isAlive = cluster.describeThriftVersion() != null; } catch ( HectorException he ) { logger.error( "Could not communicate with Cassandra cluster", he ); } return isAlive; }
SliceQuery<ByteBuffer, N, V> q = createSliceQuery( ko, be, nameSerializer, valueSerializer ); q.setColumnFamily( columnFamily.toString() ); q.setKey( bytebuffer( key ) ); q.setColumnNames( ( N[] ) nameSerializer.fromBytesSet( se.toBytesSet( new ArrayList<String>( columnNames ) ) ) .toArray() ); QueryResult<ColumnSlice<N, V>> r = q.execute(); ColumnSlice<N, V> slice = r.get(); List<HColumn<N, V>> results = slice.getColumns();
/** Return true if the column family exists */ public boolean cfExists( String keyspace, String cfName ) { KeyspaceDefinition ksDef = cluster.describeKeyspace( keyspace ); if ( ksDef == null ) { return false; } for ( ColumnFamilyDefinition cf : ksDef.getCfDefs() ) { if ( cfName.equals( cf.getName() ) ) { return true; } } return false; }
/** * Lazy creates a column family in the keyspace. If it doesn't exist, it will be created, then the call will sleep * until all nodes have acknowledged the schema change */ public void createColumnFamily( String keyspace, ColumnFamilyDefinition cfDef ) { if ( !keySpaceExists( keyspace ) ) { createKeySpace( keyspace ); } //add the cf if ( !cfExists( keyspace, cfDef.getName() ) ) { //default read repair chance to 0.1 cfDef.setReadRepairChance( 0.1d ); cfDef.setCompactionStrategy( "LeveledCompactionStrategy" ); cfDef.setCompactionStrategyOptions( new MapUtils.HashMapBuilder().map("sstable_size_in_mb", "512" ) ); cluster.addColumnFamily( cfDef, true ); logger.info( "Created column family {} in keyspace {}", cfDef.getName(), keyspace ); } }
/** Create the keyspace */ private void createKeySpace( String keyspace ) { logger.info( "Creating keyspace: {}", keyspace ); String strategy_class = getString( properties, "cassandra.keyspace.strategy", "org.apache.cassandra.locator.SimpleStrategy" ); logger.info( "Using strategy: {}", strategy_class ); int replication_factor = getIntValue( properties, "cassandra.keyspace.replication", 1 ); logger.info( "Using replication (may be overriden by strategy options): {}", replication_factor ); // try { ThriftKsDef ks_def = ( ThriftKsDef ) HFactory .createKeyspaceDefinition( keyspace, strategy_class, replication_factor, new ArrayList<ColumnFamilyDefinition>() ); @SuppressWarnings({ "unchecked", "rawtypes" }) Map<String, String> strategy_options = filter( ( Map ) properties, "cassandra.keyspace.strategy.options.", true ); if ( strategy_options.size() > 0 ) { logger.info( "Strategy options: {}", mapToFormattedJsonString( strategy_options ) ); ks_def.setStrategyOptions( strategy_options ); } cluster.addKeyspace( ks_def ); waitForCreation( keyspace ); logger.info( "Created keyspace {}", keyspace ); }
public void afterTest() { cluster.truncate(keyspace.getKeyspaceName(), "Super1"); }
public void destroy() throws Exception { if (cluster != null) { HConnectionManager connectionManager = cluster.getConnectionManager(); if (connectionManager != null) { connectionManager.shutdown(); } } cluster = null; } }
/** * @deprecated does the same thing as getClock() will be removed in a future release * @return */ public long getEffectiveClock() { return keyspace.createClock(); }
/** * Create a timestamp based on the TimeResolution set to the cluster. * * @return a timestamp */ public long createTimestamp() { return chc.getClockResolution().createClock(); }
public CassandraService( Properties properties, Cluster cluster, CassandraHostConfigurator cassandraHostConfigurator, final Injector injector) { this.properties = properties; this.cluster = cluster; chc = cassandraHostConfigurator; lockManager = injector.getInstance( LockManager.class ); db_logger.info( "{}", cluster.getKnownPoolHosts( false ) ); //getInjector applicationKeyspace = injector.getInstance( CassandraFig.class ).getApplicationKeyspace(); }
@Override public HConsistencyLevel getConsistencyLevel(OperationType operationType) { return consistency.get(operationType); }
/** Wait until all nodes agree on the same schema version */ private void waitForCreation( String keyspace ) { while ( true ) { Map<String, List<String>> versions = cluster.describeSchemaVersions(); // only 1 version, return if ( versions != null && versions.size() == 1 ) { return; } // sleep and try again try { Thread.sleep( 100 ); } catch ( InterruptedException e ) { } } }
public boolean checkKeyspacesExist() { boolean exists = false; try { exists = cluster.describeKeyspace( getApplicationKeyspace() ) != null; } catch ( Exception ex ) { logger.error( "could not describe keyspaces", ex ); } return exists; }
@JsonIgnore public ByteBuffer getColumnNameBytes() { return columnNameSerializer.toByteBuffer( columnName ); }
/** * Wrapped call to keyspace.createClock * To Specify a clock for a group of operations, use {@link AbstractTemplateUpdater} instead * @return */ public long getClock() { return keyspace.createClock(); }
private ByteBuffer findBytes(N val) { ByteBuffer valBytes; if (val == null) { valBytes = EMPTY_BYTE_BUFFER; } else { valBytes = columnNameSerializer.toByteBuffer(val); } return valBytes; }
public ExecutingVirtualKeyspace(String keyspace, E keyPrefix, Serializer<E> keyPrefixSerializer, HConnectionManager connectionManager, ConsistencyLevelPolicy consistencyLevelPolicy, FailoverPolicy failoverPolicy) { super(keyspace, connectionManager, consistencyLevelPolicy, failoverPolicy); this.keyPrefix = keyPrefix; this.keyPrefixSerializer = keyPrefixSerializer; prefixBytes = keyPrefixSerializer.toByteBuffer(keyPrefix); }
public ExecutingVirtualKeyspace(String keyspace, E keyPrefix, Serializer<E> keyPrefixSerializer, HConnectionManager connectionManager, ConsistencyLevelPolicy consistencyLevelPolicy, FailoverPolicy failoverPolicy, Map<String, String> credentials) { super(keyspace, connectionManager, consistencyLevelPolicy, failoverPolicy, credentials); this.keyPrefix = keyPrefix; this.keyPrefixSerializer = keyPrefixSerializer; prefixBytes = keyPrefixSerializer.toByteBuffer(keyPrefix); }