1、从零开始在Eclipse上写一个应用Hbase的程序

一、介绍

本文主要介绍如何开发一个使用Hbase的程序,对于Hbase的安装与卸载请参考厦门大学实验室林子雨推荐的Hbase安装教程,在Hbase之前需先搭建Hadoop平台。
安装详情请参考:http://dblab.xmu.edu.cn/blog/install-hbase/
本文将详细介绍Hbase使用流程,若遇到Hbase架构上的问题,可参考博主其他Hbase相关博客。

二、Hbase表结构

如下展示了一个Hbase的表结构,其中Row key是必须的列,其他列可选,下图所示包括了personal列族,professional列族,每个列族下面各自包含了两列。
在这里插入图片描述

三、Hbase Shell

在成功安装Hbase之后,即可进行Hbase Shell操作,我们在linux命令行中输入Hbase shell即可进入Hbase shell界面,其主要涵盖了对表的增删改查,以及表结构的增删改查,以及表的一些权限处理。
在这里插入图片描述
以上为常用的Hbase Shell命令,使用方法如:
put ‘student’,‘1001’,‘course:math’,‘100’
表示在student表中,插入了一个行键为1001,列族为course,列为math,值为100的一个数据。
若列族下没有其他列,那么默认为空即可,如
put ‘student’,‘1001’,‘name’,‘zhangsan’

四、Hbase架构

在讲解如何使用Hbase API之前需先了解Hbase架构。
如下图所示:
在这里插入图片描述
整个Hbase架构将其分为Zookeeper、HMaster、HRegionServer。我们知道,Zookeeper的事件处理能确保整个Hadoop平台只有一个Namenode,在Hbase中,能确保整个Hbase架构只有一个HMaster,察觉HRegionServer联机和宕机,存储访问控制列表等。
一个表按行被拆分成多个HRegin,每个HReginServer维护多个HRegin,每个HRegion中有一个Hlog,用来记录所有对表进行操作的命令,也就意味着如果宕机了,即可通过Hlog进行恢复。每个HRegin又被拆分成多个Store,每个Store对应一个列族,其通过写写入Memeory store,最后flush到HDFS。
更多细节可参考:https://www.cnblogs.com/swordfall/p/8737328.html

Hbase Java API

一个常见的需求是在Eclipse上,远程连接Hbase,对其进行操作。由于Hbase是一个分布式的数据库,通过Zookeeper进行管理和维护,元数据是存放在Zookeeper上的,因此是通过连接Zookeeper的来对Hbase进行操作的。
本实验通过Maven构建项目。
在使用Java API之前,需要先熟悉Maven的使用。

1、在lhbase安装目录下的hbase-site.xml中添加如下内容

<property>
	<name>zookeeper.znode.parent</name>
	<value>/hbase-unsecure</value>
</property>	

2、使用Eclipse构建maven项目,添加pom.xml配置

    <dependency>
    	<groupId>org.apache.hbase</groupId>
    	<artifactId>hbase-client</artifactId>
    	<version>1.2.6</version>
	</dependency>

3、使用Hbase API

