forked from basho/riak-php-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimeSeries.php
183 lines (155 loc) · 5.51 KB
/
TimeSeries.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
require __DIR__ . '/../vendor/autoload.php';
use Basho\Riak;
use Basho\Riak\Command;
use Basho\Riak\Node;
$node = (new Node\Builder)
->atHost('riak-test')
->onPort(8087)
->build();
$riak = new Riak([$node], [], new Riak\Api\Pb());
# create table
$table_definition = "
CREATE TABLE %s (
family varchar not null,
series varchar not null,
time timestamp not null,
weather varchar not null,
temperature double,
PRIMARY KEY((family, series, quantum(time, 15, 'm')), family, series, time)
)";
$command = (new Command\Builder\TimeSeries\Query($riak))
->withQuery(sprintf($table_definition, "GeoCheckins"))
->build();
if (!$response->isSuccess()) {
echo $response->getMessage();
exit;
}
# describe table
$command = (new Command\Builder\TimeSeries\DescribeTable($riak))
->withTable('GeoCheckins')
->build();
if (!$response->isSuccess()) {
echo $response->getMessage();
exit;
}
foreach ($response->getResults() as $column_index => $column_definition) {
print_r([$column_index => $column_definition]);
}
# store a row
$response = (new Command\Builder\TimeSeries\StoreRows($riak))
->inTable('GeoCheckins')
->withRow([
(new Cell("family"))->setValue("family1"),
(new Cell("series"))->setValue("series1"),
(new Cell("time"))->setTimestampValue(1420113600),
(new Cell("weather"))->setValue("hot"),
(new Cell("temperature"))->setValue(23.5),
])
->build()
->execute();
if (!$response->isSuccess()) {
echo $response->getMessage();
exit;
}
# store rows
$response = (new Command\Builder\TimeSeries\StoreRows($riak))
->inTable('GeoCheckins')
->withRows([
[
(new Cell("family"))->setValue("family1"),
(new Cell("series"))->setValue("series1"),
(new Cell("time"))->setTimestampValue(1420115400),
(new Cell("weather"))->setValue("hot"),
(new Cell("temperature"))->setValue(22.4),
],
[
(new Cell("family"))->setValue("family1"),
(new Cell("series"))->setValue("series1"),
(new Cell("time"))->setTimestampValue(1420117200),
(new Cell("weather"))->setValue("warm"),
(new Cell("temperature"))->setValue(20.5),
],
])
->build()
->execute();
if (!$response->isSuccess()) {
echo $response->getMessage();
exit;
}
# fetch a row
/** @var Command\TimeSeries\Response $response */
$response = (new Command\Builder\TimeSeries\FetchRow($riak))
->atKey([
(new Cell("family"))->setValue("family1"),
(new Cell("series"))->setValue("series1"),
(new Cell("time"))->setTimestampValue(1420113600),
])
->inTable('GeoCheckins')
->build()
->execute();
if (!$response->isSuccess()) {
echo $response->getMessage();
exit;
}
# output row data
foreach ($response->getRow() as $index => $column) {
switch ($column->getType()) {
case Riak\TimeSeries\Cell::INT_TYPE:
printf("Column %d: %s is an integer equal to %d\n", $index, $column->getName(), $column->getValue());
break;
case Riak\TimeSeries\Cell::DOUBLE_TYPE:
printf("Column %d: %s is a double equal to %d\n", $index, $column->getName(), $column->getValue());
break;
case Riak\TimeSeries\Cell::BOOL_TYPE:
printf("Column %d: %s is a boolean equal to %s\n", $index, $column->getName(), $column->getValue());
break;
case Riak\TimeSeries\Cell::TIMESTAMP_TYPE:
printf("Column %d: %s is a timestamp equal to %d\n", $index, $column->getName(), $column->getValue());
break;
default:
printf("Column %d: %s is a string equal to %s\n", $index, $column->getName(), $column->getValue());
break;
}
}
# query for data
$response = (new Command\Builder\TimeSeries\Query($riak))
->withQuery("select * from GeoCheckins where family = 'family1' and series = 'myseries1' and (time > 1420113500 and time < 1420116000)")
->build()
->execute();
# output rows
foreach ($response->getResults() as $row_index => $row) {
foreach ($row as $column_index => $column) {
switch ($column->getType()) {
case Riak\TimeSeries\Cell::INT_TYPE:
printf("Column %d: %s is an integer equal to %d\n", $index, $column->getName(), $column->getValue());
break;
case Riak\TimeSeries\Cell::DOUBLE_TYPE:
printf("Column %d: %s is a double equal to %d\n", $index, $column->getName(), $column->getValue());
break;
case Riak\TimeSeries\Cell::BOOL_TYPE:
printf("Column %d: %s is a boolean equal to %s\n", $index, $column->getName(), $column->getValue());
break;
case Riak\TimeSeries\Cell::TIMESTAMP_TYPE:
printf("Column %d: %s is a timestamp equal to %d\n", $index, $column->getName(), $column->getValue());
break;
default:
printf("Column %d: %s is a string equal to %s\n", $index, $column->getName(), $column->getValue());
break;
}
}
}
# delete a row
$response = (new Command\Builder\TimeSeries\DeleteRow($riak))
->atKey([
(new Cell("family"))->setValue("family1"),
(new Cell("series"))->setValue("series1"),
(new Cell("time"))->setTimestampValue(1420113600),
])
->inTable('GeoCheckins')
->build()
->execute();
if (!$response->isSuccess()) {
echo $response->getMessage();
exit;
}