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 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