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