前文已经结束了在本机安装伪分布式的hadoop,这篇文章介绍安装伪分布式的hbase。hbase也是个主从架构,有hmaster和hregionserver,自带有zookeeper。关于具体的概念我不细说,hadoop这一套东西版本的搭配很重要,版本选错了可能会出错,这是我在网上看到的一张图
我就是安装上面的做的,没有出现错误
版本介绍
jdk:1.6(可能这个版本比较低,我用1.7就错误了,换1.6版本没问题)
hadoop:0.20.2
hbase:0.90.3
1:第一步去apache官网下载对应的版本:http://archive.apache.org/dist/hbase/
2:下载后解压,此处有几个文件需要修改一下
hbase-env.sh
export JAVA_HOME=/cygdrive/c/java/jdk1.6
export HBASE_CLASSPATH=/cygdrive/c/cygwin64/hadoop/conf
hbase-site.xml
<property>
<name>hbase.rootdir</name>
<value>hdfs://localhost:9000/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
3:因为hbase是把数据存hdfs,在hbase的lib下有个hadoop的jar包,把这个去掉,再把下载的hadoop核心core包复制进去
4:把hbase放在cygwin安装目录下
5:用cygwin启动服务,先启动hadoop,再启动hbase,关闭则相反
6:可以分别打开hdfs和hbase管理页面,localhost:50070 localhost:60010
可以看到在hdfs上已经有了hbase文件夹
到这里一个简单的伪分布式搭好了,接下来eclipse连接并对habse进行DDL和DML
前提是把hbase里面的jar包导入到工程,maven就写入依赖
package com.qqw.test;
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.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.util.Bytes;
import org.junit.Test;
public class HbaseTest {
//@Test
/**
* 创建表
* @throws Exception
*/
public void test1() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");//通过zk来获取hmaster的主机入口
HBaseAdmin admin = new HBaseAdmin(config);
String table="student";//表名
if(admin.isTableAvailable(table)){
admin.disableTable(table);
admin.deleteTable(table);
}else{
HTableDescriptor hd=new HTableDescriptor(table.getBytes());
HColumnDescriptor cd=new HColumnDescriptor("cf1".getBytes());
hd.addFamily(cd);//表添加列族
admin.createTable(hd);//创建表
}
}
//@Test
/**
* 插入表
* @throws Exception
*/
public void test2() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");//通过zk来获取hmaster的主机入口
HTable table=new HTable(config, "student".getBytes());
String rowkey="18600754234";
Put put=new Put(rowkey.getBytes());
put.add("cf1".getBytes(), "name".getBytes(), Bytes.toBytes("李思思"));
put.add("cf1".getBytes(), "age".getBytes(), "23".getBytes());
put.add("cf1".getBytes(), "addrss".getBytes(), "beijing".getBytes());
table.put(put);
table.close();
}
@Test
/**
* 根据rowkey查询
* @throws Exception
*/
public void test3() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");//通过zk来获取hmaster的主机入口
HTable table=new HTable(config, "student".getBytes());
String rowkey="18600754238";
Get get = new Get(rowkey.getBytes());
Result result = table.get(get);
for (KeyValue kv : result.list()) {
System.out.println("family:" + Bytes.toString(kv.getFamily()));
System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));
System.out.println("value:" + Bytes.toString(kv.getValue()));
System.out.println("Timestamp:" + kv.getTimestamp());
System.out.println("-------------------------------------------");
}
table.close();
}
@Test
/**
* 扫描全表
* @throws Exception
*/
public void test4() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");//通过zk来获取hmaster的主机入口
HTable table=new HTable(config, "student".getBytes());
Scan scan=new Scan();
ResultScanner scanner = table.getScanner(scan);
for(Result r:scanner){
for (KeyValue kv : r.list()) {
System.out.println("family:" + Bytes.toString(kv.getFamily()));
System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));
System.out.println("value:" + Bytes.toString(kv.getValue()));
System.out.println("Timestamp:" + kv.getTimestamp());
System.out.println("-------------------------------------------");
}
}
table.close();
}
@Test
/**
* 查询某列
* @throws Exception
*/
public void test5() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");//通过zk来获取hmaster的主机入口
HTable table=new HTable(config, "student".getBytes());
String rowkey="18600754238";
Get get = new Get(rowkey.getBytes());
get.addColumn("cf1".getBytes(), "addrss".getBytes());
Result result = table.get(get);
for (KeyValue kv : result.list()) {
System.out.println("family:" + Bytes.toString(kv.getFamily()));
System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));
System.out.println("value:" + Bytes.toString(kv.getValue()));
System.out.println("Timestamp:" + kv.getTimestamp());
System.out.println("-------------------------------------------");
}
table.close();
}
@Test
/**
* 更新某列
* @throws Exception
*/
public void test6() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");//通过zk来获取hmaster的主机入口
HTable table=new HTable(config, "student".getBytes());
String rowkey="18600754238";
Put put = new Put(rowkey.getBytes());
put.add(Bytes.toBytes("cf1"), Bytes.toBytes("addrss"),
Bytes.toBytes("南昌"));
table.put(put);
System.out.println("update table Success!");
table.close();
}
/**
* 查询某列数据的多个版本
* @throws Exception
*/
@Test
public void test7() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");//通过zk来获取hmaster的主机入口
HTable table=new HTable(config, "student".getBytes());
String rowkey="18600754238";
Get get = new Get(rowkey.getBytes());
get.addColumn("cf1".getBytes(), "addrss".getBytes());
get.setMaxVersions(5);
Result result = table.get(get);
for (KeyValue kv : result.list()) {
System.out.println("family:" + Bytes.toString(kv.getFamily()));
System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));
System.out.println("value:" + Bytes.toString(kv.getValue()));
System.out.println("Timestamp:" + kv.getTimestamp());
System.out.println("-------------------------------------------");
}
table.close();
}
/*
* 删除指定的列
*
* @tableName 表名
*
* @rowKey rowKey
public static void deleteAllColumn(String tableName, String rowKey)
throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Delete deleteAll = new Delete(Bytes.toBytes(rowKey));
table.delete(deleteAll);
System.out.println("all columns are deleted!");
}
* 删除表
*
* @tableName 表名
public static void deleteTable(String tableName) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName + "is deleted!");
}*/
}
至此hbase简单介绍到此告一段落,建议多看看hbase的构造,比较复杂。