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 }

Jave Hbase AP的更多相关文章

  1. 大数据技术原理与应用【第五讲】NoSQL数据库:5.4 NoSQL的三大基石

    NoSQL的三大基石:cap,Base,最终一致性   5.4.1 cap理论(帽子理论):   consistency:一致性availability:可用性partition tolerance: ...

  2. 分布式系统中的CAP、ACID、BASE概念

    目录 CAP ACID BASE CAP 分布式系统中,这三个特性只能满足其中两个. 一致性(Consistency):分布式中一致性又分强一致性和弱一致性,强一致性主浊任何时刻任何节点看到的数据都是 ...

  3. Redis/HBase/Tair比较

    KV系统对比表 对比维度 Redis Redis Cluster Medis Hbase Tair 访问模式    支持Value大小 理论上不超过1GB(建议不超过1MB) 理论上可配置(默认配置1 ...

  4. HBase 实战(2)--时间序列检索和面检索的应用场景实战

    前言: 作为Hadoop生态系统中重要的一员, HBase作为分布式列式存储, 在线实时处理的特性, 备受瞩目, 将来能在很多应用场景, 取代传统关系型数据库的江湖地位. 本篇主要讲述面向时间序列/面 ...

  5. HBase学习笔记-HBase性能研究(1)

    使用Java API与HBase集群交互时,需要构建HTable对象,使用该对象提供的方法来进行插入/删除/查询等操作.要创建HTable对象,首先要创建一个带有HBase集群信息的配置对象Confi ...

  6. HBase、Redis、MongoDB、Couchbase、LevelDB主流 NoSQL 数据库的对比

    最近小组准备启动一个 node 开源项目,从前端亲和力.大数据下的IO性能.可扩展性几点入手挑选了 NoSql 数据库,但具体使用哪一款产品还需要做一次选型. 我们最终把选项范围缩窄在 HBase.R ...

  7. Hadoop第11周练习—HBase基础知识

    1 :数据即日志 内容 2 :HBase合并过程 内容 3 :HBase一致性 内容 书面作业1:数据即日志 内容 我们常说HBase是“数据即日志”的数据库,它是怎样修改和删除数据的?和Oracle ...

  8. HBase 安装过程记录

    http://blog.csdn.net/chenxingzhen001/article/details/7756129 环境: 操作系统Centos 6.4 32-bit 三台节点 ip       ...

  9. Hbase案例分析(二)

    情景1:

随机推荐

  1. 复习git

    git 常用点,详解 from my typora 文章目录 git 常用点,详解 git 模式解析 删除文件 方式一: 方式二: 远程库 配置忽略文件 查看版本库日志,以及版本回退 解决冲突 替换我 ...

  2. 简陋的Excel到MYSQL的数据传输JAVA实现

    实现从excel读取数据,使用的是jxl.jar(到处都有,请大家随意下载),其中封装好了通过excel提供的接口,对excel中的数据库进行读取的实现: 先为了熟悉其中的方法使用,做了以下的测试: ...

  3. ProjectEuler 004题

    1 #include<iostream> 2 using namespace std; 3 4 int main() { 5 bool isPalindromic (int num); 6 ...

  4. vue+vant实现购物车的全选和反选业务,带你研究购物车的那些细节!

    前言 喜欢购物的小伙伴看过来,你们期待已久的购物车来啦!相信小伙伴逛淘宝时最擅长的就是加入购物车了,那购物车是如何实现商品全选反选的呢?今天就带你们研究购物车的源码,以vue+vant为例. 正文 首 ...

  5. jekins

    上面是:maven配置 git安装: 容器安装: 容器配置与发布: 添加安全认证(如果tomcat没有设置密码这里也不需要设置:) 访问尝试:本地测试前置practice_war的影响 Jekins实 ...

  6. linux上传下载文件(转载https://www.jb51.net/article/143112.htm)

    转载于:https://www.jb51.net/article/143112.htmLinux下目录复制:本机->远程服务器 1 scp -r /home/shaoxiaohu/test1 z ...

  7. 小程序使用 lodash 的问题

    import _ from 'lodash' 报错: vendor.js:11874 Uncaught TypeError: Cannot read property 'prototype' of u ...

  8. 如何获取 Android CPU 核心数 (Java/C++)

    1 前言 最近学习Power HAL方面相关知识,透过Power HAL 去配置CPU的Freq需要先确定 CPU 核数.便先了解如何获取 Android CPU 核数. 2 Java层获取方式 // ...

  9. Python - 面向对象编程 - __init__() 构造方法

    什么是构造方法 在创建类时, 可手动添加一个   __init__() 方法,称为构造方法,这是一个实例方法 构造方法用于创建实例对象时使用,每当创建一个类的实例对象时,Python 解释器都会自动调 ...

  10. Linux 内核:匠心独运之无锁环形队列kfifo

    Linux 内核:匠心独运之无锁环形队列 Kernel version Linux 2.6.12   Author Toney   Email vip_13031075266@163.com   Da ...