Correct usage of TypeInferringSerializer

29 views
Skip to first unread message

Lewis John Mcgibbney

unread,
Feb 16, 2012, 1:05:55 PM2/16/12
to hector...@googlegroups.com
Hi Everyone,

When I can I've been working on improving our code with some of Hectors nice features. Previously Patricio identified a use case for the above class, but I would like to get some direction about it's implementation.
The problem with the code which was identified was that we have a switch block which evaluates different column values, however in the end the values get cast to type String anyway... The aim is that TypeInferringSerializer could be used instead for when the values are already of type bytebuffer.

I've appended the code below as a verbose example of where I am coming from and would appreciate any guidance whatsoever to get this moving in the right direction.

I'm sure that this wouldn't mean for every case we would be implementing the TypeInferringSeializer logic, therefore I'm looking for some pointers here ad there's very little on list.

Thank you very much in advance for any suggestions.

Regards

Lewis

N.B. Yes I know about the 3 TODO's I will get round to these at a later stage... maybe tomorrow ;0)

{code}
 /**
   * Add a field to Cassandra according to its type.
   * @param key     the key of the row where the field should be added
   * @param field   the Avro field representing a datum
   * @param value   the field value
   */
  private void addOrUpdateField(String key, Field field, Object value) {
    Schema schema = field.schema();
    Type type = schema.getType();
    //LOG.info(field.name() + " " + type.name());
    switch (type) {
      case STRING:
        this.cassandraClient.addColumn(key, field.name(), value);
        break;
      case INT:
        this.cassandraClient.addColumn(key, field.name(), value);
        break;
      case LONG:
        this.cassandraClient.addColumn(key, field.name(), value);
        break;
      case BYTES:
        this.cassandraClient.addColumn(key, field.name(), value);
        break;
      case FLOAT:
        this.cassandraClient.addColumn(key, field.name(), value);
        break;
      case RECORD:
        if (value != null) {
          if (value instanceof PersistentBase) {
            PersistentBase persistentBase = (PersistentBase) value;
            for (Field member: schema.getFields()) {
             
              // TODO: hack, do not store empty arrays
              Object memberValue = persistentBase.get(member.pos());
              if (memberValue instanceof GenericArray<?>) {
                GenericArray<String> array = (GenericArray<String>) memberValue;
                if (array.size() == 0) {
                  continue;
                }
              }
             
              this.cassandraClient.addSubColumn(key, field.name(), member.name(), memberValue);
            }
          } else {
            LOG.info("Record not supported: " + value.toString());
           
          }
        }
        break;
      case MAP:
        if (value != null) {
          if (value instanceof StatefulHashMap<?, ?>) {
            //TODO cast to stateful map and only write dirty keys
            Map<Utf8, Object> map = (Map<Utf8, Object>) value;
            for (Utf8 mapKey: map.keySet()) {
             
              // TODO: hack, do not store empty arrays
              Object keyValue = map.get(mapKey);
              if (keyValue instanceof GenericArray<?>) {
                GenericArray<String> array = (GenericArray<String>) keyValue;
                if (array.size() == 0) {
                  continue;
                }
              }
             
              this.cassandraClient.addSubColumn(key, field.name(), mapKey.toString(), keyValue);             
            }
          } else {
            LOG.info("Map not supported: " + value.toString());
          }
        }
        break;
      default:
        LOG.info("Type not considered: " + type.name());     
    }
  }
{code} 

--
Lewis

Patricio Echagüe

unread,
Feb 16, 2012, 3:19:23 PM2/16/12
to hector...@googlegroups.com
Lewis, are you doing/treating the values differently here?

      case INT:
        this.cassandraClient.addColumn(key, field.name(), value);
        break;
      case LONG:
        this.cassandraClient.addColumn(key, field.name(), value);
        break;
      case BYTES:
        this.cassandraClient.addColumn(key, field.name(), value);
        break;

lewismc

unread,
Feb 18, 2012, 8:43:38 AM2/18/12
to hector-users
Hi Patricio,

On Feb 16, 8:19 pm, Patricio Echagüe <patric...@gmail.com> wrote:
> Lewis, are you doing/treating the values differently here?
>
Well no, the relative outcome is that regardless of the data type
identified within each case, all are added to a column in the same
fashion. e.g. this.cassandraClient.addColumn(blah)
Reply all
Reply to author
Forward
0 new messages