使用 phpcassa 显示所有键
我对 cassandra 相当陌生,但到目前为止我已经取得了良好的进展。
$conn = new ConnectionPool('Cluster');
$User = new ColumnFamily($conn, 'User');
$index_exp = CassandraUtil::create_index_expression('email', '[email protected]');
$index_clause = CassandraUtil::create_index_clause(array($index_exp));
$rows = $User->get_indexed_slices($index_clause);
foreach($rows as $key => $columns) {
echo $columns['name']."<br />";
}
我使用这种类型的查询来从某人的电子邮件地址获取特定日期。 然而,我现在想做两件事。
- 计算数据库中的每个用户并显示数量
- 使用 $columns['name'] 列出数据库中的每个用户。" ".$columns['email']
在 mysql 中,我只需从选择查询中删除 'where 属性' ,但是我认为这里有点复杂?
I'm fairly new to cassandra but i have making good progress so far.
$conn = new ConnectionPool('Cluster');
$User = new ColumnFamily($conn, 'User');
$index_exp = CassandraUtil::create_index_expression('email', '[email protected]');
$index_clause = CassandraUtil::create_index_clause(array($index_exp));
$rows = $User->get_indexed_slices($index_clause);
foreach($rows as $key => $columns) {
echo $columns['name']."<br />";
}
Im using this type of query to get specific date from somebodys email adress.
However, i now want to do 2 things.
- Count every user in the database and display the number
- List every user in the database with $columns['name']." ".$columns['email']
In mysql i would just remove the 'where attribute' from the select query, however i think its a little bit more complicated here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Cassandra 中,没有简单的方法来计算所有行。您基本上必须扫描所有内容。如果这是您经常想做的事情,那么您就做错了。示例代码:
第二个答案类似:
In Cassandra there's no easy way to count all of the rows. You basically have to scan everything. If this is something that you want to do often, you're doing it wrong. Example code:
The second answer is similar:
泰勒霍布斯举了一个很好的例子。
但是,如果您有很多用户,您不希望一直对他们进行迭代。
最好每天进行一次或两次迭代,并将数据存储在 cassandra 或 memcached/redis 中。
我还会做一个单行 CF 并将所有用户名(或用户密钥)放在单行上。然而,有些人认为这是奇怪的做法,有些人不会推荐它。然后你就可以:
注意 get_count() 操作也很慢,所以你仍然需要缓存它。
如果 get_count() 返回 100,您将需要将 phpcassa 升级到最新版本。
关于第二部分 - 如果你的用户少于 4000-5000 个,我会再次做一些奇怪的事情 - 将然后放在单行上作为超级列。然后只需一项操作即可读取:
Tyler Hobbs give very nice example.
However if you have many users, you do not want to iterate on them all the time.
It is better to have this iteration once or twice per day and to store the data in cassandra or memcached / redis.
I also would do a CF with single row and put all usernames (or user keys) there on single row. However some considered this as odd practice and some people will not recommend it. Then you do:
note get_count() is slow operation too, so you still need to cache it.
If get_count() returns 100, you will need to upgrade your phpcassa to latest version.
About second part - if you have less 4000-5000 users, I would once again do something odd - put then on single row as supercolumns. Then read will be with just one operation: