douwei1921 2017-06-05 06:56
浏览 51
已采纳

Cassandra PHP驱动程序502 badgateway来自准备好的声明

I'm using this driver for cassandra: https://github.com/datastax/php-driver

Here's the code I used to create the table:

CREATE TABLE test.packages (
     packageuuid timeuuid, 
     ruserid text, 
     suserid text, 
     timestamp int, 
     PRIMARY KEY (ruserid, suserid, packageuuid, timestamp)
);

and then I create a materialized view:

CREATE MATERIALIZED VIEW test.packages_by_userid 
    AS SELECT * FROM test.packages 
    WHERE ruserid IS NOT NULL 
       AND suserid IS NOT NULL 
       AND TIMESTAMP IS NOT NULL 
       AND packageuuid IS NOT NULL 
    PRIMARY KEY (ruserid, suserid, timestamp, packageuuid) 
    WITH CLUSTERING ORDER BY (packageuuid DESC);

and here's a snippet of my PHP code which causes 502 bad gateway:

$session   = $cluster->connect($keyspace);
$selectstmt = $session->prepare("SELECT suserid, susername, snickname, msg, savatar, timestamp FROM packages_by_userid WHERE ruserid IN (?, ?) AND suserid IN (?, ?) AND timestamp < ? LIMIT 40;");
$params = array('ruserid' => array($rid, $sid), 'suserid' => array($rid, $sid), 'timestamp' => $endtimestamp);
$options = array('arguments' => $params);
$future    = $session->executeAsync($selectstmt, $options);
$result    = $future->get();

I believe I messed up with binding the parameters to the prepared statement. What's the proper way to do that in my case as I have to bind more than one value to ruserid and suserid?

Thanks to anyone who can help.

  • 写回答

1条回答 默认 最新

  • duanchun2349 2017-06-05 07:10
    关注

    I suggest you to change your data model like below :

    CREATE TABLE packages (
        cnvid text,
        packageuuid timeuuid,
        ruserid text,
        suserid text,
        PRIMARY KEY (cnvid, packageuuid)
    );
    

    Here cnvid is the conversation id. You can make the conversation id using the below function :

     function makeConversationId($ruserid, $suserid ) {
        return $ruserid <= $suserid ? $ruserid . ':' . $suserid : $suserid . ':' . $ruserid ;
     }
    

    Whoever the sender or receiver your conversation id will be same. i.e

     echo makeConversationId('1', '2') . '<br/>';
     echo makeConversationId('2', '1'); 
    

    Output :

    1:2
    1:2
    

    Now you have the cnvid, whenever you insert/update/delete/select use the above method to make cnvid.

    And though packageuuid is the timeuuid, all your packageuuid will be sorted by time.

    So to query first create the cnvid (i.e 1:2) then create a timeuuid with the timestamp for which you want to query (i.e 896f8110-49d4-11e7-ade6-493d3332b999)

    Check this : https://datastax.github.io/php-driver/api/Cassandra/class.Timeuuid/

    So you can query to get packages of user between 1 and 2 whose timestamp greater than the given timestamp :

    Example Data :

     cnvid | packageuuid                          | ruserid | suserid
    -------+--------------------------------------+---------+---------
       3:4 | bc0809e0-49d3-11e7-ade6-493d3332b999 |       3 |       4
       1:2 | 1f4aae70-49d1-11e7-ade6-493d3332b999 |       1 |       2
       1:2 | 237ff3b0-49d1-11e7-ade6-493d3332b999 |       2 |       1
    

    Query :

    SELECT * FROM packages WHERE cnvid = '1:2' AND packageuuid < 896f8110-49d4-11e7-ade6-493d3332b999;
    

    Output :

     cnvid | packageuuid                          | ruserid | suserid
    -------+--------------------------------------+---------+---------
       1:2 | 1f4aae70-49d1-11e7-ade6-493d3332b999 |       1 |       2
       1:2 | 237ff3b0-49d1-11e7-ade6-493d3332b999 |       2 |       1
    

    So you don't need the Materialized View and In query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条