~ubuntu-branches/ubuntu/saucy/python-happybase/saucy

« back to all changes in this revision

Viewing changes to doc/introduction.rst

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2013-05-30 13:56:42 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130530135642-tveld2y1dbkhmuv3
Tags: 0.6-1
* New upstream release (Closes: #712971).
* Ran wrap-and-sort.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
************
2
 
Introduction
3
 
************
4
 
 
5
 
.. py:currentmodule:: happybase
6
 
 
7
 
**HappyBase** is a developer-friendly `Python <http://python.org/>`_ library to
8
 
interact with `Apache HBase <http://hbase.apache.org/>`_. HappyBase is designed
9
 
for use in standard HBase setups, and offers application developers a Pythonic
10
 
API to interact with HBase.
11
 
 
12
 
The example below illustrates basic usage of the library. The :doc:`tutorial
13
 
<tutorial>` contains many more examples.
14
 
 
15
 
::
16
 
 
17
 
   import happybase
18
 
 
19
 
   connection = happybase.Connection('hostname')
20
 
   table = connection.table('table-name')
21
 
 
22
 
   table.put('row-key', {'family:qual1': 'value1',
23
 
                         'family:qual2': 'value2'})
24
 
 
25
 
   row = table.row('row-key')
26
 
   print row['family:qual1']  # prints 'value1'
27
 
 
28
 
   for key, data in table.rows(['row-key-1', 'row-key-2']):
29
 
       print key, data  # prints row key and data for each row
30
 
 
31
 
   for key, data in table.scan(row_prefix='row'):
32
 
       print key, data  # prints 'value1' and 'value2'
33
 
 
34
 
   row = table.delete('row-key')
35
 
 
36
 
Below the surface, HappyBase uses the `Python Thrift library
37
 
<http://pypi.python.org/pypi/thrift>`_ to connect to HBase using its `Thrift
38
 
<http://thrift.apache.org/>`_ gateway, which is included in the standard HBase
39
 
0.9x releases. While this HBase Thrift API can be used directly from Python
40
 
using (automatically generated) HBase Thrift service classes, application code
41
 
doing so is very verbose, cumbersome to write, and hence error-prone. The
42
 
reason for this is that the HBase Thrift API is a flat, language-agnostic
43
 
interface API closely tied to the RPC going over the wire-level protocol. In
44
 
practice, this means that applications using Thrift directly need to deal with
45
 
many imports, sockets, transports, protocols, clients, Thrift types and
46
 
mutation objects. For instance, look at the code required to connect to HBase
47
 
and store two values::
48
 
 
49
 
   from thrift import Thrift
50
 
   from thrift.transport import TSocket, TTransport
51
 
   from thrift.protocol import TBinaryProtocol
52
 
 
53
 
   from hbase import ttypes
54
 
   from hbase.Hbase import Client, Mutation
55
 
 
56
 
   sock = TSocket.TSocket('hostname', 9090)
57
 
   transport = TTransport.TBufferedTransport(sock)
58
 
   protocol = TBinaryProtocol.TBinaryProtocol(transport)
59
 
   client = Client(protocol)
60
 
   transport.open()
61
 
 
62
 
   mutations = [Mutation(column='family:qual1', value='value1'),
63
 
                Mutation(column='family:qual2', value='value2')]
64
 
   client.mutateRow('table-name', 'row-key', mutations)
65
 
 
66
 
:pep:`20` taught us that simple is better than complex, and as you can see,
67
 
Thrift is certainly complex. HappyBase hides all the Thrift cruft below a
68
 
friendly API. The resulting application code will be cleaner, more productive
69
 
to write, and more maintainable. With HappyBase, the example above can be
70
 
simplified to this::
71
 
 
72
 
   import happybase
73
 
 
74
 
   connection = happybase.Connection('hostname')
75
 
   table = connection.table('table-name')
76
 
   table.put('row-key', {'family:qual1': 'value1',
77
 
                         'family:qual2': 'value2'})
78
 
 
79
 
If you're not convinced and still think the Thrift API is not that bad, please
80
 
try to accomplish some other common tasks, e.g. retrieving rows and scanning
81
 
over a part of a table, and compare that to the HappyBase equivalents. If
82
 
you're still not convinced by then, we're sorry to inform you that HappyBase is
83
 
not the project for you, and we wish you all of luck maintaining your code ‒ or
84
 
is it just Thrift boilerplate?
85
 
 
86
 
 
87
 
.. rubric:: Next steps
88
 
 
89
 
Follow the :doc:`installation guide <installation>` and continue with the
90
 
:doc:`tutorial <tutorial>`.
91
 
 
92
 
 
93
 
.. vim: set spell spelllang=en: