|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import print_function |
| 3 | +import argparse |
| 4 | +import aerospike |
| 5 | +from aerospike import exception as e |
| 6 | +try: |
| 7 | + from aerospike_helpers.operations import list_operations as lh |
| 8 | +except: |
| 9 | + pass # Needs Aerospike client >= 3.4.0 |
| 10 | +from aerospike import predexp as pxp |
| 11 | +import datetime |
| 12 | +from datetime import timedelta |
| 13 | +import pprint |
| 14 | +import sys |
| 15 | + |
| 16 | +argparser = argparse.ArgumentParser(add_help=False) |
| 17 | +argparser.add_argument( |
| 18 | + "--help", dest="help", action="store_true", help="Displays this message." |
| 19 | +) |
| 20 | +argparser.add_argument( |
| 21 | + "-U", |
| 22 | + "--username", |
| 23 | + dest="username", |
| 24 | + metavar="<USERNAME>", |
| 25 | + help="Username to connect to database.", |
| 26 | +) |
| 27 | +argparser.add_argument( |
| 28 | + "-P", |
| 29 | + "--password", |
| 30 | + dest="password", |
| 31 | + metavar="<PASSWORD>", |
| 32 | + help="Password to connect to database.", |
| 33 | +) |
| 34 | +argparser.add_argument( |
| 35 | + "-h", |
| 36 | + "--host", |
| 37 | + dest="host", |
| 38 | + default="127.0.0.1", |
| 39 | + metavar="<ADDRESS>", |
| 40 | + help="Address of Aerospike server.", |
| 41 | +) |
| 42 | +argparser.add_argument( |
| 43 | + "-p", |
| 44 | + "--port", |
| 45 | + dest="port", |
| 46 | + type=int, |
| 47 | + default=3000, |
| 48 | + metavar="<PORT>", |
| 49 | + help="Port of the Aerospike server.", |
| 50 | +) |
| 51 | +argparser.add_argument( |
| 52 | + "-n", |
| 53 | + "--namespace", |
| 54 | + dest="namespace", |
| 55 | + default="test", |
| 56 | + metavar="<NS>", |
| 57 | + help="Port of the Aerospike server.", |
| 58 | +) |
| 59 | +argparser.add_argument( |
| 60 | + "-s", |
| 61 | + "--set", |
| 62 | + dest="set", |
| 63 | + default="sensor_data", |
| 64 | + metavar="<SET>", |
| 65 | + help="Port of the Aerospike server.", |
| 66 | +) |
| 67 | +argparser.add_argument( |
| 68 | + "--sensor", |
| 69 | + dest="sensor_id", |
| 70 | + type=int, |
| 71 | + default=1, |
| 72 | + metavar="<SENSOR-ID>", |
| 73 | + help="Sensor ID", |
| 74 | +) |
| 75 | +argparser.add_argument( |
| 76 | + "-i", |
| 77 | + "--interactive", |
| 78 | + dest="interactive", |
| 79 | + action="store_true", |
| 80 | + help="Interactive Mode", |
| 81 | +) |
| 82 | +options = argparser.parse_args() |
| 83 | +if options.help: |
| 84 | + argparser.print_help() |
| 85 | + print() |
| 86 | + sys.exit(1) |
| 87 | + |
| 88 | + |
| 89 | +def version_tuple(version): |
| 90 | + return tuple(int(i) for i in version.split(".")) |
| 91 | + |
| 92 | + |
| 93 | +def pause(): |
| 94 | + input("Hit return to continue") |
| 95 | + |
| 96 | + |
| 97 | +def print_sensor_data(rec, pp): |
| 98 | + k, _, b = rec |
| 99 | + print(k[2]) |
| 100 | + print(b['t']) |
| 101 | + print("=" * 30) |
| 102 | + |
| 103 | + |
| 104 | +if options.namespace and options.namespace != "None": |
| 105 | + namespace = options.namespace |
| 106 | +else: |
| 107 | + namespace = None |
| 108 | +set = options.set if options.set and options.set != "None" else None |
| 109 | + |
| 110 | +config = {"hosts": [(options.host, options.port)]} |
| 111 | +try: |
| 112 | + client = aerospike.client(config).connect(options.username, options.password) |
| 113 | + policy = {"key": aerospike.POLICY_KEY_SEND} |
| 114 | +except e.ClientError as e: |
| 115 | + if not options.quiet: |
| 116 | + print("Error: {0} [{1}]".format(e.msg, e.code)) |
| 117 | + sys.exit(2) |
| 118 | + |
| 119 | +version = client.info_all("version") |
| 120 | +release = list(version.values())[0][1].split(" ")[-1] |
| 121 | +if version_tuple(aerospike.__version__) < version_tuple("3.4.0") or version_tuple( |
| 122 | + release |
| 123 | +) < version_tuple("4.0"): |
| 124 | + print( |
| 125 | + "\nPlease use Python client >= 3.4.0, ", |
| 126 | + "Aerospike database >= 4.0 for this example.", |
| 127 | + ) |
| 128 | + sys.exit(3) |
| 129 | + |
| 130 | +pp = pprint.PrettyPrinter(indent=2) |
| 131 | +sensor_id = options.sensor_id |
| 132 | +spacer = "=" * 30 |
| 133 | +minute = 0 |
| 134 | +key = (namespace, set, "sensor{}-12-31".format(sensor_id)) |
| 135 | +print("\nRetrieve sensor{} data for 8-11am, December 31st".format(sensor_id)) |
| 136 | +if options.interactive: |
| 137 | + pause() |
| 138 | +starts = 8 * 60 |
| 139 | +ends = 11 * 60 |
| 140 | +try: |
| 141 | + ops = [ |
| 142 | + lh.list_get_by_value_range( |
| 143 | + "t", |
| 144 | + aerospike.LIST_RETURN_VALUE, |
| 145 | + [starts, aerospike.null()], |
| 146 | + [ends, aerospike.null()], |
| 147 | + ) |
| 148 | + ] |
| 149 | + _, _, b = client.operate(key, ops) |
| 150 | + pp.pprint(b["t"]) |
| 151 | + print(spacer) |
| 152 | + |
| 153 | + print("\nGet sensor{} data for April 2nd".format(sensor_id)) |
| 154 | + key = (namespace, set, "sensor{}-04-02".format(sensor_id)) |
| 155 | + if options.interactive: |
| 156 | + pause() |
| 157 | + _, _, b = client.get(key) |
| 158 | + pp.pprint(b["t"]) |
| 159 | + print(spacer) |
| 160 | + |
| 161 | + print("\nGet a year's data for sensor{}".format(sensor_id)) |
| 162 | + if options.interactive: |
| 163 | + pause() |
| 164 | + dt = datetime.datetime(2018, 1, 1, 0, 0, 0) |
| 165 | + keys = [] |
| 166 | + for i in range(1, 366): |
| 167 | + keys.append((namespace, set,"sensor{}-{:02d}-{:02d}".format(sensor_id, dt.month, dt.day))) |
| 168 | + dt = dt + timedelta(days=1) |
| 169 | + sensor_year = client.get_many(keys) |
| 170 | + for rec in sensor_year: |
| 171 | + k, _, b = rec |
| 172 | + print(k[2]) |
| 173 | + pp.pprint(b["t"]) |
| 174 | + print(spacer) |
| 175 | + |
| 176 | + print("\nGet the data from all sensors for June 19") |
| 177 | + if options.interactive: |
| 178 | + pause() |
| 179 | + dt = datetime.datetime(2018, 1, 1, 0, 0, 0) |
| 180 | + keys = [] |
| 181 | + for i in range(1, 1001): |
| 182 | + keys.append((namespace, set,"sensor{}-06-19".format(i))) |
| 183 | + one_day_all_sensors = client.get_many(keys) |
| 184 | + for rec in one_day_all_sensors: |
| 185 | + k, _, b = rec |
| 186 | + print(k[2]) |
| 187 | + pp.pprint(b["t"]) |
| 188 | + print(spacer) |
| 189 | + |
| 190 | + print("\nScan for a random sampling (about 0.25%) of all the sensor data") |
| 191 | + if options.interactive: |
| 192 | + pause() |
| 193 | + predexp = [ |
| 194 | + pxp.rec_digest_modulo(365), |
| 195 | + pxp.integer_value(1), |
| 196 | + pxp.integer_equal() |
| 197 | + ] |
| 198 | + query = client.query(namespace, set) |
| 199 | + query.predexp(predexp) |
| 200 | + query.foreach(print_sensor_data) |
| 201 | + |
| 202 | +except e.RecordError as e: |
| 203 | + print("Error: {0} [{1}]".format(e.msg, e.code)) |
| 204 | +client.close() |
0 commit comments