Skip to content

Collections

opuneet edited this page May 9, 2014 · 4 revisions

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

Table schema

CREATE TABLE execs (
  exec_id int,
  stages text,
  buildgraph map<text, text>,
  details map<text, text>,
  finishtime timestamp,
  stage text,
  starttime timestamp,
  status text,
  PRIMARY KEY (exec_id, stages)
) WITH
  bloom_filter_fp_chance=0.010000 AND
  caching='KEYS_ONLY' AND
  comment='' AND
  dclocal_read_repair_chance=0.000000 AND
  gc_grace_seconds=864000 AND
  read_repair_chance=0.100000 AND
  replicate_on_write='true' AND
  populate_io_cache_on_flush='false' AND
  compaction={'class': 'SizeTieredCompactionStrategy'} AND
  compression={};

Some sample data

cqlsh:aj_build> select * from execs; 

 exec_id | stages | buildgraph | details                                                                             | finishtime               | stage | starttime                | status
---------+--------+------------+-------------------------------------------------------------------------------------+--------------------------+-------+--------------------------+-----------
       1 |   stg1 |       null |                                                                   {'build': '4819'} | 2014-05-06 00:00:00+0000 | Build | 2014-05-01 00:00:00+0000 | Completed
       1 |   stg2 |       null |                                       {'build': 'WE-CLOUD-MERCH-frontend-dev-FMLY'} | 2014-05-06 00:00:00+0000 | Build | 2014-05-01 00:00:00+0000 | Completed
       1 |   stg3 |       null | {'buildUrl': 'http://builds.netflix.com/job/WE-CLOUD-MERCH-frontend-dev-FMLY/4819'} | 2014-05-06 00:00:00+0000 | Build | 2014-05-01 00:00:00+0000 | Completed

Here is some code on how to access the row with the column that has the 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