HBase(五):HBase基本API操作之CRUD
生活随笔
收集整理的這篇文章主要介紹了
HBase(五):HBase基本API操作之CRUD
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
import java.io.IOException;import java.util.Arrays; 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.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; 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.client.Table; import org.apache.hadoop.hbase.util.Bytes;創建一張userinfo表,對其表進行相關操作。
public class TestHBase {public static void main(String[] args) throws IOException {//2.獲得會話Admin admin = null;Connection con = null;try {//操作hbase數據庫//1.建立連接Configuration conf = HBaseConfiguration.create(); //獲得配制文件對象conf.set("hbase.zookeeper.quorum", "192.168.226.129");con = ConnectionFactory.createConnection(conf); //獲得連接對象admin = con.getAdmin();//3.操作//建立數據庫//a.判斷數據庫是否存在TableName tn = TableName.valueOf("userinfo"); //創建表名對象if ( admin.tableExists(tn) ) {System.out.println("----> 表存在, 刪除表.....");admin.disableTable(tn); //使用表失效admin.deleteTable(tn); //刪除表System.out.println("----> 刪除表成功....");}else{System.out.println("----> 表不存在, 創建表.....");}//創建表結構對象:用于描述表名和相關的列族HTableDescriptor htd = new HTableDescriptor(tn);//創建族列結構對象HColumnDescriptor hcd1 = new HColumnDescriptor("familycolumn1"); HColumnDescriptor hcd2 = new HColumnDescriptor("familycolumn2");//描述相關的列族htd.addFamily(hcd1);htd.addFamily(hcd2);//創建表admin.createTable(htd);System.out.println("創建表成功....");//在表中插入數據//a.單個插入//參數是行鍵 "row01".getBytes()Put put = new Put(Bytes.toBytes("row1")); //定位行: put.addColumn(family, qualifier, value) put.addColumn(Bytes.toBytes("familycolumn1"), Bytes.toBytes("name"), Bytes.toBytes("Berg"));//獲得表對象Table table = con.getTable(tn);table.put(put); //添加數據//b.批量插入Put put01 = new Put(Bytes.toBytes("row2")); put01.addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("sex"), Bytes.toBytes("male")).addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("age"), Bytes.toBytes("22"));Put put02 = new Put(Bytes.toBytes("row3")); //參數是行鍵 "row01".getBytes()put02.addColumn(Bytes.toBytes("familycolumn1"), Bytes.toBytes("sex"), Bytes.toBytes("female"));List<Put> puts = Arrays.asList(put01, put02);Table table02 = con.getTable(tn); //獲得表對象table02.put(puts);//*********************************//讀取操作//實例化scan對象。Scan scan = new Scan();Table table03 = con.getTable(tn); //獲得表對象ResultScanner rs = table03.getScanner(scan);for (Result result : rs) {List<Cell> cs = result.listCells();for (Cell cell : cs) {String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); //取行鍵long timestamp = cell.getTimestamp(); //取到時間戳String family = Bytes.toString(CellUtil.cloneFamily(cell)); //取到族列String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); //取到修飾名String value = Bytes.toString(CellUtil.cloneValue(cell)); //取到值System.out.println("rowKey : " + rowKey + ", timestamp : " + timestamp + ", family : " + family + ", qualifier : " + qualifier + ", value : " + value);}}System.out.println(" \n\n*****************get取數據*****************:");//getGet get = new Get(Bytes.toBytes("row2"));get.addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("sex"));Table t04 = con.getTable(tn);Result r = t04.get(get);List<Cell> cs = r.listCells();for (Cell cell : cs) {String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); //取行鍵long timestamp = cell.getTimestamp(); //取到時間戳String family = Bytes.toString(CellUtil.cloneFamily(cell)); //取到族列String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); //取到修飾名String value = Bytes.toString(CellUtil.cloneValue(cell)); //取到值System.out.println("rowKey : " + rowKey + ", timestamp : " + timestamp + ", family : " + family + ", qualifier : " + qualifier + ", value : " + value);}//刪除數據System.out.println(" \n\n*****************delete刪除數據*****************:");Delete delete = new Delete(Bytes.toBytes("row2"));delete.addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("age"));Table t05 = con.getTable(tn);t05.delete(delete);System.out.println(" \n\n*****************delete刪除數據后*****************:");//scanscan = new Scan();table03 = con.getTable(tn); //獲得表對象rs = table03.getScanner(scan);for (Result result : rs) {cs = result.listCells();for (Cell cell : cs) {String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); //取行鍵long timestamp = cell.getTimestamp(); //取到時間戳String family = Bytes.toString(CellUtil.cloneFamily(cell)); //取到族列String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); //取到修飾名String value = Bytes.toString(CellUtil.cloneValue(cell)); //取到值System.out.println("rowKey : " + rowKey + ", timestamp : " + timestamp + ", family : " + family + ", qualifier : " + qualifier + ", value : " + value);}}} catch (IOException e) {e.printStackTrace();}//4.關閉try {if (admin != null){admin.close();}if(con != null){con.close();}} catch (IOException e) {e.printStackTrace();}}}?
//運行結果:
----> 表存在, 刪除表..... ----> 刪除表成功.... 創建表成功.... rowKey : row1, timestamp : 1463486961279, family : familycolumn1, qualifier : name, value : Berg rowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : age, value : 22 rowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : sex, value : male rowKey : row3, timestamp : 1463486961289, family : familycolumn1, qualifier : sex, value : female*****************get取數據*****************: rowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : sex, value : male*****************delete刪除數據*****************:*****************delete刪除數據后*****************: rowKey : row1, timestamp : 1463486961279, family : familycolumn1, qualifier : name, value : Berg rowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : sex, value : male rowKey : row3, timestamp : 1463486961289, family : familycolumn1, qualifier : sex, value : female?
轉載于:https://my.oschina.net/gently/blog/676899
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的HBase(五):HBase基本API操作之CRUD的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pyv8安装
- 下一篇: FLASK上传时有中文文件名的解决方案