一、下载安装包
下载地址见文末
安装路径E:\software\hbase-1.3.1
二、修改配置文件
1、修改:E:\software\hbase-1.3.1\conf\hbase-env.cmd(linux环境是修改hbase-env.sh)
(1)指定java_home,去掉前面的@rem ,把值改成自己的jdk路径
set JAVA_HOME=F:\jdk1.7
(2)(可不改,windows修改此项无效)指定是否使用HBase自带的zk,默认是true
set HBASE_MANAGES_ZK=false
2、修改E:\software\hbase-1.3.1\conf\hbase-site.xml
<!-- 指定hbase是文件存储路径
1、使用本地路径
file:///E:/software/hbase-1.3.1/hbasedata
2、使用hdfs
hdfs://localhost:9000/hbase
使用hdfs集群要把hadoop的hdfs-site.xml和core-site.xml 放到hbase/conf下
如果hdfs是集群,可以配成hdfs://集群名/hbase 如:
hdfs://ns1/hbase
-->
<property>
<name>hbase.rootdir</name>
<value>file:///E:/software/hbase-1.3.1/hbasedata</value>
</property>
<!-- 指定hbase与zk是否是分布式的,linux环境可以用true,
windows环境为true会提示:This is not implemented yet. Stay tuned
-->
<property>
<name>hbase.cluster.distributed</name>
<value>false</value>
</property>
<!-- 指定zk的地址,多个用“,”分割,windows环境指定无效 -->
<property>
<name>hbase.zookeeper.quorum</name>
<value>localhost</value>
</property>
三、启动
如果是用hdfs存储,要先启动hdfs
1、启动hbase
在cmd下cd到E:\software\hbase-1.3.1\bin
执行start-hbase命令
启动完成后,可以用浏览器访问监控页http://localhost:16010/master-status
2、启动hbase客户端
在cmd下cd到E:\software\hbase-1.3.1\bin
执行hbase shell
在客户端输入help
package com.yanhui.yhHbaseDemo;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
*/
public class HBaseTest {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args) throws IOException {
// createTable("t2",new String[]{"cf1","cf2"});
// insterRow("t2", "rw1", "cf1", "q1", "val1");
// getData("t2", "rw1", "cf1", "q1");
scanData("t2", "rw1", "rw2");
// deleRow("t2","rw1","cf1","q1");
// deleteTable("t2");
}
//初始化链接
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","127.0.0.1");
// configuration.set("hbase.zookeeper.quorum","10.10.3.181,10.10.3.182,10.10.3.183");
configuration.set("hbase.zookeeper.property.clientPort","2181");
configuration.set("zookeeper.znode.parent","/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭连接
public static void close(){
try {
if(null != admin)
admin.close();
if(null != connection)
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//建表
public static void createTable(String tableNmae,String[] cols) throws IOException {
init();
TableName tableName = TableName.valueOf(tableNmae);
if(admin.tableExists(tableName)){
System.out.println("talbe is exists!");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String col:cols){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
}
close();
}
//删表
public static void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
}
//查看已有表
public static void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
//插入数据
public static void insterRow(String tableName,String rowkey,String colFamily,String col,String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
table.put(put);
//批量插入
/* List<Put> putList = new ArrayList<Put>();
puts.add(put);
table.put(putList);*/
table.close();
close();
}
//删除数据
public static void deleRow(String tableName,String rowkey,String colFamily,String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowkey));
//删除指定列族
//delete.addFamily(Bytes.toBytes(colFamily));
//删除指定列
//delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
table.delete(delete);
//批量删除
/* List<Delete> deleteList = new ArrayList<Delete>();
deleteList.add(delete);
table.delete(deleteList);*/
table.close();
close();
}
//根据rowkey查找数据
public static void getData(String tableName,String rowkey,String colFamily,String col)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
//获取指定列族数据
//get.addFamily(Bytes.toBytes(colFamily));
//获取指定列数据
//get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
Result result = table.get(get);
showCell(result);
table.close();
close();
}
//格式化输出
public static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
System.out.println("Timetamp:"+cell.getTimestamp()+" ");
System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
}
}
//批量查找数据
public static void scanData(String tableName,String startRow,String stopRow)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
//scan.setStartRow(Bytes.toBytes(startRow));
//scan.setStopRow(Bytes.toBytes(stopRow));
ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
showCell(result);
}
table.close();
close();
}
}
源码及安装包下载地址:http://47.98.237.162/detail/1/171