HBase基本API(java)操作(增刪改查)

來源:互聯網
上載者:User

標籤:

//package hbaseExec2;/* * 建立一個students表,並進行相關操作 */import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;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.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.util.Bytes;public class HBaseJavaAPI {// 聲明靜態配置private static Configuration conf = null;static {conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "192.168.6.91");conf.set("hbase.zookeeper.property.clientPort", "2181");}//判斷表是否存在private static boolean isExist(String tableName) throws IOException {HBaseAdmin hAdmin = new HBaseAdmin(conf);return hAdmin.tableExists(tableName);}// 建立資料庫表public static void createTable(String tableName, String[] columnFamilys)throws Exception {// 建立一個資料庫管理員HBaseAdmin hAdmin = new HBaseAdmin(conf);if (hAdmin.tableExists(tableName)) {System.out.println("表 "+tableName+" 已存在!");System.exit(0);} else {// 建立一個students表的描述HTableDescriptor tableDesc = new HTableDescriptor(tableName);// 在描述裡添加列族for (String columnFamily : columnFamilys) {tableDesc.addFamily(new HColumnDescriptor(columnFamily));}// 根據配置好的描述建表hAdmin.createTable(tableDesc);System.out.println("建立表 "+tableName+" 成功!");}}// 刪除資料庫表public static void deleteTable(String tableName) throws Exception {// 建立一個資料庫管理員HBaseAdmin hAdmin = new HBaseAdmin(conf);if (hAdmin.tableExists(tableName)) {// 關閉一個表hAdmin.disableTable(tableName);hAdmin.deleteTable(tableName);System.out.println("刪除表 "+tableName+" 成功!");} else {System.out.println("刪除的表 "+tableName+" 不存在!");System.exit(0);}}// 添加一條資料public static void addRow(String tableName, String row,String columnFamily, String column, String value) throws Exception {HTable table = new HTable(conf, tableName);Put put = new Put(Bytes.toBytes(row));// 指定行// 參數分別:列族、列、值put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),Bytes.toBytes(value));table.put(put);}// 刪除一條(行)資料public static void delRow(String tableName, String row) throws Exception {HTable table = new HTable(conf, tableName);Delete del = new Delete(Bytes.toBytes(row));table.delete(del);}// 刪除多條資料public static void delMultiRows(String tableName, String[] rows)throws Exception {HTable table = new HTable(conf, tableName);List<Delete> delList = new ArrayList<Delete>();for (String row : rows) {Delete del = new Delete(Bytes.toBytes(row));delList.add(del);}table.delete(delList);}// 擷取一條資料public static void getRow(String tableName, String row) throws Exception {HTable table = new HTable(conf, tableName);Get get = new Get(Bytes.toBytes(row));Result result = table.get(get);// 輸出結果,raw方法返回所有keyvalue數組for (KeyValue rowKV : result.raw()) {System.out.print("行名:" + new String(rowKV.getRow()) + " ");System.out.print("時間戳記:" + rowKV.getTimestamp() + " ");System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");System.out.print("列名:" + new String(rowKV.getQualifier()) + " ");System.out.println("值:" + new String(rowKV.getValue()));}}// 擷取所有資料public static void getAllRows(String tableName) throws Exception {HTable table = new HTable(conf, tableName);Scan scan = new Scan();ResultScanner results = table.getScanner(scan);// 輸出結果for (Result result : results) {for (KeyValue rowKV : result.raw()) {System.out.print("行名:" + new String(rowKV.getRow()) + " ");System.out.print("時間戳記:" + rowKV.getTimestamp() + " ");System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");System.out.print("列名:" + new String(rowKV.getQualifier()) + " ");System.out.println("值:" + new String(rowKV.getValue()));}}}// 主函數public static void main(String[] args) {try {String tableName = "student";// 第一步:建立資料庫表:“student”String[] columnFamilys = { "info", "course" };    HBaseJavaAPI.createTable(tableName, columnFamilys);// 第二步:向資料表的添加資料// 添加第一行資料if (isExist(tableName)) {HBaseJavaAPI.addRow(tableName, "zpc", "info", "age", "20");HBaseJavaAPI.addRow(tableName, "zpc", "info", "sex", "boy");HBaseJavaAPI.addRow(tableName, "zpc", "course", "china", "97");HBaseJavaAPI.addRow(tableName, "zpc", "course", "math", "128");HBaseJavaAPI.addRow(tableName, "zpc", "course", "english", "85");// 添加第二行資料HBaseJavaAPI.addRow(tableName, "henjun", "info", "age", "19");HBaseJavaAPI.addRow(tableName, "<span style="font-family: Arial, Helvetica, sans-serif;">henjun</span>", "info", "sex", "boy");HBaseJavaAPI.addRow(tableName, "henjun", "course", "china","90");HBaseJavaAPI.addRow(tableName, "henjun", "course", "math","120");HBaseJavaAPI.addRow(tableName, "henjun", "course", "english","90");// 添加第三行資料HBaseJavaAPI.addRow(tableName, "niaopeng", "info", "age", "18");HBaseJavaAPI.addRow(tableName, "<span style="font-family: Arial, Helvetica, sans-serif;">niaopeng</span>", "info", "sex","girl");HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "china","100");HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "math","100");HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "english","99");// 第三步:擷取一條資料System.out.println("**************擷取一條(zpc)資料*************");HBaseJavaAPI.getRow(tableName, "zpc");// 第四步:擷取所有資料System.out.println("**************擷取所有資料***************");HBaseJavaAPI.getAllRows(tableName);// 第五步:刪除一條資料System.out.println("************刪除一條(zpc)資料************");HBaseJavaAPI.delRow(tableName, "zpc");HBaseJavaAPI.getAllRows(tableName);// 第六步:刪除多條資料System.out.println("**************刪除多條資料***************");String rows[] = new String[] { "qingqing","xiaoxue" };HBaseJavaAPI.delMultiRows(tableName, rows);HBaseJavaAPI.getAllRows(tableName);// 第七步:刪除資料庫System.out.println("***************刪除資料庫表**************");HBaseJavaAPI.deleteTable(tableName);System.out.println("表"+tableName+"存在嗎?"+isExist(tableName));} else {System.out.println(tableName + "此資料庫表不存在!");}} catch (Exception e) {e.printStackTrace();}}}

HBase基本API(java)操作(增刪改查)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.