当前位置:文档之家› HBase原理及实例

HBase原理及实例

HBase原理及实例
HBase原理及实例

Hbase与eclipse集成的第一个例子

1.

2.

3.hbase.rootdir

4.hdfs://localhost:9000/hbase

5.

6.

网上还有这一步,但我没执行这一步,结果也正确显示出来了。

HBase提供了java api来对HBase进行一系列的管理涉及到对表的管理、数据的操作等。常用的API操作有:

1、对表的创建、删除、显示以及修改等,可以用HBaseAdmin,一旦创建了表,那么可以通过HTable的实例来访问表,每次可以往表里增加数据。

2、插入数据

创建一个Put对象,在这个Put对象里可以指定要给哪个列增加数据,以及当前的时间戳等值,然后通过调用HTable.put(Put)来提交操作,子猴在这里提请注意的是:在创建Put对象的时候,你必须指定一个行(Row)值,在构造Put对象的时候作为参数传入。

3、获取数据

要获取数据,使用Get对象,Get对象同Put对象一样有好几个构造函数,通常在构造的时候传入行值,表示取第几行的数据,通过HTable.get(Get)来调用。

4、浏览每一行

通过Scan可以对表中的行进行浏览,得到每一行的信息,比如列名,时间戳等,Scan相当于一个游标,通过next()来浏览下一个,通过调用HTable.getScanner(Scan)来返回一个ResultScanner对象。HTable.get(Get)和HTable.getScanner(Scan)都是返回一个Result。Result是一个

KeyValue的链表。

5、删除

使用Delete来删除记录,通过调用HTable.delete(Delete)来执行删除操作。(注:删除这里有些特别,也就是删除并不是马上将数据从表中删除。)

6、锁

新增、获取、删除在操作过程中会对所操作的行加一个锁,而浏览却不会。

7、簇的访问

客户端代码通过ZooKeeper来访问找到簇,也就是说ZooKeeper quorum将被使用,那么相关的类(包)应该在客户端的类(classes)目录下,即客户端一定要找到文件hbase-site.xml。

新建一个类:

[java]view plaincopyprint?

1.import java.io.IOException;

2.import java.util.ArrayList;

3.import java.util.List;

4.

5.import org.apache.hadoop.conf.Configuration;

6.import org.apache.hadoop.hbase.HBaseConfiguration;

7.import org.apache.hadoop.hbase.HColumnDescriptor;

8.import org.apache.hadoop.hbase.HTableDescriptor;

9.import org.apache.hadoop.hbase.KeyValue;

10.import org.apache.hadoop.hbase.MasterNotRunningException;

11.import org.apache.hadoop.hbase.ZooKeeperConnectionException;

12.import org.apache.hadoop.hbase.client.Delete;

13.import org.apache.hadoop.hbase.client.Get;

14.import org.apache.hadoop.hbase.client.HBaseAdmin;

15.import org.apache.hadoop.hbase.client.HTable;

16.import org.apache.hadoop.hbase.client.Result;

17.import org.apache.hadoop.hbase.client.ResultScanner;

18.import org.apache.hadoop.hbase.client.Scan;

19.import org.apache.hadoop.hbase.client.Put;

20.import org.apache.hadoop.hbase.util.Bytes;

21.

22.public class HBaseTest {

23.

24.private static Configuration conf =null;

25./**

26. * 初始化配置

27. */

28.static {

29. conf = HBaseConfiguration.create();

30. }

31.

32./**

33. * 创建一张表

34. */

35.public static void creatTable(String tableName, String[] familys) throws

Exception {

36. HBaseAdmin admin = new HBaseAdmin(conf);

37.if (admin.tableExists(tableName)) {

38. System.out.println("table already exists!");

39. } else {

40. HTableDescriptor tableDesc = new HTableDescriptor(tableName);

41.for(int i=0; i

42. tableDesc.addFamily(new HColumnDescriptor(familys[i]));

43. }

44. admin.createTable(tableDesc);

45. System.out.println("create table " + tableName + " ok.");

46. }

47. }

48.

49./**

50. * 删除表

51. */

52.public static void deleteTable(String tableName) throws Exception {

53.try {

54. HBaseAdmin admin = new HBaseAdmin(conf);

55. admin.disableTable(tableName);

56. admin.deleteTable(tableName);

57. System.out.println("delete table " + tableName + " ok.");

58. } catch (MasterNotRunningException e) {

59. e.printStackTrace();

60. } catch (ZooKeeperConnectionException e) {

61. e.printStackTrace();

62. }

63. }

64.

65./**

66. * 插入一行记录

67. */

68.public static void addRecord (String tableName, String rowKey, String fa

mily, String qualifier, String value)

69.throws Exception{

70.try {

71. HTable table = new HTable(conf, tableName);

72. Put put = new Put(Bytes.toBytes(rowKey));

73. put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toB

ytes(value));

74. table.put(put);

75. System.out.println("insert recored " + rowKey + " to table " + t

ableName +" ok.");

76. } catch (IOException e) {

77. e.printStackTrace();

78. }

79. }

80.

81./**

82. * 删除一行记录

83. */

84.public static void delRecord (String tableName, String rowKey) throws IO

