您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)HBase基本API操作之CRUD的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
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;
創(chuàng)建一張userinfo表,對(duì)其表進(jìn)行相關(guān)操作。
public class TestHBase { public static void main(String[] args) throws IOException { //2.獲得會(huì)話 Admin admin = null; Connection con = null; try { //操作hbase數(shù)據(jù)庫(kù) //1.建立連接 Configuration conf = HBaseConfiguration.create(); //獲得配制文件對(duì)象 conf.set("hbase.zookeeper.quorum", "192.168.226.129"); con = ConnectionFactory.createConnection(conf); //獲得連接對(duì)象 admin = con.getAdmin(); //3.操作 //建立數(shù)據(jù)庫(kù) //a.判斷數(shù)據(jù)庫(kù)是否存在 TableName tn = TableName.valueOf("userinfo"); //創(chuàng)建表名對(duì)象 if ( admin.tableExists(tn) ) { System.out.println("----> 表存在, 刪除表....."); admin.disableTable(tn); //使用表失效 admin.deleteTable(tn); //刪除表 System.out.println("----> 刪除表成功...."); }else{ System.out.println("----> 表不存在, 創(chuàng)建表....."); } //創(chuàng)建表結(jié)構(gòu)對(duì)象:用于描述表名和相關(guān)的列族 HTableDescriptor htd = new HTableDescriptor(tn); //創(chuàng)建族列結(jié)構(gòu)對(duì)象 HColumnDescriptor hcd1 = new HColumnDescriptor("familycolumn1"); HColumnDescriptor hcd2 = new HColumnDescriptor("familycolumn2"); //描述相關(guān)的列族 htd.addFamily(hcd1); htd.addFamily(hcd2); //創(chuàng)建表 admin.createTable(htd); System.out.println("創(chuàng)建表成功...."); //在表中插入數(shù)據(jù) //a.單個(gè)插入 //參數(shù)是行鍵 "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")); //獲得表對(duì)象 Table table = con.getTable(tn); table.put(put); //添加數(shù)據(jù) //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")); //參數(shù)是行鍵 "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); //獲得表對(duì)象 table02.put(puts); //********************************* //讀取操作 //實(shí)例化scan對(duì)象。 Scan scan = new Scan(); Table table03 = con.getTable(tn); //獲得表對(duì)象 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(); //取到時(shí)間戳 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取數(shù)據(jù)*****************:"); //get Get 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(); //取到時(shí)間戳 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); } //刪除數(shù)據(jù) System.out.println(" \n\n*****************delete刪除數(shù)據(jù)*****************:"); 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刪除數(shù)據(jù)后*****************:"); //scan scan = new Scan(); table03 = con.getTable(tn); //獲得表對(duì)象 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(); //取到時(shí)間戳 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.關(guān)閉 try { if (admin != null){ admin.close(); } if(con != null){ con.close(); } } catch (IOException e) { e.printStackTrace(); } } }
//運(yùn)行結(jié)果:
----> 表存在, 刪除表..... ----> 刪除表成功.... 創(chuàng)建表成功.... 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取數(shù)據(jù)*****************: rowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : sex, value : male *****************delete刪除數(shù)據(jù)*****************: *****************delete刪除數(shù)據(jù)后*****************: 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
關(guān)于“HBase基本API操作之CRUD的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。