1

有没有在 phantom-dsl 中实现计数器操作的例子?

已检查:

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

有点寻找此信息的 phantom-dsl 版本:

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


下面是部分实现。它提出了两个问题:

  1. 我不确定如何从应用程序中获取值并在计数器表的计数器列中实现增量计数器操作。

  2. 如何更新与同一条目相关的表中的行,其中表具有不同的行数和键数。

thiagos 示例中,这两个表;'songs' 和 'songs_by_artist' 都具有相同的行但具有不同的分区(主键/集群列)

我不确定在 phantom-dsl 中如何更新与相同条目相关的行,例如下面的“records”和“record_transaction_counts”表。

例如

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)
4

2 回答 2

3

正如蒂亚戈建议的那样,您可以使用+=-=运算符来减少计数器的值。您也可以分别使用incrementordecrement方法来实现相同的目的。

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()
}

要减少,只需将这些行替换为:

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

在你过分依赖柜台之前,你也应该谷歌一下,看看其他人遇到了什么问题。

于 2016-05-15T21:49:31.007 回答
2

为了使用CounterColumnin phantom-dsl,您必须使用以下模式来增加它:

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

在你的ConcreteRecordTransactionCountsModel你可以改变你的商店以正确的方式增加计数器,如下所示:

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

我将尝试使用我以前工作过的更多示例来更新我的 github。另外,如果您有任何建议,请开票,我会这样做。

于 2016-05-15T13:23:39.137 回答