HBase基础

hbase基本概念

hbase:海量数剧,实时查询
hbase即hadoop database,这是一个在HFDS上的分布式数据库。与传统关系型数据库的行数据存储不同,Hbase则是以列的形式存储数据。

RowKey行键info:nameinfo:sexgrade:math
student1row 1 col 2row 1 col 3row 1 col 4
student2row 2 col 2row 2 col 3row 2 col 4
student3row 3 col 2row 3 col 3row 3 col 4
student4row 4 col 2row 4 col 3row 4 col 4
student5row 5 col 2row 5 col 3row 5 col 4
hbase表结构分析
伪分布模式启动,停止

执行bin下的:

start-hbase.sh
stop-hbase.sh
进入hbase shell
./bin/hbase shell
常用命令

查看所有表:

list

创建表:创建的时候,必须指定表名和列族名

create 'test','cf'

查看表是否存在:

exists 'test'

删除表:

drop 'test'

向表中插入数据:
语法:put ‘表名’,‘行键’,‘列族:列名’,‘值’

put 'test','row1','cf:a','value1'

扫描全表数据:

scan 'test'

获取某一行数据:

get 'test','row1'

禁用表,启用表:

disable 'test'
enable 'test'

清空表中的数据:

truncate 'test'
连接hbase,使用java进行开发

使用phoenix进行连接hbase

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

/**
 * @description 建立Phoenix连接
 * @author cyq
 *
 */
public class PhoenixManager {

	Logger log = Logger.getLogger(this.getClass());
	
	private final static String url = "jdbc:phoenix:10.62.6.131:2181";
	private final static String username = "hbase";
	private final static String password = "";

	private static PhoenixManager instance = null;

	private PhoenixManager() {
		System.setProperty("HADOOP_USER_NAME", "hbase");
	}

	public static PhoenixManager getInstance() {
		if (instance == null) {
			synchronized (PhoenixManager.class) {
				if (instance == null) {
					instance = new PhoenixManager();
				}
			}
		}
		return instance;
	}

	static {
		try {
			Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
		} catch (ClassNotFoundException e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	public Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, username, password);
	}
	
	/** 关闭连接*/
	public void close(Connection con,Statement stmt) {
		try {
			stmt.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值