@Override public Health getHealth() { try { ColumnFamily<String, String> CF_SYSTEM_LOCAL = new ColumnFamily<String, String>( "system.local", StringSerializer.get(), StringSerializer.get(), StringSerializer.get() ); OperationResult<CqlResult<String, String>> result = keyspace.prepareQuery( CF_SYSTEM_LOCAL ) .setConsistencyLevel(ConsistencyLevel.CL_ONE) .withCql( "SELECT now() FROM system.local;" ) .execute(); if ( result.getResult().getRows().size() > 0 ) { return Health.GREEN; } } catch ( ConnectionException ex ) { logger.error( "Error connecting to Cassandra", ex ); } return Health.RED; } }
PreparedCqlQuery<Integer, String> pStatement = cqlQuery.asPreparedStatement(); pStatement.withIntegerValue(i).withStringValue(""+i).withLongValue(Long.valueOf(""+i)).execute(); .withRetryPolicy(new RetryNTimes(5)) .withCql("SELECT * FROM astyanaxunittests.cfdirect;") .execute(); Assert.assertTrue(result.getResult().hasRows()); .withCql("SELECT count(*) FROM astyanaxunittests.cfdirect;").execute(); Assert.assertFalse(result.getResult().hasRows()); Assert.assertTrue(result.getResult().hasNumber());
public void insert(int empId, int deptId, String firstName, String lastName) { try { @SuppressWarnings("unused") OperationResult<CqlResult<Integer, String>> result = keyspace .prepareQuery(EMP_CF) .withCql(INSERT_STATEMENT) .asPreparedStatement() .withIntegerValue(empId) .withIntegerValue(deptId) .withStringValue(firstName) .withStringValue(lastName) .execute(); } catch (ConnectionException e) { logger.error("failed to write data to C*", e); throw new RuntimeException("failed to write data to C*", e); } logger.debug("insert ok"); }
public void createColumnFamilyIfNotExists() { // Don't do in production; better to create from cqlsh to avoid parallel issues from eventual consistency. logger.debug("CQL: "+CREATE_STATEMENT); try { @SuppressWarnings("unused") OperationResult<CqlResult<Integer, String>> result = keyspace .prepareQuery(EMP_CF) .withCql(CREATE_STATEMENT) .execute(); } catch (Exception e) { logger.info("Didn't (re)create column family, message={}", e.getMessage()); } }
@Test public void testCqlCount() throws Exception { LOG.info("CQL Test"); OperationResult<CqlResult<String, String>> result = keyspace .prepareQuery(CF_STANDARD1) .withCql("SELECT count(*) FROM astyanaxunittests.standard1 where KEY='A';") .execute(); long count = result.getResult().getNumber(); LOG.info("CQL Count: " + count); Assert.assertTrue(0 <= count); }
@BeforeClass public static void init() throws Exception { initContext(); keyspace.prepareQuery(CF_DIRECT) .withCql("CREATE TABLE astyanaxunittests.cfdirect ( key int, column1 text, value bigint, PRIMARY KEY (key) )") .execute(); keyspace.prepareQuery(CF_EMPTY_TABLE) .withCql("CREATE TABLE astyanaxunittests.empty_table ( key text, column1 text, value text, PRIMARY KEY (key) )") .execute(); }
@Test public void testEmptyTable() throws Exception { CqlResult<String, String> result = keyspace.prepareQuery(CF_EMPTY_TABLE) .withCql("select * from astyanaxunittests.empty_table where key = 'blah'") .execute() .getResult(); Assert.assertFalse(result.hasRows()); Assert.assertFalse(result.hasNumber()); Assert.assertTrue(0 == result.getRows().size()); } }
public void read(int empId) { logger.debug("read()"); try { OperationResult<CqlResult<Integer, String>> result = keyspace.prepareQuery(EMP_CF) .withCql(String.format("SELECT * FROM %s WHERE %s=%d;", EMP_CF_NAME, COL_NAME_EMPID, empId)) .execute(); for (Row<Integer, String> row : result.getResult().getRows()) { logger.debug("row: "+row.getKey()+","+row); // why is rowKey null? ColumnList<String> cols = row.getColumns(); logger.debug("emp"); logger.debug("- emp id: "+cols.getIntegerValue(COL_NAME_EMPID, null)); logger.debug("- dept: "+cols.getIntegerValue(COL_NAME_DEPTID, null)); logger.debug("- firstName: "+cols.getStringValue(COL_NAME_FIRST_NAME, null)); logger.debug("- lastName: "+cols.getStringValue(COL_NAME_LAST_NAME, null)); } } catch (ConnectionException e) { logger.error("failed to read from C*", e); throw new RuntimeException("failed to read from C*", e); } }
public Set<AppsInstance> getAllInstancesFromCassandra(String app) { Set<AppsInstance> set = new HashSet<AppsInstance>(); try { final String selectClause = String.format( "SELECT * FROM %s USING CONSISTENCY LOCAL_QUORUM WHERE %s = '%s' ", CF_NAME_TOKENS, CN_APPID, app); logger.debug(selectClause); final ColumnFamily<String, String> CF_TOKENS_NEW = ColumnFamily.newColumnFamily(KS_NAME, StringSerializer.get(), StringSerializer.get()); OperationResult<CqlResult<String, String>> result = bootKeyspace.prepareQuery(CF_TOKENS_NEW) .withCql(selectClause).execute(); for (Row<String, String> row : result.getResult().getRows()) set.add(transform(row.getColumns())); } catch (Exception e) { logger.warn("Caught an Unknown Exception during reading msgs ... -> " + e.getMessage()); throw new RuntimeException(e); } return set; }
@BeforeClass public static void init() throws Exception { initContext(); keyspace.prepareQuery(CF_ACCOUNTS) .withCql("CREATE TABLE astyanaxunittests.accounts (userid text PRIMARY KEY, user text, pswd text)") .execute(); CF_ACCOUNTS.describe(keyspace); }
.withCql(selectClause).execute();
@BeforeClass public static void init() throws Exception { initContext(); keyspace.prepareQuery(CF_SERIALIZER1) .withCql("CREATE TABLE astyanaxunittests.serializer1 (key text, column1 bigint, value text, PRIMARY KEY (key))") .execute(); CF_SERIALIZER1.describe(keyspace); }
@Override protected Rows<RowKeyType, String> run() throws Exception { try { CqlResult<RowKeyType, String> cqlresult = keyspace.prepareQuery(columnFamily).withCql(cql).execute().getResult(); Rows<RowKeyType, String> result = cqlresult.getRows(); if (fallbackCache != null) { try { /* store the response in the cache for fallback if we have a cache */ fallbackCache.storeQuery(result, keyspace.toString(), columnFamily.getName(), cql); } catch (Exception e) { // don't blow up on cache population since this is non-essential } } return result; } catch (ConnectionException e) { throw e; } }
@Override protected Rows<RowKeyType, String> run() throws Exception { try { CqlResult<RowKeyType, String> cqlresult = keyspace.prepareQuery(columnFamily).withCql(cql).execute().getResult(); Rows<RowKeyType, String> result = cqlresult.getRows(); if (fallbackCache != null) { try { /* store the response in the cache for fallback if we have a cache */ fallbackCache.storeQuery(result, keyspace.toString(), columnFamily.getName(), cql); } catch (Exception e) { // don't blow up on cache population since this is non-essential } } return result; } catch (ConnectionException e) { throw e; } }
public String findKey(String app, String id, String location, String datacenter) { try { final String selectClause = String.format( "SELECT * FROM %s USING CONSISTENCY LOCAL_QUORUM WHERE %s = '%s' and %s = '%s' and %s = '%s' and %s = '%s' ", "tokens", CN_APPID, app, CN_ID, id, CN_LOCATION, location, CN_DC, datacenter); logger.info(selectClause); final ColumnFamily<String, String> CF_INSTANCES_NEW = ColumnFamily.newColumnFamily(KS_NAME, StringSerializer.get(), StringSerializer.get()); OperationResult<CqlResult<String, String>> result = bootKeyspace.prepareQuery(CF_INSTANCES_NEW) .withCql(selectClause).execute(); if (result == null || result.getResult().getRows().size() == 0) return null; Row<String, String> row = result.getResult().getRows().getRowByIndex(0); return row.getKey(); } catch (Exception e) { logger.warn("Caught an Unknown Exception during find a row matching cluster[" + app + "], id[" + id + "], and region[" + datacenter + "] ... -> " + e.getMessage()); throw new RuntimeException(e); } }
public String findKey(String cluster, String instanceId, String dc) { try { final String selectClause = String.format( "SELECT * FROM %s WHERE %s = '%s' and %s = '%s' and %s = '%s' ", CF_NAME_INSTANCES, CN_CLUSTER, cluster, CN_INSTANCEID, instanceId, CN_LOCATION, dc); logger.info(selectClause); final ColumnFamily<String, String> CF_INSTANCES_NEW = ColumnFamily.newColumnFamily(KS_NAME, StringSerializer.get(), StringSerializer.get()); OperationResult<CqlResult<String, String>> result = bootKeyspace.prepareQuery(CF_INSTANCES_NEW) .withCql(selectClause).execute(); if (result == null || result.getResult().getRows().size() == 0) return null; Row<String, String> row = result.getResult().getRows().getRowByIndex(0); return row.getKey(); } catch (Exception e) { logger.warn("Caught an Unknown Exception during find a row matching cluster[" + cluster + "], id[" + instanceId + "], and region[" + dc + "] ... -> " + e.getMessage()); throw new RuntimeException(e); } }