hbase学习(一)hbase单机部署和java客户端连接单机hbase

简介:

最近看了些hbase的东西,打算写点什么,谁知鼓捣的过程中步步是坑,最终呕心沥血,憋出了这篇文章,实属不易。

hbase和hive总是成对出现的,简单说,hbase是数据库,hive是mapReduce作业。

先从hbase单机部署说起,尽量说的简洁点,说多了都是眼泪。

1.给服务器起个机器名,iptables关掉,同时本地绑定host。

起个机器名

1

vi /etc/hosts

127.0.0.1 localhost
211.155.225.210 love-kaige

1

vi /etc/sysconfig/network

NETWORKING=yes
HOSTNAME=love-kaige

关闭iptables

1

2

service iptables stop

chkconfig iptables off

本地绑定host
C:\Windows\System32\drivers\etc\hosts
211.155.225.210 love-kaige

然后reboot重启机子,执行
hostname显示love-kaige,service iptables status 显示 iptables: Firewall is not running. 即OK。

2.下载安装jdk和hbase。

jdk应该关系不大,我安装的是jdk-7u51-linux-x64.rpm,环境变量配好即可,此处略过。

hbase下载的是稳定版,地址:http://mirrors.aliyun.com/apache/hbase/stable/hbase-0.94.18.tar.gz。阿里云对apache下的项目和linux不同的发行版都做了镜像,方便了广大的码农,给个赞。

解压hbase,然后对hbase-site.xml进行修改,修改如下:

1

2

3

4

5

6

<configuration>

<property>

<name>hbase.rootdir</name>

<value>file:/root/hbase</value>

</property>

</configuration>

然后去hbase的bin目录,./start-hbase.sh起起来。

3.编写java代码。

添加依赖:

1

2

3

4

5

6

7

8

9

10

<dependency>

<groupId> org.apache.hadoop</groupId >

<artifactId> hadoop-core </artifactId>

<version> 1.0.4</version >

</dependency>

<dependency>

<groupId> org.apache.hbase</groupId >

<artifactId> hbase</artifactId >

<version> 0.94.18</version >

</dependency>

服务端和客户端的版本最好一致,现在都是0.94.18,免得出现莫名奇妙的问题。hadoop的版本和hbase的版本也有对应关系,把官网的hbase和hadoop的版本匹配表搬过来,

Table 2.1. Hadoop version support matrix

HBase-0.92.x HBase-0.94.x HBase-0.96
Hadoop-0.20.205 S X X
Hadoop-0.22.x S X X
Hadoop-1.0.x S S S
Hadoop-1.1.x NT S S
Hadoop-0.23.x X S NT
Hadoop-2.x X S S

S = supported and tested,支持
X = not supported,不支持
NT = not tested enough.可以运行但测试不充分

由于 HBase 依赖 Hadoop,它配套发布了一个Hadoop jar 文件在它的 lib 下。该套装jar仅用于独立模式。在分布式模式下,Hadoop版本必须和HBase下的版本一致。用你运行的分布式Hadoop版本jar文件替换HBase lib目录下的Hadoop jar文件,以避免版本不匹配问题。确认替换了集群中所有HBase下的jar文件。Hadoop版本不匹配问题有不同表现,但看起来都像挂掉了。

贴代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

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;

/**

*

* @author yankai913@gmail.com

* @date 2014-4-28

*/

