Skip to content

Commit 9da2ede

Browse files
author
Bulat Shakirzyanov
committed
fix formatting and add more content
1 parent 7f19733 commit 9da2ede

File tree

1 file changed

+106
-23
lines changed

1 file changed

+106
-23
lines changed

features/README.md

Lines changed: 106 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,76 @@ $session = $cluster->connect();
1818

1919
### Discovering nodes in the cluster
2020

21-
After the initial connection to one of the hosts specified via `withContactPoints` succeeded, the driver discovers the addresses and connects to all members of the cluster automatically. You can also see the nodes that the driver discovered by running `SELECT * FROM system.peers`.
21+
After the initial connection to one of the hosts specified via `withContactPoints()` succeeded, the driver discovers the addresses and connects to all members of the cluster automatically. You can also see the nodes that the driver discovered by running `SELECT * FROM system.peers`.
22+
23+
### Configuring load balancing policy
24+
25+
PHP Driver comes with a variety of load balancing policies. By default it uses a combination of token aware data center round robin.
26+
27+
The token aware load balancing policy uses the same hashing algorithms as the Apache Cassandra to route prepared statements execution directly to replica nodes, avoiding an additional network hop to/from the coordinator. You can toggle its usage with [`Cassandra\Cluster\Builder::withTokenAwareRouting()`](http://datastax.github.io/php-driver/api/class/Cassandra/Cluster/Builder/#with-token-aware-routing).
28+
29+
```php
30+
<?php
31+
32+
$cluster = Cassandra::cluster()
33+
->withTokenAwareRouting(false)
34+
->build();
35+
$session = $cluster->connect();
36+
```
37+
38+
The default datacenter aware round robin load balancing policy is configured to keep all traffic in the same datacenter. Upon connecting to a host from the initial list of contact points, the driver will consider that host's datacenter to be local. Only hosts from the same datacenter will be connected to and used for executing statements. You can override the name of the local datacenter. The number of hosts from remote datacenters that the driver may use and whether it should execute statements with local consistencies on those hosts in case none of the local hosts are available. All of that is configurable via [`Cassandra\Cluster\Builder::withDatacenterAwareRoundRobinLoadBalancingPolicy()`](http://datastax.github.io/php-driver/api/class/Cassandra/Cluster/Builder/#with-datacenter-aware-round-robin-load-balancing-policy).
39+
40+
```php
41+
<?php
42+
43+
$cluster = Cassandra::cluster()
44+
->withDatacenterAwareRoundRobinLoadBalancingPolicy("us-west", 2, true)
45+
->build();
46+
$session = $cluster->connect();
47+
```
48+
49+
Finally, you may disable datacenter awareness by calling [`Cassandra\Cluster\Builder::withRoundRobinLoadBalancingPolicy()`](http://datastax.github.io/php-driver/api/class/Cassandra/Cluster/Builder/#with-round-robin-load-balancing-policy).
50+
51+
```php
52+
<?php
53+
54+
$cluster = Cassandra::cluster()
55+
->withRoundRobinLoadBalancingPolicy()
56+
->build();
57+
$session = $cluster->connect();
58+
```
59+
60+
### Authenticating via `PasswordAuthenticator`
61+
62+
The PHP Driver supports Apache Cassandra's built-in password authentication mechanism. To enable it, use [`Cassandra\Cluster\Builder::withCredentials()`](http://datastax.github.io/php-driver/api/class/Cassandra/Cluster/Builder/#with-credentials).
63+
64+
```php
65+
<?php
66+
67+
$cluster = Cassandra::cluster()
68+
->withCredentials("username", "password")
69+
->build();
70+
$session = $cluster->connect();
71+
```
72+
73+
### Enabling SSL encryption
74+
75+
The PHP Driver supports SSL encryption of network connections. You must configure [`Cassandra\SSLOptions`](http://datastax.github.io/php-driver/api/class/Cassandra/SSLOptions/) using the [`Cassandra\SSLOptions\Builder`](http://datastax.github.io/php-driver/api/class/Cassandra/SSLOptions/Builder/).
76+
77+
```php
78+
<?php
79+
80+
$ssl = Cassandra::ssl()
81+
->withTrustedCerts('node1.pem', 'node2.pem')
82+
->withVerifyFlags(Cassandra::VERIFY_PEER_CERT | Cassandra::VERIFY_PEER_IDENTITY)
83+
->withClientCert('client.pem')
84+
->withPrivateKey('id_rsa', 'passphrase')
85+
->build()
86+
$cluster = Cassandra::cluster()
87+
->withSSL($ssl)
88+
->build();
89+
$session = $cluster->connect();
90+
```
2291

2392
### Executing queries
2493

@@ -30,7 +99,7 @@ You run CQL statements by passing them to [`Cassandra\Session::execute()`](http:
3099
$result = $session->execute(new Cassandra\SimpleStatement('SELECT keyspace_name, columnfamily_name FROM system.schema_columnfamilies'));
31100

32101
foreach ($result as $row) {
33-
sprintf("The keyspace \"%s\" has a table \"%s\".\n", $row['keyspace_name'], $row['columnfamily_name']);
102+
sprintf("The keyspace \"%s\" has a table \"%s\".\n", $row['keyspace_name'], $row['columnfamily_name']);
34103
}
35104
```
36105

@@ -42,10 +111,10 @@ foreach ($result as $row) {
42111
<?php
43112

44113
$session->execute(
45-
new Cassandra\SimpleStatement("UPDATE users SET age = ? WHERE user_name = ?"),
46-
new Cassandra\ExecutionOptions(array(
47-
'arguments' => array(41, 'Sam')
48-
))
114+
new Cassandra\SimpleStatement("UPDATE users SET age = ? WHERE user_name = ?"),
115+
new Cassandra\ExecutionOptions(array(
116+
'arguments' => array(41, 'Sam')
117+
))
49118
);
50119
```
51120

@@ -61,7 +130,7 @@ The driver supports prepared statements. Use [`Cassandra\Session::prepare()`](ht
61130
$statement = $session->prepare('INSERT INTO users (username, email) VALUES (?, ?)');
62131

63132
$session->execute($statement, new Cassandra\ExecutionOptions(array(
64-
'arguments' => array('avalanche123', '[email protected]')
133+
'arguments' => array('avalanche123', '[email protected]')
65134
)));
66135
```
67136

@@ -75,8 +144,8 @@ With fully asynchronous api, it is very easy to run queries in parallel:
75144
<?php
76145

77146
$data = array(
78-
array(41, 'Sam'),
79-
array(35, 'Bob')
147+
array(41, 'Sam'),
148+
array(35, 'Bob')
80149
);
81150

82151
$statement = $session->prepare("UPDATE users SET age = ? WHERE user_name = ?");
@@ -85,15 +154,15 @@ $timeout = 5;
85154

86155
// execute all statements in background
87156
foreach ($data as $arguments) {
88-
$futures[]= $session->executeAsync($statement, new ExecutionOptions(
89-
'arguments' => $arguments
90-
));
157+
$futures[]= $session->executeAsync($statement, new ExecutionOptions(
158+
'arguments' => $arguments
159+
));
91160
}
92161

93162
// wait for all statements to complete
94163
foreach ($futures as $future) {
95-
// we will not wait for each result for more than 5 seconds
96-
$future->get(5);
164+
// we will not wait for each result for more than 5 seconds
165+
$future->get(5);
97166
}
98167
```
99168

@@ -171,7 +240,20 @@ Cassandra 1.2 also supported batching, but only as a CQL feature, you had to bui
171240

172241
### Result paging
173242

174-
If you're using Cassandra 2.0 or later you can page your query results by adding the `page_size` option to [`Cassandra\ExecutionOptions`](http://datastax.github.io/php-driver/api/class/Cassandra/ExecutionOptions/):
243+
**If you're using Cassandra 2.0** or later you can page your query results.
244+
245+
By default, a page size of 10000 will be used, you can override the default page size via [`Cassandra\Cluster\Builder::withDefaultPageSize()`](http://datastax.github.io/php-driver/api/class/Cassandra/Cluster/Builder/#with-default-page-size).
246+
247+
```php
248+
<?php
249+
250+
$cluster = Cassandra::cluster()
251+
->withDefaultPageSize(200)
252+
->build();
253+
$session = $cluster->connect();
254+
```
255+
256+
You can also override the page size on a per-execute basis by adding the `page_size` option to [`Cassandra\ExecutionOptions`](http://datastax.github.io/php-driver/api/class/Cassandra/ExecutionOptions/):
175257

176258
```php
177259
<?php
@@ -180,10 +262,10 @@ $statement = new Cassandra\SimpleStatement("SELECT * FROM large_table WHERE id =
180262
$result = $session->execute($statement, new Cassandra\ExecutionOptions(array('page_size' => 100)));
181263

182264
while ($result) {
183-
foreach ($result as $row) {
184-
var_dump($row);
185-
}
186-
$result = $result->nextPage();
265+
foreach ($result as $row) {
266+
var_dump($row);
267+
}
268+
$result = $result->nextPage();
187269
}
188270
```
189271

@@ -210,24 +292,25 @@ Consistency can also be passed via `Cassandra\ExecutionOptions`.
210292
<?php
211293

212294
$session->execute(
213-
new Cassandra\SimpleStatement('SELECT * FROM users'),
214-
new Cassandra\ExecutionOptions(array('consistency' => Cassandra::CONSISTENCY_LOCAL_QUORUM))
295+
new Cassandra\SimpleStatement('SELECT * FROM users'),
296+
new Cassandra\ExecutionOptions(array('consistency' => Cassandra::CONSISTENCY_LOCAL_QUORUM))
215297
);
216298

217299
$statement = $session->prepare('SELECT * FROM users');
218300
$session->execute($statement, new Cassandra\ExecutionOptions(array(
219-
'consistency' => Cassandra::CONSISTENCY_LOCAL_QUORUM
301+
'consistency' => Cassandra::CONSISTENCY_LOCAL_QUORUM
220302
)));
221303

222304
$batch = new Cassandra\BatchStatement();
223305
$batch->add(new Cassandra\SimpleStatement("UPDATE users SET email = '[email protected]' WHERE id = 'sue'"));
224306
$batch->add(new Cassandra\SimpleStatement("UPDATE users SET email = '[email protected]' WHERE id = 'tom'"));
225307
$session->execute($batch, new Cassandra\ExecutionOptions(array(
226-
'consistency' => Cassandra::CONSISTENCY_LOCAL_QUORUM
308+
'consistency' => Cassandra::CONSISTENCY_LOCAL_QUORUM
227309
)));
228310
```
229311

230312
[Read more about `Cassandra\ExecutionOptions`](http://datastax.github.io/php-driver/api/class/Cassandra/ExecutionOptions/)
313+
231314
[Read more about `Cassandra\Session::execute()`](http://datastax.github.io/php-driver/api/interface/Cassandra/Session/#execute)
232315

233316
The default consistency level unless you've set it yourself is `Cassandra::CONSISTENCY_ONE`.

0 commit comments

Comments
 (0)