天天看點

Jave Hbase API

Hbase API 類和資料模型的對應關系

Jave Hbase API

HBaseAdmin

類:org.apache.hadoop.hbase.client.HBaseAdmin

作用:提供了一個接口來管理 HBase 資料庫的表資訊。它提供的方法包括:建立表,删 除表,列出表項,使表有效或無效,以及添加或删除表列族成員等。

Jave Hbase API

HBaseConfiguration

類:org.apache.hadoop.hbase.HBaseConfiguration

作用:對 HBase 進行配置

Jave Hbase API

HTableDescriptor

類: org.apache.hadoop.hbase.HTableDescriptor

作用:包含了表的名字極其對應表的列族

Jave Hbase API

HColumnDescriptor

類: org.apache.hadoop.hbase.HColumnDescriptor

作用:維護着關于列族的資訊,例如版本号,壓縮設定等。它通常在建立表或者為表添 加列族的時候使用。列族被建立後不能直接修改,隻能通過删除然後重新建立的方式。列族被删除的時候,列族裡面的資料也會同時被删除。

Jave Hbase API
HTable
Jave Hbase API
Jave Hbase API

Put

類: org.apache.hadoop.hbase.client.Put

作用:用來對單個行執行添加操作

Jave Hbase API

Get

類: org.apache.hadoop.hbase.client.Get

作用:用來擷取單個行的相關資訊

Jave Hbase API

Result

類: org.apache.hadoop.hbase.client.Result

作用:存儲 Get 或者 Scan 操作後擷取表的單行值。使用此類提供的方法可以直接擷取值 或者各種 Map 結構( key-value 對)

Jave Hbase API
建立表

1 package com.shujia;
 2 
 3 import org.apache.hadoop.conf.Configuration;
 4 import org.apache.hadoop.hbase.HBaseConfiguration;
 5 import org.apache.hadoop.hbase.HColumnDescriptor;
 6 import org.apache.hadoop.hbase.HTableDescriptor;
 7 import org.apache.hadoop.hbase.TableName;
 8 import org.apache.hadoop.hbase.client.Admin;
 9 import org.apache.hadoop.hbase.client.Connection;
