文章目錄
- 1. 準備工作
- 2. 搭建開發環境
- 3. HBase基本作業碼樣本
本文以HBase 0.90.2為例,介紹如何在Windows系統,Eclipse IDEIntegration Environment下,使用Java語言,進行HBase用戶端編程,包含建立表、刪除表、插入記錄、刪除記錄、各種方式下的查詢操作等。
1. 準備工作
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. 搭建開發環境
1、運行Eclipse,建立一個新的Java工程“HBaseClient”,右鍵項目根目錄,選擇 “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中,如下所示為設定檔的一個樣本:
- <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>
- <property skipInDoc="true">
- <name>hbase.defaults.for.version</name>
- <value>0.90.2</value>
- </property>
- </configuration>
3、下面可以在Eclipse環境下進行HBase編程了。
3. HBase基本作業碼樣本3.1 初始化配置
1 privatestatic Configuration conf =null;
2 /**
3 * 初始化配置
4 */
5 static {
6 conf = HBaseConfiguration.create();
7 }
3.2 建立表
- /**
- * 建立表操作
- * @throws IOException
- */
- publicvoid createTable(String tablename, String[] cfs) throws IOException {
- HBaseAdmin admin =new HBaseAdmin(conf);
- if (admin.tableExists(tablename)) {
- System.out.println("表已經存在!");
- }
- else {
- HTableDescriptor tableDesc =new HTableDescriptor(tablename);
- for (int i =0; i < cfs.length; i++) {
- tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
- }
- admin.createTable(tableDesc);
- System.out.println("表建立成功!");
- }
- }
3.3 刪除表
1 /**
2 * 刪除表操作
3 * @param tablename
4 * @throws IOException
5 */
6 publicvoid deleteTable(String tablename) throws IOException {
7 try {
8 HBaseAdmin admin =new HBaseAdmin(conf);
9 admin.disableTable(tablename);
10 admin.deleteTable(tablename);
11 System.out.println("表刪除成功!");
12 } catch (MasterNotRunningException e) {
13 e.printStackTrace();
14 } catch (ZooKeeperConnectionException e) {
15 e.printStackTrace();
16 }
17 }
3.4 插入一行記錄
- /**
- * 插入一行記錄
- * @param tablename
- * @param cfs
- */
- publicvoid 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_1"));
- table.put(put);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
3.5 刪除一行記錄
1 /**
2 * 刪除一行記錄
3 * @param tablename
4 * @param rowkey
5 * @throws IOException
6 */
7 publicvoid deleteRow(String tablename, String rowkey) throws IOException {
8 HTable table =new HTable(conf, tablename);
9 List list =new ArrayList();
10 Delete d1 =new Delete(rowkey.getBytes());
11 list.add(d1);
12 table.delete(list);
13 System.out.println("刪除行成功!");
14 }
3.6 尋找一行記錄
- /**
- * 尋找一行記錄
- * @param tablename
- * @param rowkey
- */
- publicstaticvoid 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()) +"");
- System.out.print(new String(kv.getFamily()) +":");
- System.out.print(new String(kv.getQualifier()) +"");
- System.out.print(kv.getTimestamp() +"");
- System.out.println(new String(kv.getValue()));
- }
- }
3.7 查詢表中所有行
1 /**
2 * 查詢表中所有行
3 * @param tablename
4 */
5 publicvoid scaner(String tablename) {
6 try {
7 HTable table =new HTable(conf, tablename);
8 Scan s =new Scan();
9 ResultScanner rs = table.getScanner(s);
10 for (Result r : rs) {
11 KeyValue[] kv = r.raw();
12 for (int i =0; i < kv.length; i++) {
13 System.out.print(new String(kv[i].getRow()) +"");
14 System.out.print(new String(kv[i].getFamily()) +":");
15 System.out.print(new String(kv[i].getQualifier()) +"");
16 System.out.print(kv[i].getTimestamp() +"");
17 System.out.println(new String(kv[i].getValue()));
18 }
19 }
20 } catch (IOException e) {
21 e.printStackTrace();
22 }
23 }
轉自:http://www.cnblogs.com/panfeng412/archive/2011/08/14/2137984.html
第二個例子
package com.hbase;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 HbaseClient {private static Configuration conf = null; /** * hbase-0.90.3 * 初始化配置 */static {conf = HBaseConfiguration.create();conf.set("hbase.master", "192.168.27.12");}/** * 建立表操作 * * @throws IOException */public void createTable(String tablename, String[] cfs) throws IOException {HBaseAdmin admin = new HBaseAdmin(conf);if (admin.tableExists(tablename)) {System.out.println("表已經存在!");} else {HTableDescriptor tableDesc = new HTableDescriptor(tablename);for (int i = 0; i < cfs.length; i++) {tableDesc.addFamily(new HColumnDescriptor(cfs[i]));}admin.createTable(tableDesc);System.out.println(" 表建立成功!");}}/** * 刪除表操作 * * @param tablename * @throws IOException */public void deleteTable(String tablename) throws IOException {try {HBaseAdmin admin = new HBaseAdmin(conf);if (!admin.tableExists(tablename)) {System.out.println("表不存在, 無需進行刪除操作!");}else{admin.disableTable(tablename);admin.deleteTable(tablename);System.out.println(" 表刪除成功!");}} catch (MasterNotRunningException e) {e.printStackTrace();} catch (ZooKeeperConnectionException e) {e.printStackTrace();}}public void insertRow() throws Exception{ HTable table = new HTable(conf, "test"); Put put = new Put(Bytes.toBytes("row3")); put.add(Bytes.toBytes("cf"), Bytes.toBytes("444"), Bytes.toBytes("value444")); table.put(put);}/** * 插入一行記錄 * * @param tablename * @param cfs */public void writeRow(String tablename, String[] cfs) {try {HTable table = new HTable(conf, tablename);Put put = new Put(Bytes.toBytes("rows3"));for (int j = 0; j < cfs.length; j++) {put.add(Bytes.toBytes(cfs[j]),Bytes.toBytes(cfs[j]+String.valueOf(1)),Bytes.toBytes(cfs[j]+"value"));table.put(put);}System.out.println("寫入成功!");} catch (IOException e) {e.printStackTrace();}}//寫多條記錄public void writeMultRow(String tablename, String[][] cfs) {try {HTable table = new HTable(conf, tablename);List<Put> lists = new ArrayList<Put>();for (int i = 0; i < cfs.length; i++) {Put put = new Put(Bytes.toBytes(cfs[i][0]));put.add(Bytes.toBytes(cfs[i][1]),Bytes.toBytes(cfs[i][2]),Bytes.toBytes(cfs[i][3]));lists.add(put);}table.put(lists);System.out.println("寫入成功!");} catch (IOException e) {e.printStackTrace();}}/** * 刪除一行記錄 * * @param tablename * @param rowkey * @throws IOException */public void deleteRow(String tablename, String rowkey) throws IOException {HTable table = new HTable(conf, tablename);List<Delete> list = new ArrayList<Delete>();Delete d1 = new Delete(rowkey.getBytes());list.add(d1);table.delete(list);System.out.println("刪除行成功!");}/** * 尋找一行記錄 * * @param tablename * @param rowkey */public static void selectRow(String tablename, String rowKey)throws IOException {HTable table = new HTable(conf, tablename);Get g = new Get(rowKey.getBytes());//g.addColumn(Bytes.toBytes("cf:1"));Result rs = table.get(g);for (KeyValue kv : rs.raw()) {System.out.print(new String(kv.getRow()) + " ");System.out.print(new String(kv.getFamily()) + ":");System.out.print(new String(kv.getQualifier()) + " ");System.out.print(kv.getTimestamp() + " ");System.out.println(new String(kv.getValue()));}}/** * 查詢表中所有行 * * @param tablename */public void scaner(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();}}public static void main(String[] args) throws Exception {HbaseClient cli = new HbaseClient();//刪除表cli.deleteTable("test");//建立表cli.createTable("test", new String[]{"cf"});//寫多條記錄cli.writeMultRow("test", new String[][]{{"rows1","cf","1","value1"},{"rows1","cf","2","value2"},{"rows2","cf","2","value2"}});//寫一條記錄cli.writeRow("test", new String[]{"cf"});System.out.println("\n查詢一個rows1:");selectRow("test", "rows1");System.out.println("\n查詢所有:");cli.scaner("test");}}
轉自: http://blog.csdn.net/renren000/article/details/6662595