在phantom-dsl中是否有任何實施計數器操作的例子?如何用phantom-dsl增加Cassandra Counter Column?
有檢查:
http://outworkers.com/blog/post/a-series-on-cassandra-part-3-advanced-features
https://github.com/outworkers/phantom/wiki/Counter-columns
均田尋找這個信息的幻象DSL版本:
https://github.com/Netflix/astyanax/wiki/Working-with-counter-columns
以下是部分實現。它提出了兩個問題:
我不知道如何從應用程序中獲取值並在計數器表的計數器列中實現增量計數器操作。
如何更新表中與表格中具有不同行數和鍵數的相同條目相關的行。
在thiagos example這兩個表; '歌曲'&'songs_by_artist'都具有相同的行但具有不同的分區(主鍵/集羣列)
我不知道如何在phantom-dsl中更新與相同條目有關的行,例如「記錄「&」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)
@flavian這可能是一個你可能能夠澄清的人... –