(官网:http://hbase.apache.org/)
一、伪分布式搭建
(1)下载hbase并解压
tar -zxvf hbase-0.98.12.1-hadoop2-bin.tar.gz -C /opt/jxxy/
(2)配置环境变量
vi /etc/profile
export HBASE_HOME=/opt/jxxy/hbase-0.98.12.1-hadoop2
PATH=$PATH:$HBASE_HOME/bin
./etc/profile使之生效
(3)配置 hbase-env.sh (路径:/opt/jxxy/hbase-0.98.12.1-hadoop2/conf/)
export JAVA_HOME=/usr/java/jdk1.7.0_67
export HBASE_MANAGES_ZK=true
(4)配置 hbase-site.xml(路径:/opt/jxxy/hbase-0.98.12.1-hadoop2/conf/)
<property>
<name>hbase.rootdir</name>
<value>hdfs://node01:9000/hbase</value> //或:file:///home/testuser/hbase
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>node01</value>
</property>
<property>
<name>hbase.master.info.port</name>
<value>60010</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/var/jxxy/zk</value>
</property>
(5)启动HBase
start-hbase.sh //内置zookeeper
jps /查看进程
二、hbase shell常用命令
(1)hbase shell //ctrl+Backspace:后退
hbase(main):001:0>help
hbase(main):001:0>status
hbase(main):001:0>whoami
hbase(main):001:0>list //列出表
hbase(main):001:0>describe 'psn' //查看psn表
hbase(main):001:0>disable 'psn'//使psn表无效
hbase(main):001:0>drop 'psn'//删除psn表
hbase(main):001:0>create //创建表,列出各种用法
hbase(main):001:0>create 'student', 'info'
hbase(main):001:0>describe 'student'
hbase(main):001:0>create 'techer', 'info1','info2'
hbase(main):001:0>put 'student','0001','info:name','xiaohua'//插入数据
hbase(main):001:0>put 'student','0001','info:age','22'
hbase(main):001:0>get 'student','0001' //取数据
hbase(main):001:0>scan 'student'//全表查看,类似于select * from student
hbase(main):001:0>put 'student','0001','info:age','25' //修改age
(2) ctrl+alt+]:退出hbase shell
exit:回到shell
//如果hbase.rootdir设置:file:///home/testuser/hbase,
cd /home/testuser/hbase/data/default/student
ls
1a1bbe7d2cf952cdc97d172136c05ad5 //region名称
//如果hbase.rootdir设置:hdfs://node01:9000/hbase,可以在浏览器node01:9000查看
//在浏览器(node01:60010),查看region名称
cd 1a1bbe7d2cf952cdc97d172136c05ad5
ls
info //列簇
//info里无内容,上面输入的数据还在内存里
(3) hbase(main):001:0>flush 'student' //将内存数据保存到磁盘
hbase hfile -p -f 文件//查看info里面的文件
hbase(main):001:0>delete 'student','0001','info:name' //删除数据
hbase(main):001:0>truncate /删除所有数据
三、Hbase编程
(1)打开eclipse新建一个java 项目
导入hbase包和JUnit:
菜单:window-preferences-java-build path-user libraries
自定义一个jar包(比如hbase_jars)
菜单:add external JARS
选择hbase安装目录\lib里所有jar包,除了ruby
项目里导入hbase_jars包 //右击项目名-build path-configure build path-java build path-libraries-add library-use library-
(2) 新建一个com.jxxy.hbase.HBaseDemo类
(3) 导入配置文件(hbase-site.xml,log4j.properties)
(4)代码
public class HBaseDemo{
HBaseAdmin admin;
Configuration conf;
@Before
public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
conf= HBaseConfiguration.create();
admin=new HBaseAdmin(conf);
}
/**
* 创建表
* @param tableName
* @param fields
* @throws IOException
*/
public void creatTable(String tableName,String[] fields) throws IOException{
//(2)删除已存在的同名表
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
HTableDescriptor desc=new HTableDescriptor(TableName.valueOf(tableName));
for(String family:fields){
HColumnDescriptor cf=new HColumnDescriptor(family.getBytes());
desc.addFamily(cf);
}
admin.createTable(desc);
}
/**
* 向表中指定单元格添加数据
* @param tableName
* @param row
* @param fields
* @param values
* @throws RetriesExhaustedWithDetailsException
* @throws InterruptedIOException
*/
public void addRecord(String tableName,String row,String[] fields,String[] values) throws IOException {
HTable htable=new HTable(conf,tableName.getBytes());
Put put=new Put(row.getBytes());
for(int i=0;i<fields.length;++i){
put.add(fields[i].split(":")[0].getBytes(),fields[i].split(":")[1].getBytes(),values[i].getBytes());
}
htable.put(put);
}
/**
* 浏览表中得某一列数据
* @param tableName
* @param column
* @throws Exception
*/
public void scanColumn(String tableName,String column,String[] fields) throws Exception{
Scan scan=new Scan();
HTable htable=new HTable(conf,tableName.getBytes());
if (column.contains(":")) {
System.out.println("\""+column+"\"");
// 如果参数 column 包含 ":",则按具体列名扫描
String[] parts = column.split(":");
scan.addColumn(Bytes.toBytes(parts[0]), Bytes.toBytes(parts[1]));
try (ResultScanner scanner = htable.getScanner(scan)) {
for (Result r : scanner) {
byte[] value = r.getValue(Bytes.toBytes(parts[0]), Bytes.toBytes(parts[1]));
if (value != null) {
System.out.println(Bytes.toString(value));
} else {
System.out.println("null");
}
}
}
} else {
// 如果参数 column 不包含 ":",则按列族名扫描
scan.addFamily(Bytes.toBytes(column));
for(int i=0;i<fields.length;++i) System.out.print("\""+fields[i]+"\"\t");
System.out.println();
try (ResultScanner scanner = htable.getScanner(scan)) {
for (Result r : scanner) {
for(int i=0;i<fields.length;++i){
byte[] value = r.getValue(Bytes.toBytes(column), Bytes.toBytes(fields[i].split(":")[1]));
if (value != null) {
System.out.print(Bytes.toString(value)+"\t\t");
} else {
System.out.print("null\t\t");
}
}
System.out.println();
}
}
}
System.out.println();
}
/**
* 将关系数据库表装换转换成hbase数据库并插入数据
* @throws IOException
*/
@Test
public void toHbaseTable() throws Exception {
String info[]={"info"};
String score[]={"score"};
creatTable("Student",info);
creatTable("Course",info);
creatTable("SC",score);
String fields0[]={"info:name","info:sex","info:age"};
String fields2[]={"score:Math","score:Computer Science","score:English"};
String fields1[]={"info:name","info:credit"};
addRecord("Student","0001",fields0,new String[]{"xiaoming","male","20"});
addRecord("Student","0002",fields0,new String[]{"xiaohong","female","21"});
addRecord("Course","0001",fields1,new String[]{"Math","2.0"});
addRecord("Course","0002",fields1,new String[]{"Computer Science","2.0"});
addRecord("Course","0003",fields1,new String[]{"English","2.0"});
addRecord("SC","0001",fields2,new String[]{"90","80","70"});
addRecord("SC","0002",new String[]{"score:Math","score:English"},new String[]{"50","90"});
scanColumn("Student","info",fields0);
scanColumn("Course","info:name",fields1);
scanColumn("SC","score",fields2);
}
@After
public void close() throws IOException{
if (admin!=null)
admin.close();
}
}
四、hbase完全分布式
(1)修改 hbase-env.sh
export HBASE_MANAGES_ZK=false
(2)修改 hbase-site.xml
<property>
<name>hbase.rootdir</name>
<value>hdfs://node02:8020/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>node3,node4,node5</value>
</property>
(3) 修改 regionservers
node02
node03
node04
(4) 新建 backup-masters
node05
(5) 复制 hdfs-site.xml到 conf