private void createLocksKeyspace() throws ConnectionException { try { KeyspaceDefinition keyspaceDefinition = keyspace.describeKeyspace(); if ( keyspaceDefinition != null ) { logger.info("Keyspace {} already exists", keyspace.getKeyspaceName()); return; } } catch (ConnectionException ce){ logger.debug( "Received a NotFoundException when attempting to describe keyspace. It does not exist" ); } ImmutableMap.Builder<String, Object> strategyOptions = getKeySpaceProps(); ImmutableMap<String, Object> options = ImmutableMap.<String, Object>builder().put("strategy_class", cassandraFig.getLocksKeyspaceStrategy()) .put("strategy_options", strategyOptions.build()).build(); keyspace.createKeyspace(options); logger.info("Keyspace {} created with options {}", keyspace.getKeyspaceName(), options.toString()); }
@Override public Map<String, String> getCompressionOptions(String cf) throws BackendException { try { Keyspace k = keyspaceContext.getClient(); KeyspaceDefinition kdef = k.describeKeyspace(); if (null == kdef) { throw new PermanentBackendException("Keyspace " + k.getKeyspaceName() + " is undefined"); } ColumnFamilyDefinition cfdef = kdef.getColumnFamily(cf); if (null == cfdef) { throw new PermanentBackendException("Column family " + cf + " is undefined"); } return cfdef.getCompressionOptions(); } catch (ConnectionException e) { throw new PermanentBackendException(e); } } }
Keyspace keyspace = Astyanax.getKeyspaceContext(); ColumnFamily<String, String> mail = new ColumnFamily<String, String>( keyspace.getKeyspaceName(), // CF Name StringSerializer.get(), // Key Serializer StringSerializer.get()); // Column Serializer // You could start looping here to alter what data is being inserted // or make the method take in parameters and call it multiple times. String cqlStatement = "INSERT INTO MESSAGE_CF (KEY, DELETED_RECEIVER, DELETED_SENDER, SENDER) " + "VALUES ('user-1281324', 'Yes', 'No', 'user@mail.com');"; // execute the insertion OperationResult<CqlResult<String, String>> result = keyspace.prepareQuery(mail).withCql(cqlStatement).execute(); // stop looping
@Override public String getKeyspaceName() { return getPrimaryKS().getKeyspaceName(); }
private void verifyPresent(Keyspace ks, int rowKey) throws ConnectionException { ColumnList<Long> result = ks.prepareQuery(CF_DUAL_WRITES).getRow(rowKey).execute().getResult(); if (result.isEmpty()) { throw new RuntimeException("Row: " + rowKey + " missing from keysapce: " + ks.getKeyspaceName()); } else { System.out.println("Verified Row: " + rowKey + " present in ks: " + ks.getKeyspaceName()); } }
private void verifyNotPresent(Keyspace ks, int rowKey) throws ConnectionException { ColumnList<Long> result = ks.prepareQuery(CF_DUAL_WRITES).getRow(rowKey).execute().getResult(); if (!result.isEmpty()) { throw new RuntimeException("Row: " + rowKey + " present in keysapce: " + ks.getKeyspaceName()); } else { System.out.println("Verified Row: " + rowKey + " NOT present in ks: " + ks.getKeyspaceName()); } }
@Override public synchronized List<Host> get() { try { Map<String, Host> ipToHost = Maps.newHashMap(); for (TokenRange range : keyspace.describeRing(dc, rack)) { for (String endpoint : range.getEndpoints()) { Host host = ipToHost.get(endpoint); if (host == null) { host = new Host(endpoint, defaultPort); ipToHost.put(endpoint, host); } host.getTokenRanges().add(range); } } previousHosts = Lists.newArrayList(ipToHost.values()); return previousHosts; } catch (ConnectionException e) { if (previousHosts == null) { throw new RuntimeException(e); } LOG.warn("Failed to get hosts from " + keyspace.getKeyspaceName() + " via ring describe. Will use previously known ring instead"); return previousHosts; } }
if (!keyspaceInitialized) { LOGGER.info("Creating keyspace \"{}\"", keyspace.getKeyspaceName()); LOGGER.info("Successfully created keyspace \"{}\"", keyspace.getKeyspaceName());
private Keyspace pinToVerifiedHost(Host host) throws ConnectionException { //noinspection unchecked PinnedConnectionPool<Cassandra.Client> pinnedPool = new PinnedConnectionPool(_keyspace.getConnectionPool(), host); return new ThriftKeyspaceImpl( _keyspace.getKeyspaceName(), pinnedPool, _keyspace.getConfig(), EmptyKeyspaceTracerFactory.getInstance()); } }
private Keyspace pinToVerifiedHost(Host host) throws ConnectionException { //noinspection unchecked PinnedConnectionPool<Cassandra.Client> pinnedPool = new PinnedConnectionPool(_keyspace.getConnectionPool(), host); return new ThriftKeyspaceImpl( _keyspace.getKeyspaceName(), pinnedPool, _keyspace.getConfig(), EmptyKeyspaceTracerFactory.getInstance()); } }
/** * Gets the topology for a Cassandra keyspace as a Multimap, where the keys identify a rack (or availability zone * in Amazon) and the values are the token ranges for each host in that rack. For example, for a well distributed * ring of 12 hosts and a replication factor of 3 this method would return a Multimap with 3 keys and each key would * contain 4 token ranges. */ private Multimap<String, TokenRange> describeCassandraTopology(final Keyspace keyspace) { try { @SuppressWarnings ("unchecked") ConnectionPool<Cassandra.Client> connectionPool = (ConnectionPool<Cassandra.Client>) keyspace.getConnectionPool(); return connectionPool.executeWithFailover( new AbstractKeyspaceOperationImpl<Multimap<String, TokenRange>>(EmptyKeyspaceTracerFactory.getInstance().newTracer(CassandraOperationType.DESCRIBE_RING), keyspace.getKeyspaceName()) { @Override protected Multimap<String, TokenRange> internalExecute(Cassandra.Client client, ConnectionContext state) throws Exception { Multimap<String, TokenRange> racks = ArrayListMultimap.create(); for (org.apache.cassandra.thrift.TokenRange tokenRange : client.describe_local_ring(getKeyspace())) { // The final local endpoint "owns" the token range, the rest are for replication EndpointDetails endpointDetails = Iterables.getLast(tokenRange.getEndpoint_details()); racks.put(endpointDetails.getRack(), new TokenRangeImpl(tokenRange.getStart_token(), tokenRange.getEnd_token(), tokenRange.getEndpoints())); } return Multimaps.unmodifiableMultimap(racks); } }, keyspace.getConfig().getRetryPolicy().duplicate()).getResult(); } catch (ConnectionException e) { throw Throwables.propagate(e); } }
@Override public Map<String, String> getCompressionOptions(String cf) throws BackendException { try { Keyspace k = keyspaceContext.getClient(); KeyspaceDefinition kdef = k.describeKeyspace(); if (null == kdef) { throw new PermanentBackendException("Keyspace " + k.getKeyspaceName() + " is undefined"); } ColumnFamilyDefinition cfdef = kdef.getColumnFamily(cf); if (null == cfdef) { throw new PermanentBackendException("Column family " + cf + " is undefined"); } return cfdef.getCompressionOptions(); } catch (ConnectionException e) { throw new PermanentBackendException(e); } } }
@Override public Map<String, String> getCompressionOptions(String cf) throws BackendException { try { Keyspace k = keyspaceContext.getClient(); KeyspaceDefinition kdef = k.describeKeyspace(); if (null == kdef) { throw new PermanentBackendException("Keyspace " + k.getKeyspaceName() + " is undefined"); } ColumnFamilyDefinition cfdef = kdef.getColumnFamily(cf); if (null == cfdef) { throw new PermanentBackendException("Column family " + cf + " is undefined"); } return cfdef.getCompressionOptions(); } catch (ConnectionException e) { throw new PermanentBackendException(e); } } }
/** * Gets the topology for a Cassandra keyspace as a Multimap, where the keys identify a rack (or availability zone * in Amazon) and the values are the token ranges for each host in that rack. For example, for a well distributed * ring of 12 hosts and a replication factor of 3 this method would return a Multimap with 3 keys and each key would * contain 4 token ranges. */ private Multimap<String, TokenRange> describeCassandraTopology(final Keyspace keyspace) { try { @SuppressWarnings ("unchecked") ConnectionPool<Cassandra.Client> connectionPool = (ConnectionPool<Cassandra.Client>) keyspace.getConnectionPool(); return connectionPool.executeWithFailover( new AbstractKeyspaceOperationImpl<Multimap<String, TokenRange>>(EmptyKeyspaceTracerFactory.getInstance().newTracer(CassandraOperationType.DESCRIBE_RING), keyspace.getKeyspaceName()) { @Override protected Multimap<String, TokenRange> internalExecute(Cassandra.Client client, ConnectionContext state) throws Exception { Multimap<String, TokenRange> racks = ArrayListMultimap.create(); for (org.apache.cassandra.thrift.TokenRange tokenRange : client.describe_local_ring(getKeyspace())) { // The final local endpoint "owns" the token range, the rest are for replication EndpointDetails endpointDetails = Iterables.getLast(tokenRange.getEndpoint_details()); racks.put(endpointDetails.getRack(), new TokenRangeImpl(tokenRange.getStart_token(), tokenRange.getEnd_token(), tokenRange.getEndpoints())); } return Multimaps.unmodifiableMultimap(racks); } }, keyspace.getConfig().getRetryPolicy().duplicate()).getResult(); } catch (ConnectionException e) { throw Throwables.propagate(e); } }
/** * Gets the topology for a Cassandra keyspace as a Multimap, where the keys identify a rack (or availability zone * in Amazon) and the values are the token ranges for each host in that rack. For example, for a well distributed * ring of 12 hosts and a replication factor of 3 this method would return a Multimap with 3 keys and each key would * contain 4 token ranges. */ private Multimap<String, TokenRange> describeCassandraTopology(final Keyspace keyspace) { try { @SuppressWarnings ("unchecked") ConnectionPool<Cassandra.Client> connectionPool = (ConnectionPool<Cassandra.Client>) keyspace.getConnectionPool(); return connectionPool.executeWithFailover( new AbstractKeyspaceOperationImpl<Multimap<String, TokenRange>>(EmptyKeyspaceTracerFactory.getInstance().newTracer(CassandraOperationType.DESCRIBE_RING), keyspace.getKeyspaceName()) { @Override protected Multimap<String, TokenRange> internalExecute(Cassandra.Client client, ConnectionContext state) throws Exception { Multimap<String, TokenRange> racks = ArrayListMultimap.create(); for (org.apache.cassandra.thrift.TokenRange tokenRange : client.describe_local_ring(getKeyspace())) { // The final local endpoint "owns" the token range, the rest are for replication EndpointDetails endpointDetails = Iterables.getLast(tokenRange.getEndpoint_details()); racks.put(endpointDetails.getRack(), new TokenRangeImpl(tokenRange.getStart_token(), tokenRange.getEnd_token(), tokenRange.getEndpoints())); } return Multimaps.unmodifiableMultimap(racks); } }, keyspace.getConfig().getRetryPolicy().duplicate()).getResult(); } catch (ConnectionException e) { throw Throwables.propagate(e); } }
/** * Gets the topology for a Cassandra keyspace as a Multimap, where the keys identify a rack (or availability zone * in Amazon) and the values are the token ranges for each host in that rack. For example, for a well distributed * ring of 12 hosts and a replication factor of 3 this method would return a Multimap with 3 keys and each key would * contain 4 token ranges. */ private Multimap<String, TokenRange> describeCassandraTopology(final Keyspace keyspace) { try { @SuppressWarnings ("unchecked") ConnectionPool<Cassandra.Client> connectionPool = (ConnectionPool<Cassandra.Client>) keyspace.getConnectionPool(); return connectionPool.executeWithFailover( new AbstractKeyspaceOperationImpl<Multimap<String, TokenRange>>(EmptyKeyspaceTracerFactory.getInstance().newTracer(CassandraOperationType.DESCRIBE_RING), keyspace.getKeyspaceName()) { @Override protected Multimap<String, TokenRange> internalExecute(Cassandra.Client client, ConnectionContext state) throws Exception { Multimap<String, TokenRange> racks = ArrayListMultimap.create(); for (org.apache.cassandra.thrift.TokenRange tokenRange : client.describe_local_ring(getKeyspace())) { // The final local endpoint "owns" the token range, the rest are for replication EndpointDetails endpointDetails = Iterables.getLast(tokenRange.getEndpoint_details()); racks.put(endpointDetails.getRack(), new TokenRangeImpl(tokenRange.getStart_token(), tokenRange.getEnd_token(), tokenRange.getEndpoints())); } return Multimaps.unmodifiableMultimap(racks); } }, keyspace.getConfig().getRetryPolicy().duplicate()).getResult(); } catch (ConnectionException e) { throw Throwables.propagate(e); } }
/** * Initialize the client */ @Override public void init(DataGenerator dataGenerator) throws Exception { try { this.dataGenerator = dataGenerator; this.cluster = cassA6XManager.registerCluster(config.getCluster(), config.getHost(), config.getHostPort()); this.keyspace = cassA6XManager.registerKeyspace(config.getCluster(), config.getKeyspace(), config.getHost(), config.getHostPort()); AstyanaxConfigurationImpl aci = (AstyanaxConfigurationImpl) this.keyspace.getConfig(); aci.setDefaultReadConsistencyLevel(ConsistencyLevel.valueOf(config.getReadConsistencyLevel())) .setDefaultWriteConsistencyLevel(ConsistencyLevel.valueOf(config.getWriteConsistencyLevel())); CF = new ColumnFamily<>(config.getCfname(), StringSerializer.get(), IntegerSerializer.get(), StringSerializer.get()); if (config.getCreateSchema()) { logger.info("Trying to upsert schema"); preInit(); } } catch (ConnectionException e) { logger.error("Failed to initialize Astyanax driver"); throw e; } logger.info("Registered keyspace : " + this.keyspace.getKeyspaceName()); logger.info("Initialized CassAstyanaxPlugin"); }
/** * Initialize the client */ @Override public void init(DataGenerator dataGenerator) throws Exception { try { this.dataGenerator = dataGenerator; this.cluster = cassA6XManager.registerCluster(config.getCluster(), config.getHost(), config.getHostPort()); this.keyspace = cassA6XManager.registerKeyspace(config.getCluster(), config.getKeyspace(), config.getHost(), config.getHostPort()); AstyanaxConfigurationImpl aci = (AstyanaxConfigurationImpl) this.keyspace.getConfig(); aci.setDefaultReadConsistencyLevel(ConsistencyLevel.valueOf(config.getReadConsistencyLevel())) .setDefaultWriteConsistencyLevel(ConsistencyLevel.valueOf(config.getWriteConsistencyLevel())); CF = new ColumnFamily<>(config.getCfname(), StringSerializer.get(), IntegerSerializer.get(), StringSerializer.get()); if (config.getCreateSchema()) { logger.info("Trying to upsert schema"); preInit(); } } catch (ConnectionException e) { logger.error("Failed to initialize Astyanax driver"); throw e; } logger.info("Registered keyspace : " + this.keyspace.getKeyspaceName()); logger.info("Initialized CassAstyanaxPlugin"); }