package Hbase.StudentManager;

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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
	private static Configuration configuration = null;
	private Connection connection = null;
	private Table table = null;
	private Admin admin = null;
	
	//初始化
    @Before
    public void init() throws Exception{
    	configuration = HBaseConfiguration.create();
        configuration.set("zookeeper.znode.parent","/hbase-unsecure");
    	configuration.set("hbase.zookeeper.quorum", "192.168.80.131");// zookeeper地址
    	configuration.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
    	connection = ConnectionFactory.createConnection(configuration);
    	table = connection.getTable(TableName.valueOf("student"));
    	admin = connection.getAdmin();
    }
    /**********************************表操作***********************************************/
    
    //创建表
   //@Test
    public void createTable() throws Exception{
    	HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("school"));
    	tableDescriptor.addFamily(new HColumnDescriptor("Tname"));//列族名
    	tableDescriptor.addFamily(new HColumnDescriptor("Tage"));
    	admin.createTable(tableDescriptor);
    }
    
    //列出表
    //@Test
    public void showTables() throws Exception{
    	HTableDescriptor [] tableDescriptors = admin.listTables();
    	for (HTableDescriptor hTableDescriptor : tableDescriptors) {
			System.out.println(hTableDescriptor.getNameAsString());
		}
    }
    
    //禁用表
    //@Test
    public void disableTable()throws Exception{
    	if(!admin.isTableDisabled(TableName.valueOf("student"))){
    		admin.disableTable(TableName.valueOf("student"));
    	}
    }
    	
    //启用表
  //  @Test
    public void enableTable() throws Exception{
    	if(!admin.isTableEnabled(TableName.valueOf("student"))){
    		admin.enableTable(TableName.valueOf("student"));
    	}
    }
    
    //添加列族
    //@Test
    public void addColumnFimaly() throws Exception{
    	HColumnDescriptor columnDescriptor = new HColumnDescriptor("courseType");
    	admin.addColumn(TableName.valueOf("teacher"), columnDescriptor);
    }
    
    //删除列族
   // @Test
    public void deleteColumn() throws Exception{
    	admin.deleteColumn(TableName.valueOf("teacher"), Bytes.toBytes("courseType"));
    }
    
    //删除表
    //@Test
    public void deleteTable() throws Exception{
    	TableName tableName = TableName.valueOf("teacher");
    	if(admin.tableExists(tableName)){
    		admin.disableTable(tableName);	//在删除表之前需要先禁用表
    		admin.deleteTable(tableName);
    	}
    }
    
    /**********************************数据操作*********************************************/
    //写入数据
	//@Test
    public void write() throws Exception{
    	Put p = new Put(Bytes.toBytes("95002"));
    	p.addColumn(Bytes.toBytes("Sname"),Bytes.toBytes(""),Bytes.toBytes("lisan"));
    	p.addColumn(Bytes.toBytes("course"),Bytes.toBytes("math"),Bytes.toBytes("120"));
    	table.put(p);
    }
	
    //读取数据
	//@Test
	public void read() throws Exception{
    	Get get = new Get(Bytes.toBytes("95001"));
    	Result result = table.get(get);
        byte [] value = result.getValue(Bytes.toBytes("course"),Bytes.toBytes("math"));
        String math = Bytes.toString(value);
        System.out.println(math);		
	}
	
	//扫描表
	//@Test
	public void scan() throws Exception{	
		Scan scan = new Scan();
		scan.addColumn(Bytes.toBytes("Cname"), Bytes.toBytes(""));
		scan.addColumn(Bytes.toBytes("abc"), Bytes.toBytes("a"));
		ResultScanner scanner = table.getScanner(scan);
		for(Result result = scanner.next();result != null;result=scanner.next()){
			System.out.print("Cname:" + Bytes.toString(result.getValue(Bytes.toBytes("Cname"),Bytes.toBytes(""))) +"\t");
			System.out.println("abc,a:" + Bytes.toString(result.getValue(Bytes.toBytes("abc"),Bytes.toBytes("a"))));
		}
		scanner.close();
	}
	
	//删除数据
	//@Test
	public void delete() throws Exception{
		Delete delete = new Delete(Bytes.toBytes("95002"));
		delete.addColumn(Bytes.toBytes("Sname"), Bytes.toBytes(""));
		delete.addColumn(Bytes.toBytes("course"), Bytes.toBytes("math"));
		//删除一个列族
		//delete.deleteFamily(Bytes.toBytes("Sname"));
		table.delete(delete);
	}
	
	//更新数据
	//@Test
	public void update() throws Exception{
		Put p = new Put(Bytes.toBytes("95001"));
		p.addColumn(Bytes.toBytes("course"), Bytes.toBytes("math"), Bytes.toBytes("150"));
		table.put(p);
	}
    
	//关闭
    @After
    public void close() throws Exception{
    	admin.close();
    	table.close();
    	connection.close();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_25956141/article/details/89513395
今日推荐