基礎noSQL連接與實作
Client連接基礎語法實測兩種
注意以下作法無關framework , laravel & Phalcon作法相同
1. yaCassandrapdo 2.phpCass
1. yaCassandrapdo
連接方法
$setting = 'cassandra:host=10.20.30.222;port=9160;cqlversion=3.0.0';
$username = "";
$password = "";
$db = new PDO($setting, $username, $password);
建立資料表
Route::get('api/pdo_cass/create' , function(){
$db->exec ("CREATE KEYSPACE yapdo WITH REPLICATION =
{'class' : 'SimpleStrategy', 'replication_factor': 1}");
}) ;
SQL查詢
Route::get('api/pdo_cass/get' , function(){
$setting = 'cassandra:host=10.20.30.222;port=9160;
cqlversion=3.0.0';
$username = "";
$password = "";
$db = new PDO($setting, $username, $password);
$db->exec ("USE \"nl_keyspace\";");
$result = $db->query("SELECT * FROM account")->fetchObject();
var_dump($result) ;
}) ;
插入資料 & 更新資料
Route::get('api/pdo_cass/insert' , function(){
$setting = 'cassandra:host=10.20.30.222;port=9160;
cqlversion=3.0.0';
$username = "";
$password = "";
$db = new PDO($setting, $username, $password);
$db->exec ("USE \"nl_keyspace\";");
$db->exec("INSERT INTO test (id, map_test)
VALUES (1, {'val1': 2, 'val2': 1, 'val3': 3});");
//注意若插入key值等同原來值,就會自動將之後欄位值更新
}) ;
刪除資料
Route::get('api/pdo_cass/delete' , function(){
$setting = 'cassandra:host=10.20.30.222;port=9160;
cqlversion=3.0.0';
$username = "";
$password = "";
$db = new PDO($setting, $username, $password);
$db->exec ("USE \"nl_keyspace\";");
$db->exec("DELETE FROM MastersOfTheUniverse
WHERE mastersID IN ('Man-At-Arms', 'Teela');");
}) ;
phpCass
$servers = array("10.20.30.222:9160" , "10.20.30.223:9160") ;
$pool = new ConnectionPool("devKeyspace" , $servers) ;
建立資料表
$sys = new SystemManager('127.0.0.1');
$sys->create_keyspace('Keyspace1', array(
"strategy_class" => StrategyClass::SIMPLE_STRATEGY,
"strategy_options" => array('replication_factor' => '1')
));
// Use composites for column names and row keys
$sys->create_column_family('Keyspace1', 'Composites', array(
"comparator_type" => "CompositeType(LongType, AsciiType)",
"key_validation_class" => "CompositeType(AsciiType, LongType)"
));
SQL查詢
Route::get('api/cass/get ' , function()
{
$servers = array("10.20.30.222:9160", "10.20.30.223:9160" ) ;
$pool = new ConnectionPool("nl_keyspace" , $servers) ;
$column_family = new ColumnFamily($pool, "table_name");
$result = $column_family->get('key');
echo $result["col"];
echo $result["value"];
foreach($result as $key => $columns) {
print_r($columns."
");
}
});
插入資料 & 更新資料
Route::get('api/cass/insert_update' , function()
{
$servers = array("10.20.30.222:9160", "10.20.30.223:9160" ) ;
$pool = new ConnectionPool("nl_keyspace" , $servers) ;
$column_family = new ColumnFamily($pool, "a3");
$result = $column_family->insert( 'a4' , array('col' => '100' ,'value'=>'4444') ) ;
//注意若插入key值等同原來值,就會自動將之後欄位值更新
});
刪除資料
Route::get('api/cass/delete' , function()
{
$servers = array("10.20.30.222:9160", "10.20.30.223:9160" ) ;
$pool = new ConnectionPool("nl_keyspace" , $servers) ;
$column_family = new ColumnFamily($pool, "table_name");
$result = $column_family->remove('key');
});
沒有留言:
張貼留言