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 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示