Cassandra CONNECT

Source: Internet
Author: User
Keywords Name value function null nsis

&http://www.aliyun.com/zixun/aggregation/37954.html >nbsp;

<?php//Setting up nodes:////cassandraconn::add_node (' 192.168.1.1 ', 9160);//Cassandraconn::add_node (' 192.168.1.2 ', 5000);////querying:////$users = new CASSANDRACF (' Keyspace1 ', ' users ');//$users->insert (' 1 ', Array (' Email ' => ' hoan.tonthat@gmail.com ', ' Password ' => ' test ');//$users->get (' 1 ');//$users->multiget (Array ( 1, 2))//$users->get_count (' 1 ');//$users->get_range (' 1 ', ' ten ');//$users->remove (' 1 ');//$users-> Remove (' 1 ', ' Password ');//class cassandraconn {Const DEFAULT_THRIFT_PORT = 9160; static private $connections = Array (); static private $last _error; static public Function Add_node ($host, $port =self::D efault_thrift_port, $framed _transport=false, $send _timeout=null, $recv _timeout=null, $persist =false) {try {//Create Thrift by and binary Kyoto client $socket = new Tsocket ($host, $port, $persist); if ($send _timeout) $socket->setsendtimeout ($send _timeout); if ($recv _timeout) $socket->setrecvtimeout ($recv _timeout); if ($Framed_transport) {$transport = new Tframedtransport ($socket, True, true);} else {$transport = new Tbufferedtransport ($ Socket, 1024, 1024); $client = new Cassandraclient (new tbinaryprotocolaccelerated ($transport)); Store it in the 50x15 self:: $connections [] = Array (' By ' => $transport, ' client ' => $client); do return TRUE; The catch (Texception $tx) {self:: $last _error = ' texception: '. $tx->getmessage (). "\ n"; return FALSE; }//Default client static public function get_client ($write _mode = False) {//* Try to connect to every Cassandra node in order// * Failed 50x15 would be retried//* Once A connection are opened, it stays open//* Todo:add random and round robin order// * Todo:add write-preferred and read-preferred nodes Shuffle (self:: $connections); foreach (self:: $connections as $connection) {try {$transport = $connection [' by ']; $client = $connection [' Client '] ; if (! $transport->isopen ()) {$transport->open ();} return $client; The catch (Texception $tx) {self:: $last _error = ' texception: '. $tx->getmessage (). "\ n"; re-enters; } throw new Exception ("Could not connect to a Cassandra server"); }}class Cassandrautil {//UUID static public function uuid1 ($node = "", $ns = "") {return uuid::generate uuid::uuid_time, Uuid::fmt_string, $node, $ns); }//Time static public function Get_time () {//by Zach Buller (zachbuller@gmail.com) $time 1 = microtime (); Settype ($time 1, ' String '); Needs converted to string, otherwise'll omit Node-set zeroes $time 2 = Explode ("", $time 1); $time 2[0] = preg_replace ('/0./', ', $time 2[0], 1); $time 3 = ($time 2[1]. $time 2[0])/100; return $time 3; }}class CASSANDRACF {Const DEFAULT_ROW_LIMIT =//DEFAULT max # of rows for Get_range () const DEFAULT_COLUMN_TYPE = " Utf8type "; Const DEFAULT_SUBCOLUMN_TYPE = null; Public $keyspace; Public $column _family; Public $is _super; Public $read _consistency_level; Public $write _consistency_level; Public $column _type; Comparewith (todo:actually $subcolumn _type; Comparesubcolumnswith (todo:actually) public $parse _columns; /* bytestype:simple Sort by byte value. No validation is performed. Asciitype:like Bytestype, but validates that the input can be parsed as ASCII. UTF8TYPE:A string encoded as UTF8 longtype:a 64bit long lexicaluuidtype:a 128bit UUID, compared lexically (by byte value) Timeuuidtype:a 128bit version 1 UUID, compared by timestamp/public function __construct ($keyspace, $column _family, $is _ Super=false, $column _type=self::D efault_column_type, $subcolumn _type=self::D Efault_subcolumn_type, $read _ Consistency_level=cassandra_consistencylevel::one, $write _consistency_level=cassandra_consistencylevel::zero) {/ /Vars $this->keyspace = $keyspace; $this->column_family = $column _family; $this->is_super = $is _super; $this->column_type = $column _type; $this->subcolumn_type = $subcolumn _type; $this->read_consistency_level = $read _consistency_level; $this->wRite_consistency_level = $write _consistency_level; Toggles parsing columns $this->parse_columns = true; Public function Get ($key, $super _column=null, $slice _start= "", $slice _finish= "", $column _reversed=false, $column _ count=100) {$column _parent = new Cassandra_columnparent (); $column _parent->column_family = $this->column_family ; $column _parent->super_column = $this->unparse_column_name ($super _column, true); $slice _range = new Cassandra_slicerange (); $slice _range->count = $column _count; $slice _range->reversed = $column _reversed; $slice _range->start = $slice _start? $this->unparse_column_name ($slice _start, false): ""; $slice _range->finish = $slice _finish? $this->unparse_column_name ($slice _finish, false): ""; $predicate = new Cassandra_slicepredicate (); $predicate->slice_range = $slice _range; $client = Cassandraconn::get_client (); $resp = $client->get_slice ($this->keyspace, $key, $column _parent, $predicate, $this->read_consistency_level); if ($super _column) {return $this->supercolumns_or_columns_to_array ($RESP, false);} else {return $this-> Supercolumns_or_columns_to_array ($RESP); The Public Function Multiget ($keys, $slice _start= "", $slice _finish= "") {$column _parent = new Cassandra_columnparent (); $ column_parent->column_family = $this->column_family; $column _parent->super_column = NULL; $slice _range = new Cassandra_slicerange (); $slice _range->start = $slice _start? $this->unparse_column_name ($slice _start, false): ""; $slice _range->finish = $slice _finish? $this->unparse_column_name ($slice _finish, false): ""; $predicate = new Cassandra_slicepredicate (); $predicate->slice_range = $slice _range; $client = Cassandraconn::get_client (); $resp = $client->multiget_slice ($this->keyspace, $keys, $column _parent, $predicate, $this->read_consistency _level); $ret = null;//foreach ($keys as $sk => $k) {//$ret [$k] = $this->supercolumns_or_columns_to_array ($resp [$k]);//} foreach ($resp As $key => $val) {$ret [$key] = $this->supercolumns_or_columns_to_array ($val);} return $ret; The Public Function get_count ($key, $super _column=null) {$column _path = new Cassandra_columnpath (); $column _path-> column_family = $this->column_family; $column _path->super_column = $super _column; $client = Cassandraconn::get_client (); $resp = $client->get_count ($this->keyspace, $key, $column _path, $this->read_consistency_level); return $RESP; Public Function Get_range ($start _key= "", $end _key= "", $row _count=self::D efault_row_limit, $slice _start= "", $slice _ Finish= "") {$column _parent = new Cassandra_columnparent (); $column _parent->column_family = $this->column_family ; $column _parent->super_column = NULL; $slice _range = new Cassandra_slicerange (); $slice _range->start = $slice _start? $this->unparse_column_name ($slice _start, true): ""; $slice _range->finish = $slice _finish? $this->unparse_column_name ($slice _finish, true): ""; $predicate = new CassanDra_slicepredicate (); $predicate->slice_range = $slice _range; $key _range = new Cassandra_keyrange (); $key _range->start_key = $start _key; $key _range->end_key = $end _key; $key _range->count = $row _count; $client = Cassandraconn::get_client (); $resp = $client->get_range_slices ($this->keyspace, $column _parent, $predicate, $key _range, $this->read_ Consistency_level); return $this->keyslices_to_array ($RESP); Public Function Insert ($key, $columns) {$timestamp = Cassandrautil::get_time (); $cfmap = Array (); $cfmap [$key] [$this- >column_family] = $this->array_to_mutation ($columns, $timestamp); $client = Cassandraconn::get_client (); $resp = $client->batch_mutate ($this->keyspace, $cfmap, $this->write_consistency_level); return $RESP; The Public function remove ($key, $column _name=null) {$timestamp = Cassandrautil::get_time (); $column _path = new Cassandra_ Columnpath (); $column _path->column_family = $this->column_family; if ($this->is_super) {$column _path->super_column = $this->unparse_column_name ($column _name, true); else {$column _path->column = $this->unparse_column_name ($column _name, false);} $client = Cassandraconn::get_ Client (); $resp = $client->remove ($this->keyspace, $key, $column _path, $timestamp, $this->write_consistency_level); return $RESP; }//Wrappers public Function get_list ($key, $key _name= ' key ', $slice _start= "", $slice _finish= "") {//moment-in is on supercols! $ RESP = $this->get ($key, NULL, $slice _start, $slice _finish); $ret = Array (); foreach ($resp as $_key => $_value) {$_value[$key _name] = $_key; $ret [] = $_value;} return $ret; Public Function get_range_list ($key _name= ' key ', $start _key= "", $end _key= "", $row _count=self::D efault_row_limit, $ Slice_start= "", $slice _finish= "") {$resp = $this->get_range ($start _key, $end _key, $row _count, $slice _start, $slice _ Finish); $ret = Array (); foreach ($resp as $_key => $_value) {if (!empty ($_value)) {//filter nulls $_value[$key _name] = $_key; $rEt[] = $_value; return $ret; The Public Function multiget_list ($keys, $key _name= ' key ', $slice _start= "", $slice _finish= "") {$resp = $this->multiget ( $keys, $slice _start, $slice _finish); $ret = Array (); foreach ($resp as $_key => $_value) {$_value[$key _name] = $_key; $ret [] = $_value;} return $ret; }//Helpers for parsing Cassandra ' s thrift objects to PHP arrays public function Keyslices_to_array ($keyslices) {$ret = Null; foreach ($keyslices as $keyslice) {$key = $keyslice->key; $columns = $keyslice->columns; $ret [$key] = $this-> Supercolumns_or_columns_to_array ($columns); return $ret; The Public Function Supercolumns_or_columns_to_array ($array _of_c_or_sc, $parse _as_columns=true) {$ret = null; foreach ($ ARRAY_OF_C_OR_SC as $c _or_sc) {if ($c _or_sc->column) {//normal columns $name = $this->parse_column_name ($c _or_sc- >column->name, $parse _as_columns); $value = $c _or_sc->column->value; $ret [$name] = $value; else if ($c _or_sc->super_column) {//Super columns $name = $this->parse_column_name ($c _or_sc->super_column->name, $parse _as_columns); $columns = $c _or_sc->super_column->columns; $ret [$name] = $this->columns_to_array ($columns); return $ret; The Public Function Columns_to_array ($array _of_c) {$ret = null; foreach ($array _of_c as $c) {$name = $this->parse_column_ Name ($c->name, false); $value = $c->value; $ret [$name] = $value; return $ret; }//Helpers for turning PHP arrays to Cassandra ' Thrift objects Public Function array_to_mutation ($array, $timestamp = NULL) {if (empty ($timestamp)) $timestamp = Cassandrautil::get_time (); $c _or_sc = $this->array_to_supercolumns_or_ Columns ($array, $timestamp); $ret = null; foreach ($c _or_sc as $row) {$mutation = new cassandra_mutation (); $mutation->column_or_supercolumn = $row; $ret [] = $ Mutation; return $ret; The Public Function array_to_supercolumns_or_columns ($array, $timestamp =null) {if (empty ($timestamp)) $timestamp = Cassandrautil::get_time (); $ret = NULL; foreach ($array as $name => $value) {$c _or_sc = new Cassandra_columnorsupercolumn (); if (Is_array ($value)) {$c _or_sc- >super_column = new Cassandra_supercolumn (); $c _or_sc->super_column->name = $this->unparse_column_name ($name, true); $c _or_sc->super_column->columns = $this->array_to_columns ($value, $timestamp); $c _or_sc->super_column->timestamp = $timestamp; else {$c _or_sc = new Cassandra_columnorsupercolumn (); $c _or_sc->column = new Cassandra_column (); $c _or_sc-> Column->name = $this->unparse_column_name ($name, true); $c _or_sc->column->value = $this->to_column_value ($value); $c _or_sc->column->timestamp = $timestamp; $ret [] = $c _or_sc; return $ret; The Public Function array_to_columns ($array, $timestamp =null) {if (empty ($timestamp)) $timestamp = cassandrautil::get_ Time (); $ret = null; foreach ($array as $name => $value) {$column = new cassandra_column (); $column->name = $this->unparse_column_name ($name, FALSE); $column->value = $this->to_column_value ($value); $column->timestamp = $timestamp; $ret [] = $column; return $ret; The Public Function To_column_value ($thing) {if ($thing = = null) return ""; return $thing; }//Argh public Function Parse_column_name ($column _name, $is _column=true) {if (! $this->parse_columns) return $column _name; if (! $column _name) return NULL; $type = $is _column? $this->column_type: $this->subcolumn_type; if ($type = = "Lexicaluuidtype" | | $type = = "Timeuuidtype") {return Uuid::convert ($column _name, Uuid::fmt_binary, UUID:: fmt_string); else if ($type = = "Longtype") {return $this->unpack_longtype ($column _name);} else {return $column _name}} Public Function Unparse_column_name ($column _name, $is _column=true) {if (! $this->parse_columns) return $column _name ; if (! $column _name) return NULL; $type = $is _column? $this->column_type: $this->subcolumn_type; if ($type = = "Lexicaluuidtype" | | $type = = "Timeuuidtype") {return Uuid::convert ($coLumn_name, uuid::fmt_string, uuid::fmt_binary); else if ($type = = "Longtype") {return $this->pack_longtype ($column _name);} else {return $column _name}} Http://webcache.googleusercontent.com/search?q=cache:9jjbeSy434UJ:wiki.apache.org/cassandra/FAQ+cassandra +php+%22a+long+is+exactly+8+bytes%22&cd=1&hl=en&ct=clnk&gl=us Public Function Pack_longtype ($x) { Return pack (' C8 ', ($x >>) & 0xFF, ($x >>) & 0xFF, ($x >>) & 0xFF, ($x >>) & 0xf F, ($x >>) & 0xFF, ($x >>) & 0xFF, ($x >> 8) & 0xFF, $x & 0xff); The Public Function Unpack_longtype ($x) {$a = unpack (' C8 ', $x); return ($a [1] <<) + ($a [2] <<) + ($a [3] << ($a [4] <<) + ($a [5] <<) + ($a [6] <<) + ($a [7] << 8) + $a [8]; }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.