2

我是 Cassandra 的新手,我正在尝试使用 php 恢复多行,但性能确实很差。

这是我正在使用的代码:

*

<?php
$GLOBALS['THRIFT_ROOT'] = 'D:/cassandra/thrift/lib/php/src';
require_once $GLOBALS['THRIFT_ROOT'].'/packages/cassandra/Cassandra.php';
require_once $GLOBALS['THRIFT_ROOT'].'/packages/cassandra/cassandra_types.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
try {

    $ipmachine = 'localhost';
    $keyspace = 'demo';
    $field_search = 'id_log';
    $column_family = 'logs';
    // Make a connection to the Thrift interface to Cassandra
    $socket = new TSocket($ipmachine, 9160);
    $transport = new TFramedTransport($socket, 1024, 1024);
    $protocol = new TBinaryProtocol($transport);
    $client = new cassandra_CassandraClient($protocol);
    $transport->open();

    $consistency_level = ConsistencyLevel::ONE;

    $client->set_keyspace($keyspace);

    // Specify what Column Family to query against.
    $columnParent = new cassandra_ColumnParent();
    $columnParent->column_family = $column_family;
    $columnParent->super_column = NULL;
    $sliceRange = new cassandra_SliceRange();
    $sliceRange->start = "";
    $sliceRange->finish = "";    

    $predicate = new cassandra_SlicePredicate();
    $predicate->slice_range = $sliceRange;
    $numelements = 100;

    $keyRange = new cassandra_KeyRange();
    $keyRange->start_key= "";
    $keyRange->end_key = "";
    $keyRange->count =$numelements;

    $result = $client->get_range_slices($columnParent, $predicate, $keyRange, $consistency_level);

    if(!empty($result)){
    $continue = 1;
        $start_key = 1;
    while ($continue <=5){

            $keyRange = new cassandra_KeyRange();
        $keyRange->start_key= $start_key;
        $keyRange->end_key = "";
        $keyRange->count =$numelements;

        $t = microtime(true);
        $micro = sprintf("%06d",($t - floor($t)) * 1000000);
        $d1 = new DateTime( date('Y-m-d H:i:s.'.$micro,$t) );
        $now = $d1->format("H:i:s.u");

        echo $now .'................';  
        $result = $client->get_range_slices($columnParent, $predicate, $keyRange, $consistency_level);
        $t = microtime(true);
        $micro = sprintf("%06d",($t - floor($t)) * 1000000);
        $d2 = new DateTime( date('Y-m-d H:i:s.'.$micro,$t) );
        $now = $d2->format("H:i:s.u");

        echo $now . '<br>';
        // DO SOMETHING WITH THE DATA AND CHANGE 
            $start_key = $start_key * $numelements;
        $continue++;

    }
   }    

    $transport->close();

} catch (TException $tx) {
   print 'TException: '.$tx->getLine(). '<br>Error: '.$tx->getMessage();
   print '<br>Code '.$tx->getCode(). '<br>traza: '.$tx->getTraceAsString();
}
?>

*

结果,这就是我得到的

初始时间 结束时间
19:13:39.534957......................19:13:40.220973
19:13:40.221050...... .19:13:40.892968
19:13:40.893044......................19:13:41.575102
19:13:41.575181...... ...19:13:42.256830
19:13:42.256906......................19:13:42.936492

因此,要恢复 5 个 100 行的块需要 3 秒。

我怎样才能提高性能?有没有其他方法可以使用 thrift 而不是使用 get_range_slices 从 Cassandra 恢复数据?

我也尝试使用更大的计数器,而不是 100 个元素,但它或多或少需要相同的时间。

我需要恢复超过 100.000 行,所以你可以想象进度是可怕的。

4

0 回答 0