HBase Java API编程实例

风流意气都作罢 提交于 2019-12-18 03:03:02

在本实例中,首先创建一个学生成绩表 scores,用来存储学生各门课程的考试成绩,然后向 scores 添加数据。

表 scores 的概念视图如图 1 所示,用学生的名字 name 作为行键,年级 grade 是一个只有一个列的列族,score 是一个列族,每一门课程都是 score 的一个列,如 english、math、Chinese 等。score 的列可以随时添加。

例如,后续学生又参加了其他课程的考试,如 computing、physics 等,那么就可以添加到 score 列族。因为每个学生参加考试的课程也会不同,所以,并不一定表中的每一个单元都会有值。在该实例中,要向学生成绩表 scores 中添加的数据如图 2 所示。
 

学生成绩表scores的概念视图
图 1  学生成缋表 scores 的概念视图
 

学生成绩表scores的数据
图 2  学生成绩表 scores 的数据


本节首先对学生成绩表实例的代码框架进行描述,然后详细介绍每一个功能模块的代码细节。


 
  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.*;
  4. import org.apache.hadoop.hbase.client.*;
  5. import org.apache.hadoop.hbase.util.Bytes;
  6. public class StudentScores {
  7. public static Configuration configuration; //HBase 配置信息
  8. public static Connection connection; //HBase 连接
  9. public static void main (String [] agrs) thorws IOException{
  10. init();//建立连接
  11. createTable();//建表
  12. insertData();//添加课程成绩
  13. insertData();//添加课程成绩
  14. insertData();//添加课程成绩
  15. getData();//浏览课程成绩
  16. close();//关闭连接
  17. }
  18.  
  19. public static void init () {......} //建立连接
  20. public static void close () {......} //关闭连接
  21. public static void createTable (){......} //创建表
  22. public static void insertData () {......} //添加课程成绩
  23. public static getData() {……} //浏览操程成绩
  24. }

下面分别对每一个功能模块的代码进行介绍。

1. 建立连接和关闭连接

在使用 HBase 数据库前,必须首先建立连接,通过连接可以获取 Admin 子类,完成对数据库模型的操作。建立连接的代码如下。


 
  1. public static void init () {
  2. configuration = HBaseConfiguration.create();
  3. configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
  4. try{
  5. connection = ConnectionFactory.createConnection(configuration);
  6. admin = connection.getAdmin();
  7. }catch(IOException e){
  8. e.printStackTrace();
  9. }
  10. }

代码中,首先为 configuration 配置对象设置 HBase 数据库的存储路径 hbase.rootdir。如果你对大数据开发感兴趣,想系统学习大数据的话,可以加入大数据技术学习交流扣群:458数字345数字782获取学习资源本实例使用 HDFS 作为 HBase 的底层存储方式,所以在代码中把 configuration 的第二个参数赋值为 hdfs://localhost:9000/hbase。

对 HBase 数据库操作结束之后,需要关闭数据库的连接,具体代码如下。


 
  1. public static void close() {
  2. try{
  3. if(admin != null) {
  4. admin.close();
  5. }
  6. if (null != connection) {
  7. Connection.close();
  8. }
  9. }catch (IOException e) {
  10. e.printStackTrace();
  11. }
  12. }

2. 创建表

创建 HBase 数据库表的时候,首先需要定义表的模型,包括表的名称、行键和列族的名称。具体代码如下。


 
  1. public static void createTable(String myTableName,String[] colFamily) throws IOException {
  2. TableName tableName = TableName.valueOf(myTableName);
  3. If(admin.tableExists(tableName)){
  4. System.out.printIn("table exists!");
  5. } else {
  6. HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
  7. for(String str:colFamily){
  8. HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
  9. hTableDescriptor.addFamily(hColumnDescriptor);
  10. }
  11. admin.createTable(hTableDescriptor);
  12. }
  13. }

调用上述代码创建学生成绩表 scores,需要指定参数 myTableName 为“scores”,colFamily 为 “{"grade","score"}”,即 createTable("scores",{"grade","score"})。

3. 添加数据

为 HBase 数握库表添加数据,需要指定行键、列族、列限定符、时间戳,其中,时间戳可以在添加数据时由系统自动生成。因此,向表里添加数据时,需要提供行键、列族和列限定符及数据值信息,具体代码如下。


 
  1. public static void insertData(String tableName, String rowKey, String colFamily, String col, String val) throws IOException {
  2. Table table = connection.getTable(TableName.valueOf(tableName));
  3. Put put = new Put(rowKey.getBytes());
  4. put.addColumn(colFamily.getBytes(),col.getBytes(),val.getBytes());
  5. table.put(put);
  6. table.close();
  7. }

使用上述代码添加数据时,需要分别为参数 tableName、rowKey、colFamily、col 和 val赋值。例如,要添加图 2 的第一个学生的数据,就需要使用如下 4 行代码。


 
  1. insertData("scores","dandan","grade","","6");
  2. insertData("scores","dandan","score","english","95");
  3. insertData("scores","dandan","score","math","100");
  4. insertData("scores","dandan","score","Chinese","92");

通过以下代码添加第二个学生的数据。


 
  1. insertData("scores","sansan","grade","","6");
  2. insertData("scores","sansan","score","english","87");
  3. insertData("scores","sansan","score","math","95");
  4. insertData("scores","sansan","score","Chinese","98");

4. 浏览数据

在向数据库表添加数据以后,就可以查询表中的数据了。


 
  1. public static void getData (String tableName,String rowKey,String colFamily,String col)throws IOException {
  2.  
  3. Table table = connection.getTable(TableName.valueOf(tableName));
  4. Get get = new Get (rowKey. getBytes()).;
  5. get.addColumn(colFamily.getBytes(),col.getBytes());
  6. Result result = table.get(get);
  7. System.out.printIn(new String(result.getValue(colFamily.getBytes(),
  8. col.getBytes())));
  9. table.close();
  10. }

使用上述代码就可以查询学生课程的成绩,例如,为了查询学生“dandan”的“math”课程的成绩,就可以使用下述代码。

getData("scores","dandan","score","math");

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!