private static void createKeyspace() throws Exception { keyspaceContext = new AstyanaxContext.Builder() .forCluster(TEST_CLUSTER_NAME) .forKeyspace(TEST_KEYSPACE_NAME) .withAstyanaxConfiguration( new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) .setConnectionPoolType(ConnectionPoolType.TOKEN_AWARE)) .withConnectionPoolConfiguration( new ConnectionPoolConfigurationImpl(TEST_CLUSTER_NAME + "_" + TEST_KEYSPACE_NAME) .setSocketTimeout(30000) .setMaxTimeoutWhenExhausted(2000) .setMaxConnsPerHost(20) .setInitConnsPerHost(10) .setSeeds(SEEDS)) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance()); keyspaceContext.start(); keyspace = keyspaceContext.getEntity(); try { keyspace.dropKeyspace(); } catch (Exception e) { e.printStackTrace(); } keyspace.createKeyspace( ImmutableMap.<String, Object>builder() .put( "strategy_options", ImmutableMap.<String, Object>builder().put("replication_factor", "1").build()) .put("strategy_class", "SimpleStrategy") .build()); keyspace.createColumnFamily(CF_SAMPLE_ENTITY, null); keyspace.createColumnFamily(CF_SIMPLE_ENTITY, null); }
public static void createKeyspace() throws Exception { keyspaceContext = new AstyanaxContext.Builder() .forCluster(TEST_CLUSTER_NAME) .forKeyspace(TEST_KEYSPACE_NAME) .withAstyanaxConfiguration( new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) .setConnectionPoolType(ConnectionPoolType.TOKEN_AWARE) .setDiscoveryDelayInSeconds(60000)) .withConnectionPoolConfiguration( new ConnectionPoolConfigurationImpl(TEST_CLUSTER_NAME + "_" + TEST_KEYSPACE_NAME) .setSocketTimeout(30000) .setMaxTimeoutWhenExhausted(2000) .setMaxConnsPerHost(20) .setInitConnsPerHost(10) .setSeeds(SEEDS)) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance()); keyspaceContext.start(); keyspace = keyspaceContext.getEntity(); try { keyspace.dropKeyspace(); } catch (Exception e) { LOG.info(e.getMessage()); } keyspace.createKeyspace( ImmutableMap.<String, Object>builder() .put( "strategy_options", ImmutableMap.<String, Object>builder().put("replication_factor", "1").build()) .put("strategy_class", "SimpleStrategy") .build()); keyspace.createColumnFamily(CF_USER_UNIQUE_UUID, null); keyspace.createColumnFamily(CF_EMAIL_UNIQUE_UUID, null); keyspace.createColumnFamily(CF_ALL_ROWS, null); keyspace.createColumnFamily( LOCK_CF_LONG, ImmutableMap.<String, Object>builder() .put("default_validation_class", "LongType") .put("key_validation_class", "UTF8Type") .put("comparator_type", "UTF8Type") .build()); keyspace.createColumnFamily( LOCK_CF_STRING, ImmutableMap.<String, Object>builder() .put("default_validation_class", "UTF8Type") .put("key_validation_class", "UTF8Type") .put("comparator_type", "UTF8Type") .build()); keyspace.createColumnFamily( CF_STANDARD1, ImmutableMap.<String, Object>builder() .put( "column_metadata", ImmutableMap.<String, Object>builder() .put( "Index1", ImmutableMap.<String, Object>builder() .put("validation_class", "UTF8Type") .put("index_name", "Index1") .put("index_type", "KEYS") .build()) .put( "Index2", ImmutableMap.<String, Object>builder() .put("validation_class", "UTF8Type") .put("index_name", "Index2") .put("index_type", "KEYS") .build()) .build()) .build()); keyspace.createColumnFamily(UNIQUE_CF, null); KeyspaceDefinition ki = keyspaceContext.getEntity().describeKeyspace(); System.out.println("Describe Keyspace: " + ki.getName()); try { // // CF_Super : // 'A' : // 'a' : // 1 : 'Aa1', // 2 : 'Aa2', // 'b' : // ... // 'z' : // ... // 'B' : // ... // // CF_Standard : // 'A' : // 'a' : 1, // 'b' : 2, // ... // 'z' : 26, // 'B' : // ... // MutationBatch m; OperationResult<Void> result; m = keyspace.prepareMutationBatch(); for (char keyName = 'A'; keyName <= 'Z'; keyName++) { String rowKey = Character.toString(keyName); ColumnListMutation<String> cfmStandard = m.withRow(CF_STANDARD1, rowKey); for (char cName = 'a'; cName <= 'z'; cName++) { cfmStandard.putColumn(Character.toString(cName), (int) (cName - 'a') + 1, null); } cfmStandard.putColumn("Index1", (int) (keyName - 'A') + 1, null); cfmStandard.putColumn("Index2", 42, null); m.execute(); } m.withRow(CF_STANDARD1, "Prefixes") .putColumn("Prefix1_a", 1, null) .putColumn("Prefix1_b", 2, null) .putColumn("prefix2_a", 3, null); result = m.execute(); m.execute(); m = keyspace.prepareMutationBatch(); for (int i = 0; i < ALL_ROWS_COUNT; i++) { m.withRow(CF_ALL_ROWS, i).putColumn(0, true); if (m.getRowCount() == 50) { m.execute(); } } m.execute(); } catch (Exception e) { System.out.println(e.getMessage()); Assert.fail(); } }
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); } } }