Create a gist now

Instantly share code, notes, and snippets.

Perform secondary index (2i) searches on Twitter hashtags stored in Riak and count all hashtags in the matching tweets.
import riak
Riak = riak.RiakClient(pb_port=10017, protocol='pbc')
TweetsBucket = Riak.bucket('tweets')
results = TweetsBucket.get_index("hashtags_bin", "android",
"androie", return_terms=False)
other_tags = {}
# With return_terms=False, each result will be simply a Riak
# key. Retrieve each object, iterate over and tally the associated
# index values
for riak_key in results.results:
riak_object = TweetsBucket.get(riak_key)
for index in riak_object.indexes:
if index[0] == 'hashtags_bin':
hashtag = index[1]
if hashtag in other_tags:
other_tags[hashtag] += 1
other_tags[hashtag] = 1
all_tags = other_tags.keys()
for tag in all_tags:
print '{0: >5}'.format(str(other_tags[tag])) + " " + tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment