java代码如下
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
import java.util.Scanner;
import org.apache.hadoop.hbase.util.Bytes;
public class hbase{
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args)throws IOException{
}
//建立连接
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
//关闭连接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
//建表
public static void createTable(String tableName, String[] fields) throws IOException {
init(); //建立与hbase的链接
TableName tablename = TableName.valueOf(tableName);
if (admin.tableExists(tablename)) { //如果表已经存在
System.out.println("这个"+tablename+"表已经存在咯!"); //打印表存在
admin.disableTable(tablename); //禁用表
admin.deleteTable(tablename); //删除表
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);
for (String str : fields) { //将存在fields的多个属性值去除添加进表
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("创建了"+tablename+"表");
close();
}
//删表
public static void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
}
//查看已有表
public static void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
for (int i = 0; i != fields.length; i++) {
Put put = new Put(row.getBytes());
String[] cols = fields[i].split(":");
put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
table.put(put);
}
table.close();
System.out.println("这个"+row+"列放入了列限定和值!"); //打印列删除
close();
}
//删除数据
public static void deleteRow(String tableName, String row) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete=new Delete(row.getBytes());
table.delete(delete);
System.out.println("这个"+delete+"行已经删除咯!"); //打印列删除
table.close();
close();
}
public static void scanColumn(String tableName, String column) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes(column));
ResultScanner scanner = table.getScanner(scan);
for (Result result = scanner.next(); result != null; result = scanner.next()) {
showCell(result);
}
table.close();
close();
}
public static void modifyData(String tableName, String row, String column, String val) throws IOException {
long ts = 0;
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(row.getBytes());
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for (Result r : resultScanner) {
for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) {
ts = cell.getTimestamp();
}
}
put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());
table.put(put);
System.out.println(tableName+"表的"+row+"行的"+column+"列已经修改为:"+val); //打印列删除
table.close();
close();
}
//格式化输出
public static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
System.out.println("Timetamp:"+cell.getTimestamp()+" ");
System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
}
}
}
python的在这里功能和上面的java功能差不多,有点点不同。
使用python要安装thrift 还有pip3 install happybase
如果在pycharm还要再一过去点击install…
运行之前启动hadoop hbase thrift
# _*_ coding:utf-8 _*_
import happybase
def createtables(tablename,list):
tables_list = connection.tables()
if list in tables_list:
print("student tb is exists.")
deletetable(tablename)
else:
print("student tb is not exists.")
connection.create_table(
tablename,
{
'cf1': dict(),
'cf2': dict(),
'cf3': dict(), # use defaults
}
)
def deletetable(tablename):
connection.disable_table(tablename)
connection.delete_table(tablename)
def addRecord(row,fields,values):
with table.batch() as bat:
bat.put(row,{fields:values})
def scanColumn(column):
for key, value in table.scan():
cel=table.cells(key,column)
print(str(key)+':'+str(column)+':',end="")
print(cel)
def modifyData(row,column,values):
for key,value in table.scan():
cel=table.cells(key,column)
print(str(key)+':'+str(column)+':'+str(cel))
table.delete(row,columns=[column])
addRecord(row,column,values)
def deleteRow(table,row):
table.delete(row,columns=None)
connection = happybase.Connection('localhost', autoconnect=False)
try:
connection.open()
except Exception as err:
print(err)
a='nihao'
b=b"nihao"
table = connection.table(a)
createtables(a,b)
addRecord('w1','cf2:nihao','unbeliver')
scanColumn('cf2:nihao')
modifyData('w1','cf2:nihao','999')
deleteRow(table,'w1')
print(connection.tables())#查看hbase现有的所有表名
connection.close()