日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

用 Java 对 hbase 进行CRUD增删改查操作

發(fā)布時(shí)間:2025/6/15 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用 Java 对 hbase 进行CRUD增删改查操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文以HBase 0.90.2為例,介紹如何在Windows系統(tǒng),Eclipse IDE集成環(huán)境下,使用Java語言,進(jìn)行HBase客戶端編程,包含建立表、刪除表、插入記錄、刪除記錄、各種方式下的查詢操作等。

1. 準(zhǔn)備工作

1、下載后安裝jdk包(這里使用的是jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008);

2、下載eclipse,解壓到本地(這里使用的是eclipse-java-helios-SR2-win32);

3、下載HBase包,解壓安裝包到本地(這里使用的是hbase-0.90.2)。

2. 搭建開發(fā)環(huán)境

1、運(yùn)行Eclipse,創(chuàng)建一個(gè)新的Java工程“HBaseClient”,右鍵項(xiàng)目根目錄,選擇“Properties”->“Java Build Path”->“Library”->“Add External JARs”,將HBase解壓后根目錄下的hbase-0.90.2.jar、hbase-0.90.2-tests.jar和lib子目錄下所有jar包添加到本工程的Classpath下。

2、按照步驟1中的操作,將自己所連接的HBase的配置文件hbase-site.xml添加到本工程的Classpath中,如下所示為配置文件的一個(gè)示例。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <configuration> <property> <name>hbase.rootdir</name> <value> hdfs://hostname:9000/hbase </value> </property> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>*.*.*.*, *.*.*.*, *.*.*.*</value> </property> <propertyskipInDoc="true"> <name>hbase.defaults.for.version</name> <value>0.90.2</value> </property> </configuration>

3、下面可以在Eclipse環(huán)境下進(jìn)行HBase編程了。

3. HBase基本操作代碼示例

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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.MasterNotRunningException; 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.util.Bytes; public class CreateTable { ????private static Configuration conf = null; ????// 初始化配置 ????static { ????????conf = HBaseConfiguration.create(); ????} ????// 1、建表 ????public static void createTable(String tablename, String[] cfs) ????????????throws IOException { ????????HBaseAdmin admin = new HBaseAdmin(conf); ????????if (admin.tableExists(tablename)) { ????????????System.out.println("表已經(jīng)存在!"); ????????} else { ????????????HTableDescriptor tableDesc = new HTableDescriptor(tablename); ????????????for (int i = 0; i < cfs.length; i++) { ????????????????// 表建好后,列簇不能動(dòng)態(tài)增加,而列是可以動(dòng)態(tài)增加的,這是hbase伸縮性的一個(gè)體現(xiàn)。 ????????????????tableDesc.addFamily(new HColumnDescriptor(cfs[i])); ????????????} ????????????admin.createTable(tableDesc); ????????????System.out.println("表創(chuàng)建成功!"); ????????} ????} ????// 2、插入數(shù)據(jù) ????public static void writeRow(String tablename, String[] cfs) { ????????try { ????????????HTable table = new HTable(conf, tablename); ????????????Put put = new Put(Bytes.toBytes("rows1")); ????????????for (int j = 0; j < cfs.length; j++) { ????????????????put.add(Bytes.toBytes(cfs[j]), // 指定列簇 ????????????????????????Bytes.toBytes(String.valueOf("列1")),// 指定列名 ????????????????????????Bytes.toBytes("value_13"));// 指定列值 ????????????????put.add(Bytes.toBytes(cfs[j]), ????????????????????????Bytes.toBytes(String.valueOf("lie2")), ????????????????????????Bytes.toBytes("value_24")); ????????????????table.put(put); ????????????????System.out.println("插入數(shù)據(jù)成功"); ????????????} ????????} catch (IOException e) { ????????????e.printStackTrace(); ????????} ????} ????// 3、刪除一行數(shù)據(jù) ????public static void deleteRow(String tablename, String rowkey) ????????????throws IOException { ????????HTable table = new HTable(conf, tablename); ????????List list = new ArrayList(); ????????Delete d1 = new Delete(rowkey.getBytes()); ????????list.add(d1); ????????table.delete(list); ????????System.out.println("刪除行成功!"); ????} ????// 4、查找一行數(shù)據(jù) ????public static void selectRow(String tablename, String rowKey) ????????????throws IOException { ????????HTable table = new HTable(conf, tablename); ????????Get g = new Get(rowKey.getBytes()); ????????Result rs = table.get(g); ????????for (KeyValue kv : rs.raw()) { ????????????System.out.print(new String(kv.getRow()) + "? ");// 行號(hào) ????????????System.out.print(new String(kv.getFamily()) + ":");// 列簇名 ????????????System.out.print(new String(kv.getQualifier()) + "? ");// 列名 ????????????System.out.print(kv.getTimestamp() + "? ");// 時(shí)間戳 ????????????System.out.println(new String(kv.getValue()));// 單元格的值 ????????} ????} ????// 5、查找全部數(shù)據(jù) ????public static void scanerTable(String tablename) { ????????try { ????????????HTable table = new HTable(conf, tablename); ????????????Scan s = new Scan(); ????????????ResultScanner rs = table.getScanner(s); ????????????for (Result r : rs) { ????????????????KeyValue[] kv = r.raw(); ????????????????for (int i = 0; i < kv.length; i++) { ????????????????????System.out.print(new String(kv[i].getRow()) + "? "); ????????????????????System.out.print(new String(kv[i].getFamily()) + ":"); ????????????????????System.out.print(new String(kv[i].getQualifier()) + "? "); ????????????????????System.out.print(kv[i].getTimestamp() + "? "); ????????????????????System.out.println(new String(kv[i].getValue())); ????????????????} ????????????} ????????} catch (IOException e) { ????????????e.printStackTrace(); ????????} ????} ????// 6、刪除表 ????public static void deleteTable(String tablename) throws IOException { ????????try { ????????????HBaseAdmin admin = new HBaseAdmin(conf); ????????????admin.disableTable(tablename); ????????????admin.deleteTable(tablename); ????????????System.out.println("表刪除成功!"); ????????} catch (MasterNotRunningException e) { ????????????e.printStackTrace(); ????????} catch (ZooKeeperConnectionException e) { ????????????e.printStackTrace(); ????????} ????} ????public static void main(String[] args) throws IOException { ????????String[] cfs = "a,b,c".split(","); ????????// createTable("test01", cfs); ????????// writeRow("test01", cfs); ????????// deleteRow("test01", "rows1"); ????????// selectRow("test01", "rows2"); ????????// scanerTable("test01"); ????????// deleteTable("test01"); ????} }

總結(jié)

以上是生活随笔為你收集整理的用 Java 对 hbase 进行CRUD增删改查操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。