Skip to content

Instantly share code, notes, and snippets.

@macintux
Created August 21, 2013 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macintux/6296480 to your computer and use it in GitHub Desktop.
Save macintux/6296480 to your computer and use it in GitHub Desktop.
Perform secondary index (2i) searches on Twitter hashtags stored in Riak and count all hashtags in the matching tweets.
#!/usr/local/bin/python
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
else:
other_tags[hashtag] = 1
all_tags = other_tags.keys()
all_tags.sort()
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