阅读背景:

Eclipse操作Hbase数据库

来源:互联网 

1、HbaseUtil.java

package com.hbase.test;

import java.io.IOException;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.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;

public class HbaseUtil {
	//加载配置文件(先把hbase-site.xml配置文件放到src目录下)
	static Configuration cfg=HBaseConfiguration.create();
	/**
	 * 一、列出数据库中所有的表
	 * @throws IOException
	 */
	public void list() throws IOException{
		//1、创建数据库连接
		Connection conn=ConnectionFactory.createConnection(cfg);
		//2、获取Admin管理员(Admin用于管理HBase数据库的表信息)
		Admin admin=conn.getAdmin();
		//3、读取数据库中的表信息
		System.out.println("数据库中有以下表:");
		for (TableName name : admin.listTableNames()) {
			System.out.println(name);
		}
		//4、关闭连接
		conn.close();
	}
	/**
	 * 二、创建表
	 * @param tableName
	 * @param familyNames
	 * @throws IOException
	 */
	public void create(String tableName,String... familyNames) throws IOException{
		//1、创建数据库连接
		Connection conn=ConnectionFactory.createConnection(cfg);
		//2、获取Admin管理员(Admin用于管理HBase数据库的表信息)
		Admin admin=conn.getAdmin();
		//3、创建表
		TableName tn=TableName.valueOf(tableName);
		//如果表存在,就先删除
		if(admin.tableExists(tn)){
			admin.disableTable(tn);//先禁用表
			admin.deleteTable(tn);
		}
		//HTableDescriptor包含了表的名字及对应的列族
		//new出新列族
		HTableDescriptor htd=new HTableDescriptor(tn);
		for (String family : familyNames) {
			htd.addFamily(new HColumnDescriptor(family));
		}
		admin.createTable(htd);
		System.out.println(tableName+"表创建成功!");
		//4、关闭连接
		conn.close();		
	}
	/**
	 * 修改表-增加列族
	 * @throws IOException 
	 */
	public void addColumnFamily(String tableName,String... familyNames) throws IOException{
		//1、创建数据库连接
		Connection conn=ConnectionFactory.createConnection(cfg);
		//2、获取Admin管理员(Admin用于管理HBase数据库的表信息)
		Admin admin=conn.getAdmin();
		//获取表
		TableName tn=TableName.valueOf(tableName);
		//已有列族,不需要new,所以先查出来原来有哪些列族
		HTableDescriptor htd= admin.getTableDescriptor(tn);
		for (String family : familyNames) {
			htd.addFamily(new HColumnDescriptor(family));
		}
		admin.modifyTable(tn, htd);
		System.out.println("增加列族成功!");
		//关闭连接
		conn.close();
	}
	/**
	 * 查看表
	 * @param tableName
	 * @throws IOException
	 */
	public void describe(String tableName) throws IOException{
		//1、创建数据库连接
		Connection conn=ConnectionFactory.createConnection(cfg);
		//2、获取Admin管理员(Admin用于管理HBase数据库的表信息)
		Admin admin=conn.getAdmin();
		//获取表
		TableName tn=TableName.valueOf(tableName);
		//已有列族,不需要new,所以先查出来原来有哪些列族
		HTableDescriptor htd= admin.getTableDescriptor(tn);
		System.out.println("要查看的表名:"+tableName);
		System.out.println("它包含的列族分别是:");
		for (HColumnDescriptor hcd : htd.getColumnFamilies()) {
			System.out.println(hcd.getNameAsString());
		}
		//关闭连接
		conn.close();
	}
	/**
	 * 删除列族
	 * @param tableName
	 * @param familyNames
	 * @throws IOException
	 */
	public void removeColumnFamily(String tableName,String... familyNames) throws IOException{
		//1、创建数据库连接
		Connection conn=ConnectionFactory.createConnection(cfg);
		//2、获取Admin管理员(Admin用于管理HBase数据库的表信息)
		Admin admin=conn.getAdmin();
		//获取表
		TableName tn=TableName.valueOf(tableName);
		//已有列族,不需要new,所以先查出来原来有哪些列族
		HTableDescriptor htd= admin.getTableDescriptor(tn);
		for (String family : familyNames) {
			htd.removeFamily(Bytes.toBytes(family));//删除指定列族
		}
		admin.modifyTable(tn, htd);
		System.out.println("删除列族成功!");
		//关闭连接
		conn.close();
	}
	/**
	 * 新增数据
	 * @throws IOException 
	 */
	public void put(String tableName,String... content) throws IOException{
		//1、创建数据库连接
		Connection conn=ConnectionFactory.createConnection(cfg);
		//获取表
		Table tb=conn.getTable(TableName.valueOf(tableName));
		for (int i = 0; i < content.length; i+=4) {
			String hangjian=content[i];//行键
			String liezu=content[i+1];//列族
			String liexiushifu=content[i+2];//列修饰符
			String liezhi=content[i+3];//列值
			//设置行键
			Put p=new Put(Bytes.toBytes(hangjian));
			//向行中添加数据
			p.addColumn(Bytes.toBytes(liezu), Bytes.toBytes(liexiushifu), Bytes.toBytes(liezhi));
			//将行加入表中
			tb.put(p);
		}
		//关闭连接
		conn.close();
	}
/**
 * 获取数据
 * @param tableName 表名
 * @param rowkey 行键
 * @param family 列族
 * @param qualifier 列修饰符
 * @throws IOException
 */
	public void get(String tableName,String rowkey,String family,String qualifier) throws IOException{
		//1、创建数据库连接
		Connection conn=ConnectionFactory.createConnection(cfg);
		//获取表
		Table tb=conn.getTable(TableName.valueOf(tableName));
		Get g=new Get(Bytes.toBytes(rowkey));
		Result result=tb.get(g);
		byte[] buf= result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier));
		System.out.println("列值为:"+Bytes.toString(buf));
		//关闭连接
		conn.close();
	}
	/**
	 * 全表扫描
	 * @param tableName
	 * @throws IOException 
	 */
	public void scan(String tableName) throws IOException{
		//1、创建数据库连接
		Connection conn=ConnectionFactory.createConnection(cfg);
		//获取表
		Table tb=conn.getTable(TableName.valueOf(tableName));
		//扫描器
		ResultScanner scanner=tb.getScanner(new Scan());
		System.out.println("行键\t列族\t列修饰符\t列值");
		for (Result row : scanner) {
			//获取所有的单元格
			List<Cell> cellList= row.listCells();	
			
			for (Cell cell : cellList) {
				System.out.print(Bytes.toString(cell.getRow()));
				System.out.print("\t");
				System.out.print(Bytes.toString(cell.getFamily()));
				System.out.print("\t");
				System.out.print(Bytes.toString(cell.getQualifier()));
				System.out.print("\t");
				System.out.println(Bytes.toString(cell.getValue()));		
			}
			
		}	
		//关闭连接
		conn.close();
	}
}
package com.hbase.test;

imp



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: