Question

I use cassandra in my project, and use hector to connect with cassandra, I defined a column family user in my keyspace, and there is a column name "reputation" in user, now I want to increase user's reputation, how to do? can you help me?

Was it helpful?

Solution

You can create the column family with counters:

ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(keyspaceName, "UserCounters");
cfDef.setKeyValidationClass(ComparatorType.UTF8TYPE.getClassName());
cfDef.setComparatorType(ComparatorType.UTF8TYPE);
cfDef.setDefaultValidationClass(ComparatorType.COUNTERTYPE.getClassName());
cfDef.setColumnType(ColumnType.STANDARD);
cfDef.setKeyspaceName(keyspaceName);

then whenever you want to increase a users reputation you can increment it

int amountToIncrease = 1;
HCounterColumn<String> hcol = HFactory.createCounterColumn("reputation", amountToIncrease);
Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get()); 
mutator.addCounter("Bob Smith", "UserCounters", hcol);
mutator.execute();

With this you have a row per user, and a reputation counter in it.

UserCounters {
  username: {
    reputation: <counter>
  },
  username2: {
    reputation: <counter>
  }
}

Then if you want to add other counters for that user (ie like "comments" or "pageviews" or something) you can add it easily

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top