Ajeet Ganga
Ajeet Ganga

Reputation: 8653

How do I stop Astyanax from flattening my datamodel?

I created a table in Cassandra from this code :

@Entity
public class UserEvent {
    @Id
    private String userId;
    @Column
    private String col1;
    @Column
    private String col2;

....
manager = new DefaultEntityManager.Builder<UserEvent, String>()
                        .withEntityType(UserEvent.class)
                        .withKeyspace(keyspace)
                        .withColumnFamily("UserEvent")
                        .build();
manager.createStorage(null);

After adding few rows using manager , if I go to CQL3 and do select*, i see this :

cqlsh:demo> select * from "UserEvent";

 key | column1 | value
-----+---------+----------------------------------
  A2 |    col1 |                             0x61
  A2 |    col2 | 0xe6919fea82b2eaa88ae1a393e9b688
  A3 |    col1 |                             0x61
  A3 |    col2 | 0xe98fabe5bda7eca5a8e7a5b5efa1b3
 A50 |    col1 |                             0x62

So what I think, is happening is, Astyanax, FLATTENS my datamodel, to convert a individual row into M rows, where row-key is original-key+columnName.

I confirmed my suspicion by doing describe :

cqlsh:demo> describe TABLE "UserEvent";

CREATE TABLE "UserEvent" (
  key text,
  column1 text,
  value blob,
  PRIMARY KEY ((key), column1)
)

Now the question is , how do I stop astyanax from messing around with my data model. What I want is, something that produces output like :

cqlsh:demo> create table "MyUserEvent" ( key text, col1 text, col2 text, PRIMARY KEY (key) );
cqlsh:demo> insert into "MyUserEvent" (key,col1,col2) values ('key1','col11','col12');

cqlsh:demo> select * from "MyUserEvent";

 key  | col1  | col2
------+-------+-------
 key1 | col11 | col12
 key2 | col21 | col22

PS : Flattening as in Vertical flattening. :)

Upvotes: 2

Views: 73

Answers (1)

DmitryKanunnikoff
DmitryKanunnikoff

Reputation: 2266

I'm not familiar with the EntityManager, but I can offer other workable solution.

Init Astyanax:

AstyanaxContext<Cluster> context = new AstyanaxContext.Builder()
   .forCluster("MyCluster")
   .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
      .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
      .setConnectionPoolType(ConnectionPoolType.ROUND_ROBIN)
      .setCqlVersion("3.0.0")
      .setTargetCassandraVersion("2.0")
   )
   .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
      .setPort(9160)
      .setMaxConnsPerHost(20)
      .setSeeds("127.0.0.1:9160")
   )
   .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
   .buildCluster(ThriftFamilyFactory.getInstance());

   context.start();

Define keyspace (schema):

KeyspaceDefinition keyspaceDefinition = context.getClient().describeKeyspace("MyKeyspace");

Column family (table) creation:

ColumnFamilyDefinition userEventTable = context.getClient().makeColumnFamilyDefinition();
userEventTable.setKeyspace("MyKeyspace");
userEventTable.setName("UserEvent");
userEventTable.setComparatorType("UTF8Type");
userEventTable.setKeyValidationClass("UTF8Type");
userEventTable.setDefaultValidationClass("UTF8Type");

ColumnDefinition columnDefinition = context.getClient().makeColumnDefinition();
columnDefinition.setName("col1");
columnDefinition.setValidationClass("UTF8Type");
userEventTable.addColumnDefinition(columnDefinition);

columnDefinition = context.getClient().makeColumnDefinition();
columnDefinition.setName("col2");
columnDefinition.setValidationClass("UTF8Type");
userEventTable.addColumnDefinition(columnDefinition);

context.getClient().addColumnFamily(userEventTable);

If you make cqlsh:demo> DESCRIBE TABLE "UserEvent"; you must see something like this:

CREATE TABLE "UserEvent" (
    key text,
    col1 text,
    col2 text,
    PRIMARY KEY (key)
)

... ;

Upvotes: 1

Related Questions