How to increment Cassandra Counter Column with phantom-dsl?

丶灬走出姿态 提交于 2019-12-23 03:03:43

问题


Are there any examples of implementing the counter operation within phantom-dsl?

Have checked:

http://outworkers.com/blog/post/a-series-on-cassandra-part-3-advanced-features

https://github.com/outworkers/phantom/wiki/Counter-columns

https://github.com/outworkers/phantom/blob/develop/phantom-dsl/src/test/scala/com/websudos/phantom/tables/CounterTableTest.scala

Kinda looking for a phantom-dsl version of this info:

https://github.com/Netflix/astyanax/wiki/Working-with-counter-columns


The following is a partial implementation. It has risen two questions:

  1. I am not sure how to take values from within the application and implement the increment counter operation in a counter column within a counter table.

  2. How to update Rows within tables relating to the same entry where the tables have different number of rows and keys.

In thiagos example the two tables; 'songs' & 'songs_by_artist' both have the same rows but with different partitions (primary keys / clustering columns)

I am not sure how in phantom-dsl one would update rows relating to the same entries, such as with "records" & "record_transaction_counts" tables below.

e.g.

RecordTransactionCounts.{hash, time} relates to Records.{hash, time}


case class Record(hash: String,
                 size: Int,
                 time: Long,
                 difficulty: Float)


sealed class RecordsModel extends CassandraTable[RecordsModel, Record] {

  override def fromRow(row: Row): Record = {
    Record(
      hash(row),
      size(row),
      time(row),
      difficulty(row)
    )
  }

  object hash extends StringColumn(this) with PartitionKey[String]

  object size extends IntColumn(this)

  object time extends LongColumn(this)

  object difficulty extends FloatColumn(this)

}

abstract class ConcreteRecordsModel extends RecordsModel with RootConnector {

  override val tableName = "records"

  def insertNew(block: Record): Future[ResultSet] = insertNewRecord(block).future()

  def insertNewRecord(r: Record) = {
    insert
      .value(_.hash, r.hash)
      .value(_.size, r.size)
      .value(_.time, r.time)
      .value(_.difficulty, r.difficulty)
  }

}

case class RecordTransactionCounts(hash: String, time: Long, num_transactions: Long )

class RecordTransactionCountsModel extends CassandraTable[RecordTransactionCountsModel, RecordTransactionCounts] {

  override def tableName: String = "record_transaction_counts"

  object hash extends StringColumn(this) with PartitionKey[String]

  object time extends LongColumn(this) with ClusteringOrder[Long]

  object num_transactions extends CounterColumn(this)

  override def fromRow(r: Row): RecordTransactionCounts = {
    RecordTransactionCounts(
      hash(r),
      time(r),
      num_transactions(r)
    )
  }

}

abstract class ConcreteRecordTransactionCountsModel extends TransactionCountsModel with RootConnector {

  def createTable(): Future[ResultSet] = {
    create.ifNotExists().future()
  }

  def store(count: RecordTransactionCounts): Future[ResultSet] = {
    insert
      .value(_.hash, count.hash)
      .value(_.time, count.time)
      .value(_.num_transactions, count.num_transactions)
      .future()
  }

  def getCount(hash: String): Future[Option[Long]] = {
    select(_.count).where(_.hash eqs hash).one()
  }
}

class Database(val keyspace: KeySpaceDef) extends DatabaseImpl(keyspace) {

  def insertRecordTransactionCounts(tc: RecordTransactionCounts) = {
    Batch.logged
      .add(ChainDatabase.tc.store(tc))
      .future()
  }

  object tc extends ConcreteRecordTransactionCountsModel with keyspace.Connector

}

object ChainDatabase extends Database(Config.keySpaceDefinition)

回答1:


As Thiago suggested, you can use the += or alternatively the -= operator to decrement the value of a counter. You can also use the increment or decrement methods respectively to achieve the same thing.

def increment(count: RecordTransactionCounts): Future[ResultSet] = {
  update
    .where(_.hash eqs count.hash)
    .and(_.time eqs count.time)
    .modify(_.num_transactions += count.num_transactions)
    .future()
}
// or
def increment(count: RecordTransactionCounts): Future[ResultSet] = {
  update
    .where(_.hash eqs count.hash)
    .and(_.time eqs count.time)
    .modify(_.num_transactions increment count.num_transactions)
    .future()
}

To decrement, simply replace the lines with:

    ...
    .modify(_.num_transactions -= count.num_transactions)
    // or
    .modify(_.num_transactions decrement count.num_transactions)

Before you rely too much on counters, you should also Google a bit to find out what problems other people have encountered.




回答2:


In order to use the CounterColumn in phantom-dsl, you have to use the following pattern to increment it:

.modify(_.myCounterColumn += 1) //or whatever value you want to increment

In your ConcreteRecordTransactionCountsModel you could change your store to increment the counter in a proper way like this:

def increment(count: RecordTransactionCounts): Future[ResultSet] = {
  update
    .where(_.hash eqs count.hash)
    .and(_.time eqs count.time)
    .modify(_.num_transactions += count.num_transactions)
    .future()
}

I will try to update my github with more examples I have had worked before. Also if you have any suggestions, please open a ticket and I will do so.



来源:https://stackoverflow.com/questions/37236128/how-to-increment-cassandra-counter-column-with-phantom-dsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!