1. 程式人生 > >Hbase java API 的呼叫例子

Hbase java API 的呼叫例子


1、首先要在專案中匯入Hbase依賴的jar包

2、修改windows中的 C:\Windows\System32\drivers\etc\hosts

10.49.85.152    master
10.49.85.182    slaver1
10.49.85.183    slaver2

3、Java API

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache
.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.MasterNotRunningException
; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache
.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.Column; public class HBaseDemo { static Configuration conf = null; static{ conf = HBaseConfiguration.create(); conf.set("hbase.rootdir", "hdfs://10.49.85.152:9000/hbase"); conf.set("hbase.master", "hdfs://10.49.85.152:60000"); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.zookeeper.quorum", "master,slaver1,slaver2"); } //實現了list功能 public static List<String> getAllTables() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ List<String> list = null; //HBaseAdmin可以建立表、刪除表、查看錶 HBaseAdmin admin = new HBaseAdmin(conf); if(admin != null){ //HTableDescriptor表的相關資訊 HTableDescriptor[] tables = admin.listTables(); for(HTableDescriptor table : tables){ System.out.println(table.getNameAsString()); list = new ArrayList<String>(); list.add(table.getNameAsString()); } } admin.close(); return list; } public static int createTabble(String tableName, String[] family) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ HBaseAdmin admin = new HBaseAdmin(conf); HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName)); // HTableDescriptor table = new HTableDescriptor(tableName); //HColumnDescriptor列的相關資訊 for(String str : family){ HColumnDescriptor cloumn = new HColumnDescriptor(str); cloumn.setMaxVersions(3); table.addFamily(cloumn); } if(admin.tableExists(tableName)){ System.out.println(tableName + "已經存在"); return -1; } admin.createTable(table); admin.close(); return 1; } public static void deleteTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ HBaseAdmin admin = new HBaseAdmin(conf); admin.disableTable(tableName); admin.deleteTable(tableName); System.out.println(tableName + "成功刪除"); admin.close(); } public static void insert(String tableName, String rowkey, String family, String column, String cell) throws IOException{ Put put = new Put(rowkey.getBytes()); //HTable負責表的get put delete操作 HTable table = new HTable(conf, tableName); put.add(family.getBytes(), column.getBytes(), cell.getBytes()); table.put(put); } public static Result queryByRow(String tableName, String rowkey) throws IOException{ Get get = new Get(rowkey.getBytes()); HTable table = new HTable(conf, tableName); return table.get(get); } public static Result queryByColumn(String tableName, String rowkey, String family, String column) throws IOException{ Get get = new Get(rowkey.getBytes()); HTable table = new HTable(conf, tableName); get.addColumn(family.getBytes(), column.getBytes()); return table.get(get); } public static Result queryByRowByVersions(String tableName, String rowkey) throws IOException{ Get get = new Get(rowkey.getBytes()); get.setMaxVersions(3); HTable table = new HTable(conf, tableName); return table.get(get); } public static ResultScanner queryByScan(String tableName) throws IOException{ Scan scan = new Scan(); // scan.setStartRow(startRow); // scan.setStopRow(stopRow); // scan.addColumn(family, qualifier); HTable table = new HTable(conf, tableName); return table.getScanner(scan); } public static void deleteByColumn(String tableName, String rowkey, String family, String column) throws IOException{ Delete delete = new Delete(rowkey.getBytes()); HTable table = new HTable(conf, tableName); delete.deleteColumn(family.getBytes(), column.getBytes()); table.delete(delete); System.out.println("成功刪除"); } public static void deleteByRow(String tableName, String rowkey) throws IOException{ Delete delete = new Delete(rowkey.getBytes()); HTable table = new HTable(conf, tableName); table.delete(delete); System.out.println("成功刪除"); } public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException { //1.建立book表 // createTabble("book", new String[]{"author", "info"}); // getAllTables(); //2.刪除book表 // deleteTable("book"); // getAllTables(); //3.建立book表 // createTabble("book", new String[]{"author", "info"}); // getAllTables(); //4.book表插入資料 // insert("book", "rw001", "author", "name", "zhangsan"); // insert("book", "rw001", "author", "age", "65"); // insert("book", "rw001", "info", "name", "database"); // insert("book", "rw001", "info", "price", "35.6"); //5.按行查詢資料(舊方法) // Result result = queryByRow("book", "rw001"); // List<KeyValue> values = result.list(); // System.out.println("COLUMN\t\t\tCELL "); // for(KeyValue value : values){ // System.out.print(new String(value.getFamily()) + ":"); // System.out.print(new String(value.getQualifier()) + "\t\t"); // System.out.print("value = " + new String(value.getValue()) + ","); // System.out.println("timestamp = " + value.getTimestamp()); // } //5.按行查詢資料(新方法) // Result result = queryByRow("book", "rw001"); // Cell[] cells = result.rawCells(); // System.out.println("COLUMN\t\t\tCELL "); // for(Cell cell : cells){ // System.out.print(new String(CellUtil.cloneFamily(cell)) + ":"); // System.out.print(new String(CellUtil.cloneQualifier(cell)) + "\t\t"); // System.out.print("value = " + new String(CellUtil.cloneValue(cell)) + ","); // System.out.println("timestamp = " + cell.getTimestamp()); // } //6.按列查詢資料(新方法) // Result result = queryByColumn("book", "rw001", "author", "name"); // Cell[] cells = result.rawCells(); // System.out.println("COLUMN\t\t\tCELL "); // for(Cell cell : cells){ // System.out.print(new String(CellUtil.cloneFamily(cell)) + ":"); // System.out.print(new String(CellUtil.cloneQualifier(cell)) + "\t\t"); // System.out.print("value = " + new String(CellUtil.cloneValue(cell)) + ","); // System.out.println("timestamp = " + cell.getTimestamp()); // } //7.scan // ResultScanner scanner = queryByScan("book"); // System.out.println("COLUMN\t\t\tCELL "); // for(Result result : scanner){ // Cell[] cells = result.rawCells(); // // for(Cell cell : cells){ // System.out.print(new String(CellUtil.cloneFamily(cell)) + ":"); // System.out.print(new String(CellUtil.cloneQualifier(cell)) + "\t\t"); // System.out.print("value = " + new String(CellUtil.cloneValue(cell)) + ","); // System.out.println("timestamp = " + cell.getTimestamp()); // } // } //8.刪除某一列 // deleteByColumn("book", "rw001", "author", "name"); //9.刪除某一行資料 // deleteByRow("book", "rw001"); //10. 按行查詢多版本資料(新方法) Result result = queryByRowByVersions("book", "rw001"); Cell[] cells = result.rawCells(); System.out.println("COLUMN\t\t\tCELL "); for(Cell cell : cells){ System.out.print(new String(CellUtil.cloneFamily(cell)) + ":"); System.out.print(new String(CellUtil.cloneQualifier(cell)) + "\t\t"); System.out.print("value = " + new String(CellUtil.cloneValue(cell)) + ","); System.out.println("timestamp = " + cell.getTimestamp()); } } }