0

I have a Cassandra table defined like so:

create table foo (id int primary key, mapofmaps map<text, frozen<map<text, int>>>);

Into which I place some data:

insert into foo (id, mapofmaps) values (1, {'pets'; {'dog'; 42, 'cat'; 7}, 'foods': {'taco': 555, 'cake', '721'}});

I am then trying to use spring-data-cassandra to interact with it. I have a POJO:

@Table
public class Foo {
    @PrimaryKey
    private Integer id;

    @Column("mapofmaps")
    private Map<String, Map<String, Integer>> mapOfMaps;

    // getters/setters omitted for brevity
}

And a Repository:

public interface FooRepository extends CassandraRepository<Foo> {
}

And then the following code to try and retrieve all the records as a simple test:

public Iterable<Foo> getAllFoos() {
    return fooRepository.findAll();
}

Unfortunately this throws an exception. Less funky column types work OK, e.g. a List<String> and non-nested `Map type columns work fine. But this map of maps is not working for me.

Wondering if there is no support for this in spring-data-cassandra (though the exception appears to be in the DataStax code) or whether I just need to do something different with the POJO.

The exception thrown is as follows:

Caused by: java.lang.NullPointerException: null
    at com.datastax.driver.core.TypeCodec$MapCodec.deserialize(TypeCodec.java:821)
    at com.datastax.driver.core.TypeCodec$MapCodec.deserialize(TypeCodec.java:775)
    at com.datastax.driver.core.ArrayBackedRow.getMap(ArrayBackedRow.java:299)
    at org.springframework.data.cassandra.convert.ColumnReader.get(ColumnReader.java:53)
Jon Archer
  • 618
  • 9
  • 14
  • Debugging a little deeper into the datastax code that throws the exception, it looks like the `TypeCodec` class does not support maps of anything other than simple types; see `TypeCodec.mapOf(...) method :( Assuming I can't change the table schema, any suggestions on the best way to access this column from Java? – Jon Archer Nov 08 '15 at 16:01

2 Answers2

3

I dont know about the spring cassandra framework bit you can access the data using the datastax driver directly.

https://github.com/datastax/java-driver

I did some digging and the spring framework uses the java driver so there must be a cluster object already instantiated that you can leverage if the maps functionality you need is not exposed by spring-cassandra. Functionality could probably be added.

phact
  • 7,305
  • 23
  • 27
0

OK, what @phact said is not the answer is was looking for, but it did set me on the path to figuring things out.

As per my own comment on my original post, it appeared that this was a datastax driver issue, not a spring-data-cassandra issue. And that was borne out when I wrote a small test harness to try and query the problem table w/just the datastax client. I picked v2.1.7.1 of cassandra-driver-core and was able to query the table w/the map of maps fine.

Looking at the version of the driver that v1.3.0 of spring-data-cassandra brings in it's older, v2.0.4. A bit of maven dependency malarkey and I had my spring-data-cassandra project using a newer datastax driver and everything works fine.

Jon Archer
  • 618
  • 9
  • 14