Created
December 29, 2012 19:48
-
-
Save anonymous/4408944 to your computer and use it in GitHub Desktop.
Python Riak Bucket Creation with Protobufs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import riak, riak_pb # riak_pb needs to be imported separately, but as you'll see below, it tends just to work | |
import json | |
import time | |
import random | |
# Sets for quick creation of random items | |
tool_ids = ('a845307', 'b1095304', 'c10309440', 'd15406786', 'e17693392', 'f1107541', 'g10737653', 'h15084673', 'i17693358') | |
super_depts = ('alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta', 'iota', 'kappa') | |
depts = ('alef', 'bet', 'gimel', 'dalet', 'he', 'vav', 'zayin', 'het', 'tet', 'yod') | |
cats = ('ay', 'bee', 'cee', 'dee', 'ee', 'ef', 'gee', 'aitch', 'ai') | |
subcats = ('a', 'be', 'ce', 'de', 'e', 'efe', 'ge', 'hache', 'i') | |
brand_names = ('eins', 'zwei', 'drei', 'vier', 'funf', 'sechs', 'sieben', 'acht', 'neun', 'zehn') | |
start_dates = ('uno', 'dos', 'tres', 'cuatro', 'cinco', 'seis', 'siete', 'ocho', 'nueve', 'diez') | |
display_status_descs = ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten') | |
avail_codes = ("a0", "b1") | |
class Item: | |
def __init__(self): | |
self.tool_id = random.choice(tool_ids) | |
self.dept = random.choice(depts) | |
self.cat = random.choice(cats) | |
self.subcat = random.choice(subcats) | |
self.brand_name = random.choice(tool_ids) | |
self.start_date = random.choice(start_dates) | |
self.display_status_desc = random.choice(display_status_descs) | |
self.avail_code = random.choice(avail_codes) | |
def to_dict(self): | |
return self.__dict__ | |
def record(item, bucket): | |
# str(records_saved) makes sure that we save a unique item each time | |
riak_obj = bucket.new(item["tool_id"] + str(records_saved), data = item) # bucket.new(key, value) | |
riak_obj.store() | |
if __name__ == "__main__": | |
client = riak.RiakClient(port=8087, transport_class=riak.RiakPbcTransport) # using protobuf | |
iteration_counts = [999, 1998, 3996, 7992, 15984, 31968, 63936, 127827, 255744] | |
bucket_incrementor = 0 | |
records_saved = 0 | |
with open("outfile.csv", "w") as f: | |
for total_iterations in iteration_counts: | |
start = time.time() | |
print "Expecting: " + str(total_iterations) | |
for x in xrange(0, total_iterations): | |
item = Item() | |
record(item.to_dict(), client.bucket("protobuf_test" + str(bucket_incrementor))) | |
records_saved += 1 | |
end = time.time() | |
print "Records saved: " + str(records_saved) + " in " + str(end-start) + " seconds." | |
f.write("\"{0}\", \"{1}\"\n".format(str(records_saved), str(end - start))) | |
records_saved = 0 | |
bucket_incrementor += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment