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. 初始C3P0连接池

    C3P0连接池只需要一个jar包: 其中我们可以看到有三个jar包: 属于C3P0的jar包只有一个,另外两个是测试时使用的JDBC驱动:一个是mysql的,一个是oracle的: 可以看到在src下 ...

  2. MongoDB学习笔记三 - MongooseAPI操作数据

    在上一篇我们讲了如何通过Mongoose想数据库动态添加数据, 接下来我们一起来看一下如何通过Mongoose来对数据库进行增删改查等一系列操作 Model 对象的方法 remove(cinditio ...

  3. mac下编译安装grafana

    下载grafana源码 从grafana git 仓库下载指定的分支. 编译后端 我下载的时候,grafana的最新release是7.3.7,其需要安装go 1.15版本 生成可执行文件 进入项目根 ...

  4. seo高手教你seo优化排名该怎么做

    seo高手教你seo优化排名该怎么做 第一节:如何在本地搭建服务器环境 本节课程主要是讲如何利用 Xampp在本地搭建服务器环境 .网站使用asp和php比较常见,当然,就目前而言,使用php搭建网站 ...

  5. 手撕LRU缓存了解一下

    面试官:来了,老弟,LRU缓存实现一下? 我:直接LinkedHashMap就好了. 面试官:不要用现有的实现,自己实现一个. 我:..... 面试官:回去等消息吧.... 大家好,我是程序员学长,今 ...

  6. vue-父子组件之传值和单项数据流问题

    前言 我们知道 vue 中父子组件的核心概念是单项数据流问题,props 是单项传递的.那究竟什么是单项数据流问题,这篇文章来总结一下关于这个知识点的学习笔记. 正文 1.父组件传值给子组件 < ...

  7. djangoProject default codes

    1 === 2 settings.py 3 === 4 """ 5 Django settings for djangoProject project. 6 7 Gene ...

  8. poll?transport=longpoll&connection...烦人的请求c

    1.问题描述: 最近使用miniui做了一个后台管理系统,打开浏览器调试时,总发现一堆无关的请求,结构大致是:poll?transport=longpoll&connection.....一直 ...

  9. 线程调用BeginInvoke

    线程异步调用 Thread objThread = new Thread(new ThreadStart(delegate             {                 Dispatch ...

  10. WPF 过渡效果

    http://blog.csdn.net/lhx527099095/article/details/8005095 先上张效果图看看 如果不如您的法眼 可以移步了 或者有更好的效果 可以留言给我 废话 ...