10 import org.apache.hadoop.hbase.client.ConnectionFactory;
11 
12 import java.io.IOException;
13 
14 public class Demo01 {
15     public static void main(String[] args) throws IOException {
16 
17         //建立配置,指定zk叢集
18         Configuration conf = HBaseConfiguration.create();
19         conf.set("hbase.zookeeper.quorum","master,node1,node2");
20 
21         //建立連接配接
22         Connection coon = ConnectionFactory.createConnection(conf);
23 
24         //建立admin對象
25         Admin admin = coon.getAdmin();
26 
27         //建立表
28         HTableDescriptor test_api = new HTableDescriptor(TableName.valueOf("test_api"));
29 
30         //建立列簇
31         HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
32 
33         //配置列簇
34         cf1.setTimeToLive(20); //設定死亡時間20s
35         cf1.setMaxVersions(3); //設定版本
36 
37         //增加列簇
38         test_api.addFamily(cf1);
39 
40         //建立表
41         admin.createTable(test_api);
42 
43         //關閉連接配接
44         coon.close();
45     }
46 }      
1 package com.shujia;
  2 
  3 import org.apache.hadoop.conf.Configuration;
  4 import org.apache.hadoop.hbase.HBaseConfiguration;
  5 import org.apache.hadoop.hbase.HColumnDescriptor;
  6 import org.apache.hadoop.hbase.HTableDescriptor;
  7 import org.apache.hadoop.hbase.TableName;
  8 import org.apache.hadoop.hbase.client.*;
  9 import org.apache.hadoop.hbase.util.Addressing;
 10 import org.apache.hadoop.hbase.util.Bytes;
 11 import org.junit.After;
 12 import org.junit.Before;
 13 import org.junit.Test;
 14 
 15 import javax.swing.tree.VariableHeightLayoutCache;
 16 import java.io.BufferedReader;
 17 import java.io.FileReader;
 18 import java.io.IOException;
 19 import java.util.ArrayList;
 20 
 21 public class Demo03API {
 22     Connection conn;
 23     TableName table=TableName.valueOf("test_api");
 24 
 25     @Before
 26     public void init() throws IOException {
 27         Configuration conf = HBaseConfiguration.create();
 28         conf.set("hbase.zookeeper.quorum","master,node1,node2");
 29 
 30         conn = ConnectionFactory.createConnection(conf);
 31     }
 32     //put
 33     @Test
 34     public void Put() throws IOException {
 35         Table test_api = conn.getTable(TableName.valueOf("test_api"));
 36         Put put = new Put("001".getBytes());
 37         put.addColumn("cf1".getBytes(),"name".getBytes(),"張三".getBytes());
 38         test_api.put(put);
 39     }
 40 
 41     // putAll 讀取students.txt 并将資料寫入HBase
 42     @Test
 43     public void PutAll() throws IOException {
 44         // 建立students表 info
 45         Admin admin = conn.getAdmin();
 46         TableName studentsT = TableName.valueOf("students");
 47         // 判斷表是否存在
 48         if (!admin.tableExists(studentsT)) {
 49             HTableDescriptor students = new HTableDescriptor(studentsT);
 50             HColumnDescriptor info = new HColumnDescriptor("info");
 51             students.addFamily(info);
 52             admin.createTable(students);
 53         }
 54 
 55         Table stu = conn.getTable(studentsT);
 56 
 57 
 58         BufferedReader br = new BufferedReader(new FileReader("data/students.txt"));
 59         String line = null;
 60         ArrayList<Put> puts = new ArrayList<Put>();
 61         int batchSize = 11;
 62         while ((line = br.readLine()) != null) {
 63 
 64             // 讀取每一行資料
 65             String[] split = line.split(",");
 66             String id = split[0];
 67             String name = split[1];
 68             String age = split[2];
 69             String gender = split[3];
 70             String clazz = split[4];
 71             Put put = new Put(id.getBytes());
 72             put.addColumn("info".getBytes(), "name".getBytes(), name.getBytes());
 73             put.addColumn("info".getBytes(), "age".getBytes(), age.getBytes());
 74             put.addColumn("info".getBytes(), "gender".getBytes(), gender.getBytes());
 75             put.addColumn("info".getBytes(), "clazz".getBytes(), clazz.getBytes());
 76             puts.add(put); // 将每條資料建構好的put對象加入puts清單
 77             if (puts.size() == batchSize) {
 78                 stu.put(puts); // 批量寫入
 79                 puts = new ArrayList<Put>();
 80             }
 81         }
 82         if (puts.size() != 0) {
 83             stu.put(puts); // 批量寫入
 84         }
 85 
 86     }
 87     //get
 88     @Test
 89     public void Get() throws IOException {
 90         Table test_api = conn.getTable(table);
 91         Get get = new Get("001".getBytes());
 92         Result rs = test_api.get(get);
 93         byte[] value = rs.getValue("cf1".getBytes(), "name".getBytes());
 94         System.out.println( Bytes.toString(value));
 95     }
 96 
 97 
 98     @Test//alter table 修改表
 99     public void alterTable() throws IOException {
100         Admin admin = conn.getAdmin();
101         //擷取表原有的結果
102         HTableDescriptor tableDescriptor = admin.getTableDescriptor(table);
103         //擷取所有列簇構成的數組
104         HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
105         //周遊列簇
106         for (HColumnDescriptor columnFamily : columnFamilies) {
107             //擷取列簇名稱
108             String cfName = columnFamily.getNameAsString();
109             //對列簇名為cf1的進行修改
110             if("cf1".equals(cfName)){
111                 //修改TTL
112                 columnFamily.setTimeToLive(100000);
113             }
114         }
115         //修改表結構
116         admin.modifyTable(table,tableDescriptor);
117 
118 
119     }
120 
121     @After
122     public void closed() throws IOException {
123         conn.close();
124     }
125 }