W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
要將Jython與HBase
一起使用,您的 CLASSPATH
必須包含 HBase
的類路徑以及代碼所需的 Jython JAR
。
將路徑設置為包含 Jython .jar
的目錄,以及每個項目需要的附加的 Jython
相關 JAR
。然后導出指向$ JYTHON_HOME
環(huán)境變量的 HBASE_CLASSPATH
。
$ export HBASE_CLASSPATH=/directory/jython.jar
在類路徑中使用 HBase
和 Hadoop JAR
啟動 Jython shell:$ bin / hbase org.python.util.jython
使用 Jython
創(chuàng)建表,填充,獲取和刪除表
以下 Jython
代碼示例檢查表,如果存在,則刪除它然后創(chuàng)建它。然后,它使用數(shù)據(jù)填充表并獲取數(shù)據(jù)。
import java.lang
from org.apache.hadoop.hbase import HBaseConfiguration, HTableDescriptor, HColumnDescriptor, TableName
from org.apache.hadoop.hbase.client import Admin, Connection, ConnectionFactory, Get, Put, Result, Table
from org.apache.hadoop.conf import Configuration
# First get a conf object. This will read in the configuration
# that is out in your hbase-*.xml files such as location of the
# hbase master node.
conf = HBaseConfiguration.create()
connection = ConnectionFactory.createConnection(conf)
admin = connection.getAdmin()
# Create a table named 'test' that has a column family
# named 'content'.
tableName = TableName.valueOf("test")
table = connection.getTable(tableName)
desc = HTableDescriptor(tableName)
desc.addFamily(HColumnDescriptor("content"))
# Drop and recreate if it exists
if admin.tableExists(tableName):
admin.disableTable(tableName)
admin.deleteTable(tableName)
admin.createTable(desc)
# Add content to 'column:' on a row named 'row_x'
row = 'row_x'
put = Put(row)
put.addColumn("content", "qual", "some content")
table.put(put)
# Now fetch the content just added, returns a byte[]
get = Get(row)
result = table.get(get)
data = java.lang.String(result.getValue("content", "qual"), "UTF8")
print "The fetched row contains the value '%s'" % data
使用 Jython 進行表掃描
此示例掃描表并返回與給定族限定符匹配的結果。
import java.lang
from org.apache.hadoop.hbase import TableName, HBaseConfiguration
from org.apache.hadoop.hbase.client import Connection, ConnectionFactory, Result, ResultScanner, Table, Admin
from org.apache.hadoop.conf import Configuration
conf = HBaseConfiguration.create()
connection = ConnectionFactory.createConnection(conf)
admin = connection.getAdmin()
tableName = TableName.valueOf('wiki')
table = connection.getTable(tableName)
cf = "title"
attr = "attr"
scanner = table.getScanner(cf)
while 1:
result = scanner.next()
if not result:
break
print java.lang.String(result.row), java.lang.String(result.getValue(cf, attr))
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: