@Test public void testSelectAllSuppressesKeyColumn() { CqlQuery<String,String,Long> cqlQuery = new CqlQuery<String,String,Long>(keyspace, se, se, le); cqlQuery.setQuery("select * from StandardLong1"); cqlQuery.setSuppressKeyInColumns(true); QueryResult<CqlRows<String,String,Long>> result = cqlQuery.execute(); CqlRows<String, String, Long> rows = result.get(); // check that we contain a 'key' column assertNull(rows.getList().get(0).getColumnSlice().getColumnByName("KEY")); // arbitrary row check assertNull(rows.getList().get(3).getColumnSlice().getColumnByName("KEY")); assertEquals(6,rows.getCount()); }
@Test public void testSimpleSelect20() { CqlQuery<String,String,Long> cqlQuery = new CqlQuery<String,String,Long>(keyspace, se, se, le); cqlQuery.setQuery("select birth .. birthzz from StandardLong1"); cqlQuery.setCqlVersion("2.0.0"); QueryResult<CqlRows<String,String,Long>> result = cqlQuery.execute(); CqlRows<String, String, Long> rows = result.get(); assertEquals(6 + customColumns,rows.getCount()); }
/** * Private helper to obtain a CqlQuery given an entity class type and a cql query * @param type the class type * @param query the cql query * @param <T> The type of entity * @param <V> The type of value * @return a CqlQuery */ protected <T, V> CqlQuery<String, String, V> getCQLQuery(Class<T> type, String query) { ClassMetadata classMetadata = getClassMetadata(type); Keyspace keyspace; if (classMetadata == null) { //this is not a managed class such as requesting a long for now as workaround the first keyspace will be selected //todo change in the future to be able to pass akeyspace keyspace = getDefaultKeyspace(); } else { keyspace = getKeyspace(classMetadata); } CqlQuery<String, String, V> cqlQuery = new CqlQuery<String, String, V>(keyspace, StringSerializer.get(), StringSerializer.get(), new TypeConverterSerializer<V>()); cqlQuery.setQuery(query); cqlQuery.setSuppressKeyInColumns(true); return cqlQuery; }
throw new IllegalArgumentException(String.format("type: %s not recognized as ColumnFamily or returnable value", type)); CqlQuery<String, String, Object> indexedQuery = getCQLQuery(type, query.getQuery()); CqlRows<String, String, Object> cqlRows = indexedQuery.execute().get(); if (cqlRows != null) { T entity = null;
if (cql3) { result = cassandra.execute_cql3_query(query, useCompression ? Compression.GZIP : Compression.NONE, ThriftConverter.consistencyLevel(getConsistency())); } else { result = cassandra.execute_cql_query(query,
if (cql3) { result = cassandra.execute_cql3_query(query, useCompression ? Compression.GZIP : Compression.NONE, ThriftConverter.consistencyLevel(getConsistency())); } else { result = cassandra.execute_cql_query(query,
/** * Fetch a map of columns and their values * * @param query a cql query * @return the resulting columns and their values */ protected Map<String, ByteBuffer> getColumns(String query) { Map<String, ByteBuffer> resultMap = new LinkedHashMap<String, ByteBuffer>(); CqlQuery<String, String, Object> cqlQuery = new CqlQuery<String, String, Object>(getDefaultKeyspace(), StringSerializer.get(), StringSerializer.get(), new TypeConverterSerializer<Object>()); cqlQuery.setQuery(query); cqlQuery.setSuppressKeyInColumns(true); QueryResult<CqlRows<String, String, Object>> results = cqlQuery.execute(); CqlRows<String, String, Object> rows = results.get(); for (Row<String, String, Object> row : rows) { ColumnSlice<String, Object> slice = row.getColumnSlice(); for (HColumn<String, Object> column : slice.getColumns()) { resultMap.put(column.getName(), column.getValueBytes()); } } return resultMap; }
@Test public void testCountQuery() { CqlQuery<String,String,Long> cqlQuery = new CqlQuery<String,String,Long>(keyspace, se, se, le); cqlQuery.setQuery("SELECT COUNT(*) FROM StandardLong1 WHERE KEY in ('cqlQueryTest_key1', 'cqlQueryTest_key2')"); QueryResult<CqlRows<String,String,Long>> result = cqlQuery.execute(); assertEquals(2, result.get().getAsCount()); }
@Test public void testInsertSyntaxHex() { CqlQuery<String,String,Long> cqlQuery = new CqlQuery<String,String,Long>(keyspace, se, se, le); String query = String.format("update Standard1 set '%s' = '%s' WHERE KEY = '%s'", ByteBufferUtil.bytesToHex(se.toByteBuffer("birthyear")), ByteBufferUtil.bytesToHex(se.toByteBuffer("1976")), ByteBufferUtil.bytesToHex(se.toByteBuffer("mykey1"))); cqlQuery.setQuery(query); cqlQuery.execute(); } }
@Test public void testSimpleSelect() { CqlQuery<String,String,Long> cqlQuery = new CqlQuery<String,String,Long>(keyspace, se, se, le); cqlQuery.setQuery("select * from StandardLong1"); QueryResult<CqlRows<String,String,Long>> result = cqlQuery.execute(); CqlRows<String, String, Long> rows = result.get(); // check that we contain a 'key' column assertNotNull(rows.getList().get(0).getColumnSlice().getColumnByName("KEY")); assertEquals(6 + customColumns,rows.getCount()); }
private static int countNumberOfRowsByFamilyColumn(Keyspace keyspace, String columnFamilyName) { CqlQuery<String, String, Long> cqlQuery = new CqlQuery<String, String, Long>(keyspace, StringSerializer.get(), StringSerializer.get(), new LongSerializer()); cqlQuery.setQuery("SELECT COUNT(*) FROM " + columnFamilyName); QueryResult<CqlRows<String, String, Long>> result = cqlQuery.execute(); return result.get().getAsCount(); }
private static int countNumberOfRowsByFamilyColumn(Keyspace keyspace, String columnFamilyName) { CqlQuery<String, String, Long> cqlQuery = new CqlQuery<String, String, Long>(keyspace, StringSerializer.get(), StringSerializer.get(), new LongSerializer()); cqlQuery.setQuery("SELECT COUNT(*) FROM " + columnFamilyName); QueryResult<CqlRows<String, String, Long>> result = cqlQuery.execute(); return result.get().getAsCount(); }