@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()); }
/** * 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; }
/** * 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; }