Created
August 21, 2013 16:07
-
-
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.
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
#!/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