Sources : happybase.readthedocs.io,
Related pages: setups
See setupsfor installation guides, and happybase configuration.
python2.7
import happybase
#Connect hbase thrift service on port 9090, hbase thrift server must be running.
connection=happybase.Connection("192.168.56.1")
connection=happybase.Connection("192.168.56.1", table_prefix='ns1') #for specific namespace
connection.open()
#Connection pool
pool = happybase.ConnectionPool(size=3, host='192.168.56.1')
with pool.connection() as connection:
print(connection.tables())
#Connection pool for specific namespace(table_prefix)
pool = happybase.ConnectionPool(size=3, host='192.168.56.1', table_prefix='projectNameX')
with pool.connection() as connection:
connection.tables()
#Create table #If connection is for specific namespace then tables will be created with 'namespace_' as prefix
connection.create_table('hbtable', {'cf1' : dict(max_versions=3), 'cf2' : dict() } )
#get table instance
table=connection.table('hbtable')
#put rows in table
table.put(b'1', {b'cf1:name': b'Rahul', b'cf2:employer':b'emdee Digitronics Pvt. Ltd', b'cf1:age':b'31'})
table.put(b'2', {b'cf1:name': b'Rohit', b'cf2:employer':b'ugam', b'cf1:age':b'31'})
#multiple puts:
with table.batch() as b:
b.put(b'3', {b'cf1:name': b'name3', b'cf1:age': b'age3'})
b.put(b'4', {b'cf1:col1': b'name4', b'cf1:age': b'age4'})
b.put(b'4', {b'cf1:name': b'name4', b'cf1:age': b'age4'})
#get specific row
row=table.row(b"1")
print(row[b'cf1:name'])
print(row[b'cf2:employer'])
for data in row:
print(str(data) + "===" + str(row[data]))
#get specific rows
rows=table.rows([b"1", b'2'])
for key, datas in rows:
print(key)
for data in datas:
print(str(data) + "===" + str(datas[data]))
#get specific qualifiers only
row = table.row(b'1', columns=[b'cf1:name', b'cf2:employer'])
for data in row:
print(str(data) + "===" + str(row[data]))
#get all qualifiers of specific columnFamily
row = table.row(b'1', columns=[b'cf1'])
for data in row:
print(str(data) + "===" + str(row[data]))
#get values earlier than given timestamp
row = table.row(b'1', timestamp=1492920395629) #helpful in retrieving older version of data
for data in row:
print(str(data) + "===" + str(row[data]))
#obtain timestamp of insertion
row = table.row(b'1', columns=[b'cf1:name'], include_timestamp=True)
value, timestamp = row[b'cf1:name']
print(value, timestamp)
#scan all rows in table
for key, data in table.scan():
print(key, data)
#scan rowkeys starting from
for key, data in table.scan(row_start=b'2'):
print(key, data)
for key, data in table.scan(row_stop=b'2'):
print(key, data)
for key, data in table.scan(row_start=b'1', row_stop=b'2'):
print(key, data)
for key, data in table.scan(row_prefix=b'1'):
print(key, data)
#delete a row's qualifier
table.delete(b'4', {b'cf1:col1'})
table.delete(b'4')
##Counters
print(table.counter_inc(b'5', b'cf1:counter')) # prints 1
print(table.counter_inc(b'5', b'cf1:counter')) # prints 1
print(table.counter_inc(b'5', b'cf1:counter')) # prints 1
print(table.counter_dec(b'5', b'cf1:counter')) # prints 1
print(table.counter_inc(b'5', b'cf1:counter', value=3)) last counter + 3
print(table.counter_get(b'5', b'cf1:counter'))
print(table.counter_set(b'5', b'cf1:counter', 6))
print(table.counter_get(b'5', b'cf1:counter'))
print(table.counter_set(b'5', b'cf1:counter2', 6))
print(table.counter_get(b'5', b'cf1:counter2'))