public function testCreateSecondaryIndex() { $cluster = new \CouchbaseCluster('127.0.0.1'); $clusterManager = $cluster->manager('Administrator', 'Administrator'); $clusterManager->createBucket($this->bucket, ['bucketType' => 'couchbase', 'saslPassword' => '', 'flushEnabled' => true]); sleep(5); /** @var \Ytake\LaravelCouchbase\Database\CouchbaseConnection $connection */ $connection = $this->databaseManager->connection('couchbase'); $bucket = $connection->openBucket($this->bucket); $bucket->manager()->createN1qlPrimaryIndex(); sleep(4); $output = new \Symfony\Component\Console\Output\BufferedOutput(); $this->command->run(new \Symfony\Component\Console\Input\ArrayInput(['bucket' => $this->bucket, 'name' => 'testing_gsi', 'fields' => ['params1', 'params2']]), $output); $fetch = $output->fetch(); $this->assertSame("created SECONDARY INDEX [testing_gsi] fields [params1,params2], for [index_testing] bucket.", trim($fetch)); /** @var \Ytake\LaravelCouchbase\Database\CouchbaseConnection $connection */ $connection = $this->databaseManager->connection('couchbase'); $bucket = $connection->openBucket($this->bucket); $indexes = $bucket->manager()->listN1qlIndexes(); foreach ($indexes as $index) { if (!$index->isPrimary && $index->keyspace === 'keyspace') { $this->assertSame("testing_gsi", $index->name); $this->assertInstanceOf('CouchbaseN1qlIndex', $index); } } $bucket->manager()->dropN1qlPrimaryIndex(); $bucket->manager()->dropN1qlIndex('testing_gsi'); $clusterManager->removeBucket($this->bucket); }
public function upsert($config, $key, $value) { if (!self::$bucket) { $cluster = new \CouchbaseCluster($config["connectionString"]); self::$bucket = $cluster->openBucket($config["bucket"]); } self::$bucket->upsert($key, $value); }
/** * Open the connection to Couchbase (called by PHP on `session_start()`) */ public function open($savePath, $sessionName) { $cluster = new CouchbaseCluster($this->_host, $this->_user, $this->_password); $this->_connection = $cluster->openBucket($this->_bucket); $this->_connection->__set('operationTimeout', 2000000); // 100000 = 1 second return $this->_connection ? true : false; }
public function __construct($host = 'localhost', $bucket = 'default') { if (!extension_loaded('couchbase') || !class_exists('CouchbaseCluster')) { $this->client = null; return false; } $cluster = new \CouchbaseCluster('couchbase://' . $host); $this->client = $cluster->openBucket($bucket); }
private function active($request) { $cluster = new CouchbaseCluster('couchbase://localhost', 'root', '123456'); $bucket = $cluster->openBucket((string) $request->getResource()); $resource = $request->getResource(); $token = 20; $query = CouchbaseN1qlQuery::fromString('UPDATE ' . '`' . $resource . '`' . "USE KEYS " . "'" . $token . "'" . 'SET deleted = 0'); $result = $bucket->query($query); return $result; }
public static function setUpBeforeClass() { if (!extension_loaded('couchbase') || !class_exists('CouchbaseCluster')) { static::$cache = null; return; } $cluster = new \CouchbaseCluster('couchbase://localhost'); $bucket = $cluster->openBucket('cachalot-test'); static::$cache = new \Cachalot\Couchbase2Cache($bucket); }
/** * @param array $data * @return array */ protected function openBuckets(array $data) { $buckets = []; foreach ($data as $bucket => $settings) { $buckets[$bucket] = $this->connection->openBucket($bucket, $this->buckets[$bucket]); } return $buckets; }
public function testDropSecondaryIndex() { $cluster = new \CouchbaseCluster('127.0.0.1'); $clusterManager = $cluster->manager('Administrator', 'Administrator'); $clusterManager->createBucket($this->bucket, ['bucketType' => 'couchbase', 'saslPassword' => '', 'flushEnabled' => true]); sleep(5); /** @var \Ytake\LaravelCouchbase\Database\CouchbaseConnection $connection */ $connection = $this->databaseManager->connection('couchbase'); $bucket = $connection->openBucket($this->bucket); $bucket->manager()->createN1qlPrimaryIndex(); $bucket->manager()->createN1qlIndex('testing_gsi', ['params1', 'params2']); $output = new \Symfony\Component\Console\Output\BufferedOutput(); $this->command->run(new \Symfony\Component\Console\Input\ArrayInput(['bucket' => $this->bucket, 'name' => 'testing_gsi']), $output); $fetch = $output->fetch(); $this->assertSame("dropped SECONDARY INDEX [testing_gsi] for [index_testing] bucket.", trim($fetch)); $clusterManager->removeBucket($this->bucket); }
/** * @return bool */ protected function driverConnect() { if ($this->instance instanceof CouchbaseClient) { throw new \LogicException('Already connected to Couchbase server'); } else { $host = isset($this->config['host']) ? $this->config['host'] : '127.0.0.1'; //$port = isset($server[ 'port' ]) ? $server[ 'port' ] : '11211'; $password = isset($this->config['password']) ? $this->config['password'] : ''; $username = isset($this->config['username']) ? $this->config['username'] : ''; $buckets = isset($this->config['buckets']) ? $this->config['buckets'] : [['bucket' => 'default', 'password' => '']]; $this->instance = $this->instance ?: new CouchbaseClient("couchbase://{$host}", $username, $password); foreach ($buckets as $bucket) { $this->bucketCurrent = $this->bucketCurrent ?: $bucket['bucket']; $this->setBucket($bucket['bucket'], $this->instance->openBucket($bucket['bucket'], $bucket['password'])); } } }
define("COUCHBASE_PASSWORD", ""); define("INDEX_DISPLAY_LIMIT", 20); /** * Init Block. * * This block requires the autoloader and initializes the Silex Application. * It also connects to our Couchbase Cluster and registeres the template * engine (Twig). */ // Autoloader require_once __DIR__ . '/vendor/autoload.php'; // Silex-Application Bootstrap $app = new Application(); $app['debug'] = SILEX_DEBUG; // Connecting to Couchbase $cluster = new CouchbaseCluster(COUCHBASE_CONNSTR); $cb = $cluster->openBucket(COUCHBASE_BUCKET, COUCHBASE_PASSWORD); $cb->enableN1ql(array('http://db:8093/')); // Commande pour transférer des données // /opt/couchbase/bin/cbtransfer http://127.0.0.1:8091 http://127.0.0.1:8091 -u Administrator -p Password -b catalogue-assemblage -B catalogue-volumetrie // Commande pour créer l'index // /opt/couchbase/bin/cbq CREATE PRIMARY INDEX on `catalogue-assemblage` USING GSI; // Liste des documents via N1QL $query = CouchbaseN1qlQuery::fromString(' SELECT * FROM `catalogue-assemblage` WHERE type="admin"; '); $res = $cb->query($query); $tmp = $res[0]; $liste = $tmp->{'catalogue-assemblage'}->listeCategorie; $pages = array(); $categories['modeles'] = ['admin'];
public static function getBucketConnection($config) { $cluster = new \CouchbaseCluster($config["connectionString"]); return $cluster->openBucket($config["bucket"]); }
<?php // Connect to Couchbase Server //$cluster = new CouchbaseCluster('http://127.0.0.1:8091'); $cluster = new CouchbaseCluster('couchbase://localhost'); $bucket = $cluster->openBucket('beer-sample'); // Retrieve a document $result = $bucket->get('aass_brewery-juleol'); $doc = $result->value; echo $doc->name . ', ABV: ' . $doc->abv . "\n"; // Store a document $doc->comment = 'Random beer from Norway'; $result = $bucket->replace('aass_brewery-juleol', $doc); var_dump($result);
/** * @test * Test all option values to make sure they save/load * We open a new bucket for this test to make sure our settings * changes do not affect later tests */ function testConfigCache() { $key = $this->makeKey('ccache'); $h = new CouchbaseCluster($this->testDsn . '?config_cache=./test.cache'); $b = $h->openBucket(); $b->upsert($key, 'yes'); $h2 = new CouchbaseCluster($this->testDsn . '?config_cache=./test.cache'); $b2 = $h2->openBucket(); $res = $b2->get($key); $this->assertValidMetaDoc($res, 'value', 'cas', 'flags'); $this->assertEquals($res->value, 'yes'); }
<?php // Connecting to Couchbase $cluster = new CouchbaseCluster("http://db"); $cb = $cluster->openBucket("catalogue-assemblage", ""); $cb->enableN1ql(array('http://db:8093/')); // Liste des documents via N1QL $query = CouchbaseN1qlQuery::fromString(' SELECT ARRAY_AGG(ARRAY_CONCAT(fils.caracteristiques,pere.caracteristiques)) FROM `catalogue-assemblage` fils JOIN `catalogue-assemblage` pere ON KEYS fils.idProduitCommercial WHERE fils.type = "offreCommerciale" ; '); $res = $cb->query($query); //var_dump($res); echo '<pre>', json_encode($res, JSON_PRETTY_PRINT), '</pre>'; //SELECT ARRAY_CONCAT(fils.caracteristiques,pere.caracteristiques) FROM `catalogue-assemblage` fils JOIN `catalogue-assemblage` pere ON KEYS fils.idProduitCommercial WHERE fils.type = "offreCommerciale";
/** * Migrate existing mysql db to couchbase * */ public function index() { $myCluster = new \CouchbaseCluster('couchbase://localhost'); $myBucket = $myCluster->openBucket('5sportal'); $tables = DB::select('show tables'); if (!empty($tables)) { foreach ($tables as $table) { $table = array_values(get_object_vars($table))[0]; $data = $this->getData($table); if (!empty($data)) { foreach ($data as $row) { $item = (array) $row; $docId = $table . '_' . $item['id']; //cast int data type foreach ($item as $field => $val) { if (is_numeric($val)) { $item[$field] = (int) $val; } } if ($table == 'person') { $item['role'] = $item['type']; $item['type'] = $table; $myBucket->upsert($docId, $item); } elseif ($table == 'report') { $item['report_type'] = $item['type']; $item['type'] = $table; $myBucket->upsert($docId, $item); } elseif ($table == 'family') { //do nothing } else { $item['type'] = $table; $myBucket->upsert($docId, $item); } echo 'Inserting document: ' . $docId . '<br/>'; } } } $members = $this->processReportMembers(); //delete existing family in the document $query = \CouchbaseViewQuery::from('family', 'by_report')->stale(1); $res = $myBucket->query($query, null, true); foreach ($res['rows'] as $row) { try { $myBucket->remove($row['id']); } catch (\CouchbaseException $e) { echo $e->getMessage() . "<br/>"; continue; } } foreach ($members as $id => $val) { $tmp = explode('_', $id); $item = ['type' => 'family', 'report_id' => (int) end($tmp), 'members' => $val]; $myBucket->upsert($id, $item); } $tags = $this->processItemTags(); //delete existing tags in the document $query = \CouchbaseViewQuery::from('item', 'item_tags')->stale(1); $res = $myBucket->query($query, null, true); foreach ($res['rows'] as $row) { try { $myBucket->remove($row['id']); } catch (\CouchbaseException $e) { echo $e->getMessage() . "<br/>"; continue; } } foreach ($tags as $id => $val) { $tmp = explode('_', $id); $item = ['type' => 'tags', 'item_id' => (int) end($tmp), 'tags' => $val]; $myBucket->upsert($id, $item); } } return 'Setup done.'; }
<?php $nbDocumentsDemandes = ['usine' => 10, 'produitTechnique' => 350, 'operation' => 10, 'unite' => 20, 'valeur' => 300, 'natureCaracteristique' => 9, 'caracteristique' => 500, 'produitModele' => 20, 'produitGenerique' => 80, 'produitCommercial' => 200, 'offreCommerciale' => 300]; $nbVersions = 10; // Connecting to Couchbase $cluster = new CouchbaseCluster("http://db"); $cb = $cluster->openBucket("catalogue-volumetrie", ""); $cb->enableN1ql(array('http://db:8093/')); // foreach ($modelesFlux as $k => $flux) { // for($i=0; $i<$nbDocumentsDemandes[$k]; $i++) { // for($i=0; $i<4; $i++) { // echo $nbDocumentsDemandes[$k].' : '.$flux.'<br/>'; // $cb->upsert($k.'_'.($i+1), $flux); // } // } for ($version = 0; $version < $nbVersions; $version++) { foreach ($nbDocumentsDemandes as $k => $v) { for ($i = 0; $i < $v; $i++) { $modelesFlux = ['usine' => ' { "type": "usine", "libelle": "' . generate("string", 7, 30, $version) . '" } ', 'produitTechnique' => ' { "type": "produitTechnique", "libelle": "' . generate("string", 40, 60, $version) . '", "libelleCourt": "' . generate("string", 7, 15, $version) . '", "varianteProduit": "' . generate("nb", 4, 6, $version) . '", "idUsine": "' . generate("id", "usine", "", $version) . '" }
protected function doInit($config) { if (!class_exists('CouchbaseCluster')) { return false; } $cluster = new CouchbaseCluster($config['dsn'], $config['username'], $config['password']); try { $this->bucket = $cluster->openBucket($config['name']); } catch (CouchbaseException $e) { KalturaLog::err($e); return false; } if (isset($config['properties'])) { foreach ($config['properties'] as $propertyName => $propertyValue) { $this->bucket->{$propertyName} = $propertyValue; } } if (isset($config['views'])) { foreach ($config['views'] as $view => $viewConfig) { list($designDocumentName, $viewName) = explode(',', $viewConfig, 2); $this->views[$view] = array('designDocumentName' => trim($designDocumentName), 'viewName' => trim($viewName)); } } return true; }
/** * Gets a couchbase cluster resource. * * @param string $id * * @return \CouchbaseBucket * * @throws Exception\RuntimeException */ public function getResource($id) { if (!$this->hasResource($id)) { throw new Exception\RuntimeException("No resource with id '{$id}'"); } $resource = $this->resources[$id]; if ($resource instanceof \CouchbaseBucket) { return $resource; } $memc = new CouchbaseClusterResource('couchbase://' . $resource['server'][0]['host'] . ':' . $resource['server'][0]['port'], (string) $resource['username'], (string) $resource['password']); $bucket = $memc->openBucket((string) $resource['bucket'], (string) $resource['password']); $bucket->setTranscoder($resource['encoder'], $resource['decoder']); // buffer and return $this->resources[$id] = $bucket; return $bucket; }
/** * @param string $bucket * * @return CouchbaseClusterManager */ protected function createBucket($bucket) { $cluster = new \CouchbaseCluster('127.0.0.1'); $clusterManager = $cluster->manager('Administrator', 'Administrator'); $clusterManager->createBucket($bucket, ['bucketType' => 'couchbase', 'saslPassword' => '', 'flushEnabled' => true]); sleep(5); return $clusterManager; }
/** * @test * Test that connections with invalid details fail. * * @expectedException CouchbaseException */ function testBadHost() { $h = new CouchbaseCluster('http://999.99.99.99:8091'); $h->openBucket('default'); }
<?php /** * The following example demonstrates how you might implement caching * for HTTP requests that are being made. In this case, we make requests * to the GitHub api to retrieve couchbaselabs events and cache the result * of this request for 5 seconds to reduce the load on GitHub. */ /* * Create a new Cluster object to represent the connection to our * cluster and specify any needed options such as SSL. */ $cb = new CouchbaseCluster('192.168.7.26'); /* * We open the default bucket to store our cached data in. */ $db = $cb->openBucket('default'); /* * Lets define a function which can wrap executing the curl request and * handling the cache in one. */ function cached_curl_request($path) { global $db; /* * Lets define a key to use for storing out cached request into. We * base this key directly on the path so that we can support caching * many different URIs. We also add a prefix for good measure. */ $cache_key = 'cache_' . $path; /*
#!/usr/bin/php <?php include 'config.php'; $cluster = new CouchbaseCluster('http://swebb-admin.dishonline.com:8091'); $bucket = $cluster->openBucket('soundcloud'); $my_conn = mysql_connect($mysql_host, $mysql_user, $mysql_passwd); if (!mysql_select_db("soundcloud", $my_conn)) { $error_string = "ERROR: can't connect to the DB\n"; print "{$error_string}"; exit(1); } $result = mysql_query("select * from users limit 2000"); while ($row = mysql_fetch_assoc($result)) { $id = $row['id']; $username = $row['username']; $url = $row['url']; $avatar_url = $row['avatar_url']; $followers = $row['followers']; $followings = $row['followings']; print "{$id}\n"; $res = $bucket->upsert($id, array('id' => $id, 'username' => $username, 'url' => $url, 'avatar_url' => $avatar_url, 'followers' => $followers, 'followings' => $followings)); }