如何使用 phantom-dsl 增加 Cassandra 计数器列?
How to increment Cassandra Counter Column with phantom-dsl?
有没有在phantom-dsl中实现计数器操作的例子?
已检查:
http://outworkers.com/blog/post/a-series-on-cassandra-part-3-advanced-features
https://github.com/outworkers/phantom/wiki/Counter-columns
有点在寻找此信息的 phantom-dsl 版本:
https://github.com/Netflix/astyanax/wiki/Working-with-counter-columns
以下是部分实现。已经升了两个问题:
我不确定如何从应用程序中获取值并在计数器 table 的计数器列中实现增量计数器操作。
如何更新 table 内与同一条目相关的行,其中 table 具有不同的行数和键数。
在thiagos example两个table; 'songs' & 'songs_by_artist' 都有相同的行但分区不同(主键/聚簇列)
我不确定在 phantom-dsl 中如何更新与相同条目相关的行,例如下面的 "records" & "record_transaction_counts" tables。
例如
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)
为了在 phantom-dsl 中使用 CounterColumn
,您必须使用以下模式来递增它:
.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。另外如果大家有什么建议,请开工单,我会提的。
正如 Thiago 所建议的,您可以使用 +=
或 -=
运算符来减少计数器的值。您也可以分别使用 increment
或 decrement
方法来实现相同的目的。
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)
在过于依赖计数器之前,您也应该Google稍微了解一下其他人遇到的问题。
有没有在phantom-dsl中实现计数器操作的例子?
已检查:
http://outworkers.com/blog/post/a-series-on-cassandra-part-3-advanced-features
https://github.com/outworkers/phantom/wiki/Counter-columns
有点在寻找此信息的 phantom-dsl 版本:
https://github.com/Netflix/astyanax/wiki/Working-with-counter-columns
以下是部分实现。已经升了两个问题:
我不确定如何从应用程序中获取值并在计数器 table 的计数器列中实现增量计数器操作。
如何更新 table 内与同一条目相关的行,其中 table 具有不同的行数和键数。
在thiagos example两个table; 'songs' & 'songs_by_artist' 都有相同的行但分区不同(主键/聚簇列)
我不确定在 phantom-dsl 中如何更新与相同条目相关的行,例如下面的 "records" & "record_transaction_counts" tables。
例如
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)
为了在 phantom-dsl 中使用 CounterColumn
,您必须使用以下模式来递增它:
.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。另外如果大家有什么建议,请开工单,我会提的。
正如 Thiago 所建议的,您可以使用 +=
或 -=
运算符来减少计数器的值。您也可以分别使用 increment
或 decrement
方法来实现相同的目的。
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)
在过于依赖计数器之前,您也应该Google稍微了解一下其他人遇到的问题。