使用 phpcassa 显示所有键

发布于 2024-11-04 22:30:26 字数 760 浏览 6 评论 0原文

我对 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 />";
}

我使用这种类型的查询来从某人的电子邮件地址获取特定日期。 然而,我现在想做两件事。

  1. 计算数据库中的每个用户并显示数量
  2. 使用 $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.

  1. Count every user in the database and display the number
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2024-11-11 22:30:26

在 Cassandra 中,没有简单的方法来计算所有行。您基本上必须扫描所有内容。如果这是您经常想做的事情,那么您就做错了。示例代码:

$rows = $User->get_range("", "", 1000000);
$count = 0;
foreach($rows as $row) {
    $count += 1;
}

第二个答案类似:

$rows = $User->get_range("", "", 1000000, null, array("name", "email"));
foreach($rows as $key => $columns) {
    echo $columns["name"]." ".$columns["email"];
}

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:

$rows = $User->get_range("", "", 1000000);
$count = 0;
foreach($rows as $row) {
    $count += 1;
}

The second answer is similar:

$rows = $User->get_range("", "", 1000000, null, array("name", "email"));
foreach($rows as $key => $columns) {
    echo $columns["name"]." ".$columns["email"];
}
你如我软肋 2024-11-11 22:30:26

泰勒霍布斯举了一个很好的例子。

但是,如果您有很多用户,您不希望一直对他们进行迭代。

最好每天进行一次或两次迭代,并将数据存储在 cassandra 或 memcached/redis 中。


我还会做一个单行 CF 并将所有用户名(或用户密钥)放在单行上。然而,有些人认为这是奇怪的做法,有些人不会推荐它。然后你就可以:

$count = $cf->get_count($rowkey = 0);

注意 get_count() 操作也很慢,所以你仍然需要缓存它。

如果 get_count() 返回 100,您将需要将 phpcassa 升级到最新版本。


关于第二部分 - 如果你的用户少于 4000-5000 个,我会再次做一些奇怪的事情 - 将然后放在单行上作为超级列。然后只需一项操作即可读取:

$users = $scf->get($rowkey = 0, new ColumnSlice("", "", 5000));
foreach($users 作为 $user){
echo $user["姓名"]." ".$user["电子邮件"];
}

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:

$count = $cf->get_count($rowkey = 0);

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:

$users = $scf->get($rowkey = 0, new ColumnSlice("", "", 5000));
foreach($users as $user){
echo $user["name"]." ".$user["email"];
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文