Skip to content

Commit 2c05f7d

Browse files
committed
readme
1 parent 81a9ddb commit 2c05f7d

File tree

2 files changed

+77
-286
lines changed

2 files changed

+77
-286
lines changed

README.md

+77-31
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Package was written as client for [Clickhouse](https://clickhouse.yandex/).
55

66
Client uses [Guzzle](https://github.com/guzzle/guzzle) for sending Http requests to Clickhouse servers.
7-
7+
88
## Requirements
99
`php7.1`
1010

@@ -16,21 +16,23 @@ Composer
1616
composer require the-tinderbox/clickhouse-php-client
1717
```
1818

19-
# Using
19+
# Usage
2020

2121
Client works with alone server and cluster. Also, client can make async select and insert (from local files) queries.
2222

2323
## Alone server
2424

2525
```php
2626
$server = new Tinderbox\Clickhouse\Server('127.0.0.1', '8123', 'default', 'user', 'pass');
27-
$client = new Tinderbox\Clickhouse\Client($server);
27+
$serverProvider = (new Tinderbox\Clickhouse\ServerProvider())->addServer($server);
28+
29+
$client = new Tinderbox\Clickhouse\Client($serverProvider);
2830
```
2931

3032
## Cluster
3133

3234
```php
33-
$cluster = new Tinderbox\Clickhouse\Cluster([
35+
$testCluster = new Tinderbox\Clickhouse\Cluster('cluster-name', [
3436
'server-1' => [
3537
'host' => '127.0.0.1',
3638
'port' => '8123',
@@ -40,11 +42,31 @@ $cluster = new Tinderbox\Clickhouse\Cluster([
4042
],
4143
'server-2' => new Tinderbox\Clickhouse\Server('127.0.0.1', '8124', 'default', 'user', 'pass')
4244
]);
43-
$client = new Tinderbox\Clickhouse\Client($cluster);
45+
46+
$anotherCluster = new Tinderbox\Clickhouse\Cluster('cluster-name', [
47+
[
48+
'host' => '127.0.0.1',
49+
'port' => '8125',
50+
'database' => 'default',
51+
'user' => 'user',
52+
'password' => 'pass'
53+
],
54+
new Tinderbox\Clickhouse\Server('127.0.0.1', '8126', 'default', 'user', 'pass')
55+
]);
56+
57+
$serverProvider = (new Tinderbox\Clickhouse\ServerProvider())->addCluster($testCluster)->addCluster($anotherCluster);
58+
59+
$client = (new Tinderbox\Clickhouse\Client($serverProvider));
60+
```
61+
62+
Before execute any query on cluster, you should provide cluster name and client will run all queries on specified cluster.
63+
64+
```
65+
$client->onCluster('test-cluster');
4466
```
4567

46-
By default client will use first server in given list. If you want to perform request on another server you should use
47-
`using($hostname)` method on client and then run query;
68+
By default client will use random server in given list of servers or in specified cluster. If you want to perform request on specified server you should use
69+
`using($hostname)` method on client and then run query. Client will remember hostname for next queries:
4870

4971
```php
5072
$client->using('server-2')->select('select * from table');
@@ -91,7 +113,7 @@ $client->select('select * from table where column = :column', [
91113

92114
## Select queries
93115

94-
Any SELECT query will return instance of `Result`. This class implements interfaces `\ArrayAccess`, `\Countable` и `\Iterator`,
116+
Any SELECT query will return instance of `Result`. This class implements interfaces `\ArrayAccess`, `\Countable` и `\Iterator`,
95117
which means that it can be used as an array.
96118

97119
Array with result rows can be obtained via `rows` property
@@ -104,8 +126,9 @@ $rows = $result->getRows();
104126
Also you can get some statistic of your query execution:
105127

106128
1. Number of read rows
107-
2. Number of read bytes
129+
2. Number of read bytes
108130
3. Time of query execution
131+
4. Rows before limit at least
109132

110133
Statistic can be obtained via `statistic` property
111134

@@ -121,12 +144,15 @@ echo $statistic->getBytes();
121144

122145
echo $statistic->time;
123146
echo $statistic->getTime();
147+
148+
echo $statistic->rowsBeforeLimitAtLeast;
149+
echo $statistic->getRowsBeforeLimitAtLeast();
124150
```
125151

126152
### Sync
127153

128154
```php
129-
$result = $client->select('select number from system.numbers limit 100');
155+
$result = $client->readOne('select number from system.numbers limit 100');
130156

131157
foreach ($result as $number) {
132158
echo $number['number'].PHP_EOL;
@@ -135,7 +161,7 @@ foreach ($result as $number) {
135161

136162
**Using local files**
137163

138-
You can use local files as temporary tables in Clickhouse. You should pass as second argument array of `TempTable` instances or single `TempTable`
164+
You can use local files as temporary tables in Clickhouse. You should pass as third argument array of `TempTable` instances.
139165
instance.
140166

141167
In this case will be sent one file to the server from which Clickhouse will extract data to temporary table.
@@ -152,7 +178,7 @@ If you pass such an array as a structure:
152178
Then each column from file wil be named as _1, _2, _3.
153179

154180
```php
155-
$result = $client->select('select number from system.numbers where number in _numbers limit 100', new TempTable('_numbers', 'numbers.csv', [
181+
$result = $client->readOne('select number from system.numbers where number in _numbers limit 100', new TempTable('_numbers', 'numbers.csv', [
156182
'number' => 'UInt64'
157183
]));
158184

@@ -161,35 +187,37 @@ foreach ($result as $number) {
161187
}
162188
```
163189

190+
You can provide path to file or pass `FileInterface` instance as second argument.
191+
164192
### Async
165193

166-
Unlike the `select` method, which returns` Result`, the `selectAsync` method returns an array of` Result` for each executed query.
194+
Unlike the `readOne` method, which returns` Result`, the `read` method returns an array of` Result` for each executed query.
167195

168196
```php
169197

170-
list($clicks, $visits, $views) = $client->selectAsync([
171-
['select * from clicks where date = ?', ['2017-01-01']],
172-
['select * from visits where date = ?', ['2017-01-01']],
173-
['select * from views where date = ?', ['2017-01-01']],
198+
list($clicks, $visits, $views) = $client->read([
199+
['query' => 'select * from clicks where date = ?', 'bindings' => ['2017-01-01']],
200+
['query' => 'select * from visits where date = ?', 'bindings' => ['2017-01-01']],
201+
['query' => 'select * from views where date = ?', 'bindings' => ['2017-01-01']],
174202
]);
175203

176204
foreach ($clicks as $click) {
177205
echo $click['date'].PHP_EOL;
178206
}
179207

180208
```
181-
**In `selectAsync` method, you can pass the parameter `$concurrency` which is responsible for the maximum simultaneous number of requests.**
209+
**In `read` method, you can pass the parameter `$concurrency` which is responsible for the maximum simultaneous number of requests.**
182210

183211
**Using local files**
184212

185213
As with synchronous select request you can pass files to the server:
186214

187215
```php
188216

189-
list($clicks, $visits, $views) = $client->selectAsync([
190-
['select * from clicks where date = ? and userId in _users', ['2017-01-01'], new TempTable('_users', 'users.csv', ['number' => 'UInt64'])],
191-
['select * from visits where date = ?', ['2017-01-01']],
192-
['select * from views where date = ?', ['2017-01-01']],
217+
list($clicks, $visits, $views) = $client->read([
218+
['query' => 'select * from clicks where date = ? and userId in _users', 'bindings' => ['2017-01-01'], new TempTable('_users', 'users.csv', ['number' => 'UInt64'])],
219+
['query' => 'select * from visits where date = ?', 'bindings' => ['2017-01-01']],
220+
['query' => 'select * from views where date = ?', 'bindings' => ['2017-01-01']],
193221
]);
194222

195223
foreach ($clicks as $click) {
@@ -207,29 +235,47 @@ Insert queries always returns true or throws exceptions in case of error.
207235
Data can be written row by row or from local CSV or TSV files.
208236

209237
```php
210-
$client->insert('insert into table (date, column) values (?,?), (?,?)', ['2017-01-01', 1, '2017-01-02', 2]);
238+
$client->writeOne('insert into table (date, column) values (?,?), (?,?)', ['2017-01-01', 1, '2017-01-02', 2]);
239+
$client->write([
240+
['query' => 'insert into table (date, column) values (?,?), (?,?)', 'bindings' => ['2017-01-01', 1, '2017-01-02', 2]],
241+
['query' => 'insert into table (date, column) values (?,?), (?,?)', 'bindings' => ['2017-01-01', 1, '2017-01-02', 2]],
242+
['query' => 'insert into table (date, column) values (?,?), (?,?)', 'bindings' => ['2017-01-01', 1, '2017-01-02', 2]]
243+
]);
211244

212-
$client->insertFiles('table', ['date', 'column'], [
213-
'/file-1.csv',
214-
'/file-2.csv'
245+
$client->writeFiles('table', ['date', 'column'], [
246+
new Tinderbox\Clickhouse\Common\File('/file-1.csv'),
247+
new Tinderbox\Clickhouse\Common\File('/file-2.csv')
215248
]);
216249

217250
$client->insertFiles('table', ['date', 'column'], [
218-
'/file-1.tsv',
219-
'/file-2.tsv'
251+
new Tinderbox\Clickhouse\Common\File('/file-1.tsv'),
252+
new Tinderbox\Clickhouse\Common\File('/file-2.tsv')
220253
], Tinderbox\Clickhouse\Common\Format::TSV);
221254
```
222255

223-
In case of `insertFiles` queries exetues asynchronously
256+
In case of `writeFiles` queries executes asynchronously. If you have butch of files and you want to insert them in one insert query, you can
257+
use our `ccat` utility and `MergedFiles` instance instead of `File`. You should put list of files to insert into
258+
one file:
259+
260+
```
261+
file-1.tsv
262+
file-2.tsv
263+
```
264+
265+
### Building ccat
266+
267+
`ccat` sources placed into `utils/ccat` directory. Just run `make && make install` to build and install library into
268+
`bin` directory of package. There are already compiled binary of `ccat` in `bin` directory, but it
269+
may not work on some systems.
224270

225-
**In `insertFiles` method, you can pass the parameter `$concurrency` which is responsible for the maximum simultaneous number of requests.**
271+
**In `writeFiles` method, you can pass the parameter `$concurrency` which is responsible for the maximum simultaneous number of requests.**
226272

227273
## Other queries
228274

229275
In addition to SELECT and INSERT queries, you can execute other queries :) There is `statement` method for this purposes.
230276

231277
```php
232-
$client->statement('DROP TABLE table');
278+
$client->writeOne('DROP TABLE table');
233279
```
234280

235281
## Testing

0 commit comments

Comments
 (0)