null

Show HBase table data in Django using Happybase

To work with Apache Hbase and django as backend , we need to use Happybase python library to connect with Hbase .

HappyBase is a developer-friendly Python library to interact with Apache HBase. 

HappyBase is designed for use in standard HBase setups, and offers application developers a Pythonic API to interact with HBase.

HappyBase uses the Python Thrift library to connect to HBase using its Thrift gateway.

Below is a sample django project example to work with Hbase

Install below requirements :-

happybase==1.2.0

Django==3.0.3 

 

Create a django project .

  • Open settings.py file and add below Variables

# HBase settings

HBASE_HOST = 'localhost'

HBASE_TABLE = 'test_table

 

  • In views.py add the business logic to call and connect hbase through Python and django.

#Views starts here

    import json

    import logging

    import random

    import threading

    from django.conf import settings

    from django.http import HttpResponse

    import happybase

    # Initialization

    N_KEYS = 10000

    pool = happybase.ConnectionPool(

            size=3,host=settings.HBASE_HOST)

    

def populate_table():

        with pool.connection() as connection:

              connection.delete_table(settings.HBASE_TABLE, disable=True)

            connection.create_table(

                   settings.HBASE_TABLE,

                    families={'cf': {}}

                )

            table = connection.table(settings.HBASE_TABLE)

            with table.batch() as b:

                    for i in xrange(N_KEYS):

                        row_data = {'cf:col1': 'value-%d' % i}

                        b.put('row-key-%d' % i, row_data)    

 

with pool.connection() as connection:

    if not settings.HBASE_TABLE in connection.tables():

        populate_table()

# Views , index will return the hbase data as json response and django with render the data as # httpresponse.

def index(request):

    start = 'row-key-%d' % random.randint(0, N_KEYS)

    with pool.connection() as connection:

        table = connection.table(settings.HBASE_TABLE)

        scan = table.scan(row_start=start, limit=4)

        output = list(scan)

    if 'use-after-return' in request.GET:

        # XXX: It is an error to use the connection after it was

        # returned to the pool. The next line introduces a race

        # condition and may cause random errors when used in

        # a multi-threaded environment!

        connection.tables()

 

    logger.debug('Request from thread %s', threading.current_thread().name)

    return HttpResponse(

        json.dumps(output),

        content_type='application/json')

 

If we want we can modify and render the data to our preferred specific html templates and display the data using django template language (Jinja).

 

Urls.py:-

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:

# from django.contrib import admin

# admin.autodiscover()

 

urlpatterns = patterns('',

    url(r'^$', 'myproj.views.index', name='index'),

    # Examples:

    # url(r'^$', 'myproj.views.home', name='home'),

    # url(r'^myproj/', include('myproj.foo.urls')),

 

    # Uncomment the admin/doc line below to enable admin documentation:

    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

 

    # Uncomment the next line to enable the admin:

    # url(r'^admin/', include(admin.site.urls)),

)

 

Hence our sample webapp is ready and we can view the hbase data with own web application .

0
0 Comments
Publish Publish

Choose Background


Delete