hbase基本概念
hbase:海量数剧,实时查询
hbase即hadoop database,这是一个在HFDS上的分布式数据库。与传统关系型数据库的行数据存储不同,Hbase则是以列的形式存储数据。
RowKey行键 | info:name | info:sex | grade:math |
---|---|---|---|
student1 | row 1 col 2 | row 1 col 3 | row 1 col 4 |
student2 | row 2 col 2 | row 2 col 3 | row 2 col 4 |
student3 | row 3 col 2 | row 3 col 3 | row 3 col 4 |
student4 | row 4 col 2 | row 4 col 3 | row 4 col 4 |
student5 | row 5 col 2 | row 5 col 3 | row 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();
}
}
}