您的位置:首页 > 运维架构 > Linux

Centos6.5里安装Hbase(伪分布式)

2015-08-24 21:16 513 查看
首先我们到官方网站下载Hbase,而我使用的版本是hbase-0.94.27.tar.gz

解压下来:

tar zxvf hbase-0.94.27.tar.gz


寻找java安装路径

[root@localhost conf]# which java
/usr/bin/java


我们找到java的安装路径

/usr/bin/java

所以hbase的配置,如下:

vim hbase-env.sh

export JAVA_HOME=/usr


启动:

[root@localhost bin]# ./start-hbase.sh
starting master, logging to /root/hbase_soft/hbase-0.94.27/bin/../logs/hbase-root-master-localhost.localdomain.out


hbase已经启动了:

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.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.eclipse.jdt.internal.core.BatchOperation;

public class HBaseDBDao {

//定义配置对象HBaseConfiguration
static Configuration conf =null;
static {
//        Configuration configuration = new Configuration();
//        configuration.set("hbase.zookeeper.property.clientPort","2181");
//        configuration.set("hbase.zookeeper.quorum", "192.168.1.112");
//        configuration.set("hbase.master", "192.168.1.112:60000");
//        cfg = new HBaseConfiguration(configuration);
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort","2181");
conf.set("hbase.zookeeper.quorum", "192.168.1.112");
//        conf.set("hbase.master", "192.168.1.112:60000");
}

//创建一张表,指定表名,列族
public static void createTable(String tableName,String columnFarily)throws Exception{
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
System.out.println(tableName+"不存在!");
System.exit(0);
}else{
HTableDescriptor  tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(columnFarily+":"));
System.out.println("创建表成功!");
}
}

//添加数据,通过HTable。和BatchUpdate为已经存在的表添加数据data
//    public static void addData(String tableName,String row,String columnFamily,String column,String data)throws Exception{
//        HTable table = new HTable(cfg,tableName);
//        Put update = new Put(row);
//        update.put(columnFamily+":"+column, data.getBytes());
//        table.commit(update);
//        System.out.println("添加成功!");
//    }

//显示所有数据,通过HTable Scan类获取已有表的信息
public static void getAllData(String tableName)throws Exception{
HTable table = new HTable(conf,tableName);
Scan scan = new Scan();
ResultScanner rs = table.getScanner(scan);
for(Result r:rs){
for(KeyValue kv:r.raw()){
System.out.println(new String(kv.getKey())+new String(kv.getValue()));
}
}
}

//测试函数
public static void main(String[] args){
try{
String tableName = "student";
HBaseDBDao.createTable(tableName, "c1");
//            HBaseDBDao.addData(tableName, "row1", "c1", "1", "this is row 1 column c1:c1");
HBaseDBDao.getAllData(tableName);
}catch(Exception e){
e.printStackTrace();
}
}
}


View Code

参考:
http://hbase.apache.org/book.html http://hbase.apache.org/ http://blog.csdn.net/zwhfyy/article/details/8349788 http://niuzhenxin.iteye.com/blog/1447769 http://abloz.com/hbase/book.html http://www.paul4llen.com/installing-apache-hbase-on-centos-6/ http://cn.soulmachine.me/blog/20140208/ http://blog.csdn.net/wuwenxiang91322/article/details/44684655 http://www.oschina.net/question/54100_24054 http://www.cnblogs.com/heyCoding/archive/2012/11/09/2762334.html http://www.cnblogs.com/zhenjing/p/hbase_example.html http://www.cnblogs.com/panfeng412/archive/2011/08/14/2137984.html http://www.cnblogs.com/caca/p/centos_hadoop_install.html http://www.cnblogs.com/elaron/archive/2013/01/05/2846803.html http://www.cnblogs.com/Dreama/articles/2219190.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: