@Override public boolean containsKey(ByteBuffer key, TransactionHandle txh) throws StorageException { try { // See getSlice() below for a warning suppression justification @SuppressWarnings("rawtypes") RowQuery rq = (RowQuery) keyspace .prepareQuery(cf) .withRetryPolicy(retryPolicy.duplicate()) .setConsistencyLevel(readLevel) .getKey(key); @SuppressWarnings("unchecked") OperationResult<ColumnList<ByteBuffer>> r = rq.withColumnRange(EMPTY, EMPTY, false, 1).execute(); return 0 < r.getResult().size(); } catch (ConnectionException e) { throw new TemporaryStorageException(e); } }
@Override public List<Entry> getSlice( ByteBuffer key, ByteBuffer columnStart, ByteBuffer columnEnd, int limit, TransactionHandle txh) throws StorageException { /* * The following hideous cast dance avoids a type-erasure error in the * RowQuery<K, V> type that emerges when K=V=ByteBuffer. Specifically, * these two methods erase to the same signature after generic reduction * during compilation: * * RowQuery<K, C> withColumnRange(C startColumn, C endColumn, boolean * reversed, int count) RowQuery<K, C> withColumnRange(ByteBuffer * startColumn, ByteBuffer endColumn, boolean reversed, int count) * * * The compiler substitutes ByteBuffer=C for both startColumn and * endColumn, compares it to its identical twin with that type * hard-coded, and dies. * * Here's the compiler error I received when attempting to compile this * code without the following casts. I used Oracle JDK 6 Linux x86_64. * * AstyanaxOrderedKeyColumnValueStore.java:[108,4] reference to * withColumnRange is ambiguous, both method * withColumnRange(C,C,boolean,int) in * com.netflix.astyanax.query.RowQuery<java.nio.ByteBuffer,java.nio.ByteBuffer> * and method * withColumnRange(java.nio.ByteBuffer,java.nio.ByteBuffer,boolean,int) * in * com.netflix.astyanax.query.RowQuery<java.nio.ByteBuffer,java.nio.ByteBuffer> * match * */ @SuppressWarnings("rawtypes") RowQuery rq = (RowQuery) keyspace .prepareQuery(cf) .setConsistencyLevel(readLevel) .withRetryPolicy(retryPolicy.duplicate()) .getKey(key); // RowQuery<ByteBuffer, ByteBuffer> rq = keyspace.prepareQuery(cf).getKey(key); rq.withColumnRange(columnStart, columnEnd, false, limit + 1); OperationResult<ColumnList<ByteBuffer>> r; try { @SuppressWarnings("unchecked") OperationResult<ColumnList<ByteBuffer>> tmp = (OperationResult<ColumnList<ByteBuffer>>) rq.execute(); r = tmp; } catch (ConnectionException e) { throw new TemporaryStorageException(e); } List<Entry> result = new ArrayList<Entry>(r.getResult().size()); int i = 0; for (Column<ByteBuffer> c : r.getResult()) { ByteBuffer colName = c.getName(); if (colName.equals(columnEnd)) { break; } result.add(new Entry(colName, c.getByteBufferValue())); if (++i == limit) { break; } } return result; }
public static void main(String args[]) throws ConnectionException { String[] calles_28001 = {"Alcala", "Preciados", "Gran Via", "Princesa"}; String[] calles_28002 = {"Castellana", "Goya", "Serrano", "Velazquez"}; int index_28001 = 0; int index_28002 = 0; List<User> users = new ArrayList<User>(); for (int i = 0; i < 10; i++) { String id = (i + 1) + ""; String email = "user" + id + "@void.com"; String nombre = "nombre_" + id; String cp; String calle; if (i % 2 == 0) { cp = "28001"; calle = calles_28001[index_28001]; index_28001++; index_28001 = index_28001 % 4; } else { cp = "28002"; calle = calles_28002[index_28002]; index_28002++; index_28002 = index_28002 % 4; } User user = new User(id, email, nombre, cp, calle); users.add(user); } // conectar y crear column family Keyspace ksUsers = Utils.getKeyspace("utad"); String columnFamily = "compositeKeys"; ColumnFamily<String, String> cfUsers = new ColumnFamily<String, String>( columnFamily, StringSerializer.get(), StringSerializer.get()); try { ksUsers.dropColumnFamily(columnFamily); } catch (Exception e) { System.out.println("No existe el column family a borrar: " + columnFamily); } try { ksUsers.createColumnFamily( cfUsers, ImmutableMap.<String, Object>builder() .put("key_validation_class", "BytesType") .put("comparator_type", "BytesType") .build()); } catch (Exception e) { System.out.println("Error creando el column family: " + columnFamily + " " + e.getMessage()); } MutationBatch m = ksUsers.prepareMutationBatch(); String rowKey = "usersByCPAddress"; ColumnListMutation<String> clm = m.withRow(cfUsers, rowKey); System.out.println("\nEscribimos los datos"); for (User user : users) { String id = user.id; String cp = user.cp; String nombre = user.nombre; String email = user.email; String calle = user.calle; // escribir String key = id + ":" + cp + ":" + calle; String value = id + ":" + nombre + ":" + email; clm.putColumn(key, value); ksUsers.prepareColumnMutation(cfUsers, rowKey, key).putValue(value, null).execute(); } // leer el resultado System.out.println("\nLeer el resultado"); RowQuery<String, String> query = ksUsers .prepareQuery(cfUsers) .getKey(rowKey) .withColumnRange(new RangeBuilder().build()) .autoPaginate(true); ColumnList<String> columns = query.execute().getResult(); for (Column<String> c : columns) { String key = c.getName(); String value = c.getStringValue(); System.out.println("\nclave"); String[] ksplit = key.split(":"); for (String string : ksplit) { System.out.println("\t" + string); } System.out.println("valor"); String[] kvalue = value.split(":"); for (String string : kvalue) { System.out.println("\t" + string); } } }