Created
April 22, 2011 12:18
-
-
Save lanius/936547 to your computer and use it in GitHub Desktop.
Riak's "Links and Link Walking" for Python.
This file contains hidden or 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/bin/env python | |
# -*- coding: utf-8 -*- | |
from riak import RiakClient | |
from riak.mapreduce import RiakLink | |
# see http://wiki.basho.com/Links-and-Link-Walking.html | |
HOST = 'example.jp' | |
def print_links(links): | |
for link in links: | |
print('key: {0}, tag: {1}'.format(link.get_key(), link.get_tag())) | |
def main(): | |
client = RiakClient(host=HOST) | |
bucket = client.bucket('people') | |
# Working with Links | |
tim = bucket.new('timoreilly', data='I am an excellent public speaker.') | |
tim.add_link(RiakLink('people', 'dhh', tag='friend')).store() | |
links = bucket.get('timoreilly').get_links() | |
print_links(links) | |
# key: dhh, tag: dhh | |
bucket.new('dhh', data='I drive a Zonda.').store() | |
# Link Walking | |
links = bucket.get('timoreilly').link('people', 'friend', 1).run() | |
print_links(links) | |
# key: dhh, tag: dhh | |
links = bucket.get('timoreilly').link('_', 'friend', 1).run() | |
print_links(links) | |
# key: dhh, tag: dhh | |
davethomas = bucket.new('davethomas', data='I publish books') | |
davethomas.add_link(RiakLink('people', 'timoreilly', tag='friend')).store() | |
links = bucket.get('davethomas').link('_', 'friend', False).\ | |
link('_', 'friend', False).run() | |
# it is required to use True/False in method chain. not 1/0. | |
print_links(links) | |
# key: dhh, tag: dhh | |
links = bucket.get('davethomas').link('_', 'friend', True).\ | |
link('_', 'friend', False).run() | |
print_links(links) | |
# key: timoreilly, tag: friend | |
dhh = bucket.get('dhh') | |
dhh.add_link(RiakLink('people', 'davethomas', tag='friend')).store() | |
links = bucket.get('davethomas').link('_', 'friend', False).\ | |
link('_', 'friend', False).\ | |
link('_', 'friend', False).run() | |
print_links(links) | |
# key: davethomas, tag: friend | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment