Search code examples
cassandraastyanax

How to use static columns from astyanax


Is there a way to create and use following column family with Astyanax:

CREATE TABLE mytest ( id text, subid text, clustering text, static_value text static, value text, PRIMARY KEY((id, subid), clustering));

If not, what are the best options for static columns?


Solution

  • The Astyanax Getting Started section contains a section on how to ensure proper serialization by annotating key fields with the "ordinal" keyword:

    // Annotated composite class
    Class SessionEvent{
      private @Component(ordinal=0) String   sessiondId;
      private @Component(ordinal=1) UUID timestamp;
    
      public SessionEvent() {
      }
    
      public int hashCode() { ... }
      public boolean equals(Object o) { ... }
      public int compareTo(Object o) { ... }
    }
    

    Otherwise, the Astyanax repo also has an example showing how to work directly with CQL3. To create your CF:

    String CREATE_STATEMENT = "CREATE TABLE mytest ( id text, subid text, clustering text, static_value text static, value text, PRIMARY KEY((id, subid), clustering))";
    
    try {
      @SuppressWarnings("unused")
      OperationResult<CqlResult<Integer, String>> result = keyspace
          .prepareQuery(EMP_CF)
          .withCql(CREATE_STATEMENT)
          .execute();
    } catch (ConnectionException e) {
      logger.error("failed to create CF", e);
      throw new RuntimeException("failed to create CF", e);
    }
    

    The (CQL3) link above also contains example methods that demonstrate reading and inserting as well.