Jave Hbase API

Hbase API 类和数据模型的对应关系

 

 

 

HBaseAdmin

 

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

作用:提供了一个接口来管理 HBase 数据库的表信息。它提供的方法包括:创建表,删 除表,列出表项,使表有效或无效,以及添加或删除表列族成员等。

 

 

 

 

HBaseConfiguration

 

类:org.apache.hadoop.hbase.HBaseConfiguration

作用:对 HBase 进行配置

 

 

 

HTableDescriptor

 

类: org.apache.hadoop.hbase.HTableDescriptor

作用:包含了表的名字极其对应表的列族

 

 

 

 

 

HColumnDescriptor

 

 

类: org.apache.hadoop.hbase.HColumnDescriptor

作用:维护着关于列族的信息,例如版本号,压缩设置等。它通常在创建表或者为表添 加列族的时候使用。列族被创建后不能直接修改,只能通过删除然后重新创建的方式。 列族被删除的时候,列族里面的数据也会同时被删除。

 

 

 

 

 

 

HTable

 

 

 

 

 

 

 

 

Put

 

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

作用:用来对单个行执行添加操作

 

 

 

 

 

 

Get

 

 

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

作用:用来获取单个行的相关信息

 

 

 

 

Result

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

作用:存储 Get 或者 Scan 操作后获取表的单行值。使用此类提供的方法可以直接获取值 或者各种 Map 结构( key-value 对)

 

 

创建表

 

 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 }

 

posted @ 2021-10-12 22:46  lmandcc  阅读(127)  评论(0编辑  收藏  举报