Hbase的安装与测试

实验环境

虚拟机伪分布式

Ubuntu 17.10

JDK 1.8

Hadoop 2.7.6

Hbase 1.3.3

 

①安装和配置HBase。

首先从官网http://archive.apache.org/dist/hbase/下载HBase安装包,为了兼容性,这里选择HBase-1.3.3。然后解压到文件夹。

②配置环境变量

将hbase下的bin目录添加到path中

编辑~/.bashrc文件添加 export PATH=$PATH:bin路径

 

③伪分布式模式配置

配置hbase/conf/hbase-env.sh

 

配置hbase/conf/hbase-site.xml

假设当前Hadoop集群运行在伪分布式模式下,在本机上运行,且NameNode运行在9000端口。

 

④启动Hadoop

 

启动Hbase

 

⑤根据表格,用Hbase Shell命令模式设计student学生表格。

 

⑥查询zhangsan 的Computer成绩。

 

⑦修改lisi的Math成绩,改为95。

 

⑧根据上面的student表,用Hbase Java API编程

新建一个工程,将hbase/lib中的所有jar包添加到项目

a)    添加数据:English:45 Math:89  Computer:100

 1 import java.io.IOException;
 2 import org.apache.hadoop.conf.Configuration;
 3 import org.apache.hadoop.hbase.HBaseConfiguration;
 4 import org.apache.hadoop.hbase.TableName;
 5 import org.apache.hadoop.hbase.client.Admin;
 6 import org.apache.hadoop.hbase.client.Connection;
 7 import org.apache.hadoop.hbase.client.ConnectionFactory;
 8 import org.apache.hadoop.hbase.client.Put;
 9 import org.apache.hadoop.hbase.client.Table;
10 
11 public class Hbase {
12     public static Configuration configuration;
13     public static Connection connection;
14     public static Admin admin;
15 
16     public static void main(String[] args) {
17         configuration = HBaseConfiguration.create();
18         configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
19         try {
20             connection = ConnectionFactory.createConnection(configuration);
21             admin = connection.getAdmin();
22         } catch (IOException e) {
23             e.printStackTrace();
24         }
25         try {
26             insertRow("student", "scofield", "score", "English", "45");
27             insertRow("student", "scofield", "score", "Math", "89");
28             insertRow("student", "scofield", "score", "Computer", "100");
29         } catch (IOException e) {
30             e.printStackTrace();
31         }
32         close();
33     }
34 
35     public static void insertRow(String tableName, String rowKey, String colFamily, String col, String val)
36             throws IOException {
37         Table table = connection.getTable(TableName.valueOf(tableName));
38         Put put = new Put(rowKey.getBytes());
39         put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
40         table.put(put);
41         table.close();
42     }
43 
44     public static void close() {
45         try {
46             if (admin != null) {
47                 admin.close();
48             }
49             if (null != connection) {
50                 connection.close();
51             }
52         } catch (IOException e) {
53             e.printStackTrace();
54         }
55     }
56 }
Hbase添加数据

b)    获取scofield的English成绩信息

 1 import java.io.IOException;
 2 import org.apache.hadoop.conf.Configuration;
 3 import org.apache.hadoop.hbase.Cell;
 4 import org.apache.hadoop.hbase.CellUtil;
 5 import org.apache.hadoop.hbase.HBaseConfiguration;
 6 import org.apache.hadoop.hbase.TableName;
 7 import org.apache.hadoop.hbase.client.Admin;
 8 import org.apache.hadoop.hbase.client.Connection;
 9 import org.apache.hadoop.hbase.client.ConnectionFactory;
10 import org.apache.hadoop.hbase.client.Get;
11 import org.apache.hadoop.hbase.client.Result;
12 import org.apache.hadoop.hbase.client.Table;
13 
14 public class GetScofieid {
15     public static Configuration configuration;
16     public static Connection connection;
17     public static Admin admin;
18 
19     public static void main(String[] args) {
20         configuration = HBaseConfiguration.create();
21         configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
22         try {
23             connection = ConnectionFactory.createConnection(configuration);
24             admin = connection.getAdmin();
25             getData("student", "scofield", "score", "English");
26         } catch (IOException e) {
27             e.printStackTrace();
28         }
29         close();
30     }
31 
32     public static void getData(String tableName, String rowKey, String colFamily, String col) throws IOException {
33         Table table = connection.getTable(TableName.valueOf(tableName));
34         Get get = new Get(rowKey.getBytes());
35         get.addColumn(colFamily.getBytes(), col.getBytes());
36         Result result = table.get(get);
37         Cell[] cells = result.rawCells();
38         for (Cell cell : cells) {
39             System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)));
40             System.out.println("timestamp:" + cell.getTimestamp());
41             System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)));
42             System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)));
43             System.out.println("value:" + new String(CellUtil.cloneValue(cell)));
44         }
45         table.close();
46     }
47 
48     public static void close() {
49         try {
50             if (admin != null) {
51                 admin.close();
52             }
53             if (null != connection) {
54                 connection.close();
55             }
56         } catch (IOException e) {
57             e.printStackTrace();
58         }
59     }
60 }
Hbase获取数据

⑨关闭所有服务

 

 

posted @ 2019-04-30 14:48  feifei97  阅读(1057)  评论(0编辑  收藏  举报