Exception{

85. HTable table = new HTable(conf, tableName);

86. List list = new ArrayList();

87. Delete del = new Delete(rowKey.getBytes());

88. list.add(del);

89. table.delete(list);

90. System.out.println("del recored " + rowKey + " ok.");

91. }

92.

93./**

94. * 查找一行记录

95. */

96.public static void getOneRecord (String tableName, String rowKey) throws

IOException{

97. HTable table = new HTable(conf, tableName);

98. Get get = new Get(rowKey.getBytes());

99. Result rs = table.get(get);

100.for(KeyValue kv : rs.raw()){

101. System.out.print(new String(kv.getRow()) + " " );

102. System.out.print(new String(kv.getFamily()) + ":" ); 103. System.out.print(new String(kv.getQualifier()) + " " ); 104. System.out.print(kv.getTimestamp() + " " );

105. System.out.println(new String(kv.getValue()));

106. }

107. }

108.

109./**

110. * 显示所有数据

111. */

112.public static void getAllRecord (String tableName) {

113.try{

114. HTable table = new HTable(conf, tableName);

115. Scan s = new Scan();

116. ResultScanner ss = table.getScanner(s);

117.for(Result r:ss){

118.for(KeyValue kv : r.raw()){

119. System.out.print(new String(kv.getRow()) + " "); 120. System.out.print(new String(kv.getFamily()) + ":");

121. System.out.print(new String(kv.getQualifier()) + " ");

122. System.out.print(kv.getTimestamp() + " ");

123. System.out.println(new String(kv.getValue())); 124. }

125. }

126. } catch (IOException e){

127. e.printStackTrace();

128. }

129. }

130.

131.public static void main (String [] agrs) {

132.try {

133. String tablename = "scores";

134. String[] familys = {"grade", "course"};

135. HBaseTest.creatTable(tablename, familys);

136.

137.//add record zkb

138. HBaseTest.addRecord(tablename,"zkb","grade","","5"); 139. HBaseTest.addRecord(tablename,"zkb","course","","90"); 140. HBaseTest.addRecord(tablename,"zkb","course","math","97");

141. HBaseTest.addRecord(tablename,"zkb","course","art","87");

142.//add record baoniu

143. HBaseTest.addRecord(tablename,"baoniu","grade","","4");

144. HBaseTest.addRecord(tablename,"baoniu","course","math","89");

145.

146. System.out.println("===========get one record========");

147. HBaseTest.getOneRecord(tablename, "zkb");

148.

149. System.out.println("===========show all record========");

150. HBaseTest.getAllRecord(tablename);

151.

152. System.out.println("===========del one record========");

153. HBaseTest.delRecord(tablename, "baoniu");

154. HBaseTest.getAllRecord(tablename);

155.

156. System.out.println("===========show all record========");

157. HBaseTest.getAllRecord(tablename);

158. } catch (Exception e) {

159. e.printStackTrace();

160. }

161. }

162.}

3. 结果显示为:

create table scores ok.

insert recored zkb to table scores ok.

insert recored zkb to table scores ok.

insert recored zkb to table scores ok.

insert recored zkb to table scores ok.

insert recored baoniu to table scores ok.

insert recored baoniu to table scores ok.

===========get one record========

zkb course: 1345450733304 90

zkb course:art 1345450733323 87

zkb course:math 1345450733316 97

zkb grade: 1345450733294 5

===========show all record========

baoniu course:math 1345450733333 89

baoniu grade: 1345450733328 4

zkb course: 1345450733304 90

zkb course:art 1345450733323 87

zkb course:math 1345450733316 97

zkb grade: 1345450733294 5

===========del one record========

del recored baoniu ok.

zkb course: 1345450733304 90

zkb course:art 1345450733323 87

zkb course:math 1345450733316 97

zkb grade: 1345450733294 5

===========show all record========

zkb course: 1345450733304 90

zkb course:art 1345450733323 87

zkb course:math 1345450733316 97

zkb grade: 1345450733294 5

我的HBase之旅

博客分类:

云计算

HBaseHadoopApacheOSSocket

最近在学hbase,看了好多资料,具体的参考:

https://www.doczj.com/doc/7d15947922.html,/u3/102568/article_144792.html

https://www.doczj.com/doc/7d15947922.html,/dajuezhao/category/724896.aspx

https://www.doczj.com/doc/7d15947922.html,/article/apache-hbase-shell-and-install-key-value.html 以上三个里面的所有资料都看了,相信你就知道一定的hbase概念了。

好了,现在讲讲我的配置环境:

cygwin + hadoop-0.20.2 + zookeeper-3.3.2 + hbase-0.20.6 (+ eclipse3.6)

具体的配置细节,这里不讲了,网上很多,只要细心就没问题。

假设都配置好了,那么启动这些服务吧,据说启动顺序也是有要求的:1,hadoop ./start-all.sh

2,zookeeper ./zkServer.sh start

3,hbase ./start-hbase.sh

停止的时候也是有顺序的,hbase--zookeeper--hadoop

成功后的界面截图:

http://localhost:60010/master.jsp 【hbase的管理信息】

下面就写java代码来操作hbase,我写了简单的增删改查:

相关主题
文本预览
相关文档 最新文档