Skip to content

Collections

opuneet edited this page May 8, 2014 · 4 revisions

Assume that this is your column family definition where you have a Map<String, String>

Here is how you can access this map.

public Keyspace setupKeyspace() throws Exception {

		AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
		.forCluster(clusterName)
		.forKeyspace(keyspaceName)
		.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()      
		.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
		.setCqlVersion("3.0.0")
		.setTargetCassandraVersion("1.2")
				)
				.withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
				.setPort(7102)
				.setMaxConnsPerHost(1)
				.setSeeds(seeds)

						)
						.withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
						.buildKeyspace(ThriftFamilyFactory.getInstance());

		context.start();
		Keyspace keyspace = context.getClient();
		return keyspace;
	}

public void doCollectionsQuery() throws Exception {
		
		Keyspace ks = setupKeyspace();
		
		ColumnFamily<Integer, String> cf = 
				new ColumnFamily<Integer, String>("workflow", IntegerSerializer.get(), StringSerializer.get(), ByteSerializer.get());
		
		CqlResult<Integer, String> result = ks.prepareQuery(cf)
				.withCql("select * from aj_build.execs where exec_id = 1 ")
		.execute().getResult();
		
		MapSerializer<String, String> mapSer = new MapSerializer<String, String>(UTF8Type.instance, UTF8Type.instance);
		
		Rows<Integer, String> rows = result.getRows();
		
		for (Row<Integer, String> row : rows) {
			
			ColumnList<String> cols = row.getColumns();
			System.out.println("\nCols: " + cols.size() + " rKey: WILL BE NULL ---> " + row.getKey());
			
			Column<String> col = cols.getColumnByName("details");
			
			Map<String, String> map = col.getValue(mapSer);
			System.out.println("MAP!!: " + map);
		}
	}
Clone this wiki locally