public class SimpleClient {

static final String rowKey = "row1";

static HBaseAdmin hBaseAdmin;

static Configuration conf;

static {

conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum", "love-kaige");

try {

hBaseAdmin = new HBaseAdmin(conf);

}

catch (MasterNotRunningException e) {

e.printStackTrace();

}

catch (ZooKeeperConnectionException e) {

e.printStackTrace();

}

}

public static void createTable(String tableName, String[] columns) throws Exception {

dropTable(tableName);

HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

for (String columnName : columns) {

HColumnDescriptor column = new HColumnDescriptor(columnName);

hTableDescriptor.addFamily(column);

}

hBaseAdmin.createTable(hTableDescriptor);

System.out.println("create table successed");

}

public static void dropTable(String tableName) throws Exception {

if (hBaseAdmin.tableExists(tableName)) {

hBaseAdmin.disableTable(tableName);

hBaseAdmin.deleteTable(tableName);

}

System.out.println("drop table successed");

}

public static HTable getHTable(String tableName) throws Exception {

return new HTable(conf, tableName);

}

public static void insert(String tableName, Map<String, String> map) throws Exception {

HTable hTable = getHTable(tableName);

byte[] row1 = Bytes.toBytes(rowKey);

Put p1 = new Put(row1);

for (String columnName : map.keySet()) {

byte[] value = Bytes.toBytes(map.get(columnName));

String[] str = columnName.split(":");

byte[] family = Bytes.toBytes(str[0]);

byte[] qualifier = null;

if (str.length > 1) {

qualifier = Bytes.toBytes(str[1]);

}

p1.add(family, qualifier, value);

}

hTable.put(p1);

Get g1 = new Get(row1);

Result result = hTable.get(g1);

System.out.println("Get: " + result);

System.out.println("insert successed");

}

public static void delete(String tableName, String rowKey) throws Exception {

HTable hTable = getHTable(tableName);

List<Delete> list = new ArrayList<Delete>();

Delete d1 = new Delete(Bytes.toBytes(rowKey));

list.add(d1);

hTable.delete(list);

Get g1 = new Get(Bytes.toBytes(rowKey));

Result result = hTable.get(g1);

System.out.println("Get: " + result);

System.out.println("delete successed");

}

public static void selectOne(String tableName, String rowKey) throws Exception {

HTable hTable = getHTable(tableName);

Get g1 = new Get(Bytes.toBytes(rowKey));

Result result = hTable.get(g1);

foreach(result);

System.out.println("selectOne end");

}

private static void foreach(Result result) throws Exception {

for (KeyValue keyValue : result.raw()) {

StringBuilder sb = new StringBuilder();

sb.append(Bytes.toString(keyValue.getRow())).append("\t");

sb.append(Bytes.toString(keyValue.getFamily())).append("\t");

sb.append(Bytes.toString(keyValue.getQualifier())).append("\t");

sb.append(keyValue.getTimestamp()).append("\t");

sb.append(Bytes.toString(keyValue.getValue())).append("\t");

System.out.println(sb.toString());

}

}

public static void selectAll(String tableName) throws Exception {

HTable hTable = getHTable(tableName);

Scan scan = new Scan();

ResultScanner resultScanner = null;

try {

resultScanner = hTable.getScanner(scan);

for (Result result : resultScanner) {

foreach(result);

}

}

catch (Exception e) {

e.printStackTrace();

}

finally {

if (resultScanner != null) {

resultScanner.close();

}

}

System.out.println("selectAll end");

}

public static void main(String[] args) throws Exception {

String tableName = "tableTest";

String[] columns = new String[] { "column_A", "column_B" };

createTable(tableName, columns);

Map<String, String> map = new HashMap<String, String>();

map.put("column_A", "AAA");

map.put("column_B:1", "b1");

map.put("column_B:2", "b2");

insert(tableName, map);

selectOne(tableName, rowKey);

selectAll(tableName);

delete(tableName, rowKey);

dropTable(tableName);

}

练习代码看这里


相关实践学习
lindorm多模间数据无缝流转
展现了Lindorm多模融合能力——用kafka API写入,无缝流转在各引擎内进行数据存储和计算的实验。
云数据库HBase版使用教程
&nbsp; 相关的阿里云产品:云数据库 HBase 版 面向大数据领域的一站式NoSQL服务,100%兼容开源HBase并深度扩展,支持海量数据下的实时存储、高并发吞吐、轻SQL分析、全文检索、时序时空查询等能力,是风控、推荐、广告、物联网、车联网、Feeds流、数据大屏等场景首选数据库,是为淘宝、支付宝、菜鸟等众多阿里核心业务提供关键支撑的数据库。 了解产品详情:&nbsp;https://cn.aliyun.com/product/hbase &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
1月前
|
分布式计算 Java Hadoop
java使用hbase、hadoop报错举例
java使用hbase、hadoop报错举例
68 4
|
11天前
|
存储 大数据 关系型数据库
HBase系列学习:基础知识
HBase系列学习:基础知识
HBase系列学习:基础知识
|
14天前
|
分布式计算 Java Hadoop
Hadoop-30 ZooKeeper集群 JavaAPI 客户端 POM Java操作ZK 监听节点 监听数据变化 创建节点 删除节点
Hadoop-30 ZooKeeper集群 JavaAPI 客户端 POM Java操作ZK 监听节点 监听数据变化 创建节点 删除节点
36 1
|
1月前
|
IDE Java 分布式数据库
Apache HBase 落地JAVA 实战
Apache HBase 落地 Java 实战主要涉及使用 Java API 来操作 HBase 数据库,包括表的创建、删除、数据的插入、查询等操作。以下是一个基于 Java 的 HBase 实战指南,包括关键步骤和示例代码。
129 23
|
29天前
|
缓存 Java Linux
java操作hbase报错:KeeperErrorCode=NoNode for /hbase-unsecure/master
java操作hbase报错:KeeperErrorCode=NoNode for /hbase-unsecure/master
75 2
|
1月前
|
JSON NoSQL Java
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
这篇文章介绍了在Java中使用Redis客户端的几种方法,包括Jedis、SpringDataRedis和SpringBoot整合Redis的操作。文章详细解释了Jedis的基本使用步骤,Jedis连接池的创建和使用,以及在SpringBoot项目中如何配置和使用RedisTemplate和StringRedisTemplate。此外,还探讨了RedisTemplate序列化的两种实践方案,包括默认的JDK序列化和自定义的JSON序列化,以及StringRedisTemplate的使用,它要求键和值都必须是String类型。
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
|
2月前
|
Java
Java使用FileInputStream&&FileOutputStream模拟客户端向服务器端上传文件(单线程)
Java使用FileInputStream&&FileOutputStream模拟客户端向服务器端上传文件(单线程)
72 1
|
2月前
|
缓存 监控 Java
"Java垃圾回收太耗时?阿里HBase GC优化秘籍大公开,让你的应用性能飙升90%!"
【8月更文挑战第17天】阿里巴巴在HBase实践中成功将Java垃圾回收(GC)时间降低90%。通过选用G1垃圾回收器、精细调整JVM参数(如设置堆大小、目标停顿时间等)、优化代码减少内存分配(如使用对象池和缓存),并利用监控工具分析GC行为,有效缓解了高并发大数据场景下的性能瓶颈,极大提升了系统运行效率。
62 4
|
2月前
|
Java
Java模拟文件发送给服务器,服务器将文件转发给其他用户,并保存到服务器本地,其他用户可以接收,并保存到本地磁盘,支持各种文件格式,并解决通信中服务器怎么区分客户端发来的文件类型
Java模拟文件发送给服务器,服务器将文件转发给其他用户,并保存到服务器本地,其他用户可以接收,并保存到本地磁盘,支持各种文件格式,并解决通信中服务器怎么区分客户端发来的文件类型
|
3月前
|
大数据 分布式数据库 Hbase
Hbase学习三:Hbase常用命令总结
Hbase学习三:Hbase常用命令总结
345 0