当前位置:   article > 正文

php程序使用cassandra_cassandra php

cassandra php

PHP 程序使用 Cassandra

在 PHP 代码中使用 Cassandra,需要借助 Thrift 来生成需要的 PHP 文件,通过使用 thrift --gen php interface/cassandra.thrift 生成所需要的 PHP 文件,生成的 PHP 文件中提供了与 Cassandra 建立连接、读写数据时所需要的函数。

 <?php 
 $GLOBALS['THRIFT_ROOT'] = '/usr/share/php/Thrift'; 
 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 { 
 // 建立 Cassandra 连接
 $socket = new TSocket('192.168.10.2', 9160); 
 $transport = new TBufferedTransport($socket, 1024, 1024); 
 $protocol = new TBinaryProtocolAccelerated($transport); 
 $client = new CassandraClient($protocol); 
 $transport->open(); 
 $keyspace = 'Keyspace1'; 
 $keyUser = "studentA"; 
 $columnPath = new cassandra_ColumnPath(); 
 $columnPath->column_family = 'Standard1'; 
 $columnPath->super_column = null; 
 $columnPath->column = 'age'; 
 $consistency_level = cassandra_ConsistencyLevel::ZERO; 
 $timestamp = time(); 
 $value = "18"; 
 // 写入数据
 $client->insert($keyspace, $keyUser, $columnPath, $value, 
                            $timestamp, $consistency_level); 
 $columnParent = new cassandra_ColumnParent(); 
 $columnParent->column_family = "Standard1"; 
 $columnParent->super_column = NULL; 
 $sliceRange = new cassandra_SliceRange(); 
 $sliceRange->start = ""; 
 $sliceRange->finish = ""; 
 $predicate = new cassandra_SlicePredicate(); 
 list() = $predicate->column_names; 
 $predicate->slice_range = $sliceRange; 
 $consistency_level = cassandra_ConsistencyLevel::ONE; 
 $keyUser = studentA; 
 // 查询数据
 $result = $client->get_slice($keyspace, $keyUser, $columnParent, 
             $predicate, $consistency_level); 
 // 关闭连接
 $transport->close(); 
 } catch (TException $tx) { 
 }?>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/126228?site
推荐阅读
相关标签
  

闽ICP备14008679号