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

时间:2023-11-10 12:11:44

首先我们到官方网站下载Hbase,而我使用的版本是hbase-0.94.27.tar.gz

解压下来:

tar zxvf hbase-.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-/bin/../logs/hbase-root-master-localhost.localdomain.out

hbase已经启动了:

[root@localhost bin]# ps ax | grep hbase
  pts/    Sl     : /usr/bin/java -XX:OnOutOfMemoryError=kill - %p -Xmx1000m -XX:+UseConcMarkSweepGC -Dhbase.log.dir=/root/hbase_soft/hbase-/bin/../logs -Dhbase.log.file=hbase-root-master-localhost.localdomain.log -Dhbase.home.dir=/root/hbase_soft/hbase-/bin/.. -Dhbase.id.str=root -Dhbase.root.logger=INFO,DRFA -Djava.library.path=/root/hbase_soft/hbase-/bin/../lib/native/Linux-amd64- -Dhbase.security.logger=INFO,DRFAS -classpath /root/hbase_soft/hbase-/bin/../conf:/usr/lib/tools.jar:/root/hbase_soft/hbase-/bin/..:/root/hbase_soft/hbase-/bin/../hbase-.jar:/root/hbase_soft/hbase-/bin/../hbase--tests.jar:/root/hbase_soft/hbase-/bin/../lib/activation-/bin/../lib/asm-/bin/../lib/avro-.jar:/root/hbase_soft/hbase-/bin/../lib/avro-ipc-.jar:/root/hbase_soft/hbase-/bin/../lib/commons-beanutils-.jar:/root/hbase_soft/hbase-/bin/../lib/commons-beanutils-core-.jar:/root/hbase_soft/hbase-/bin/../lib/commons-cli-/bin/../lib/commons-codec-/bin/../lib/commons-collections-.jar:/root/hbase_soft/hbase-/bin/../lib/commons-configuration-/bin/../lib/commons-digester-/bin/../lib/commons-el-/bin/../lib/commons-httpclient-/bin/../lib/commons-io-/bin/../lib/commons-lang-/bin/../lib/commons-logging-.jar:/root/hbase_soft/hbase-/bin/../lib/commons-math-/bin/../lib/commons-net-.jar:/root/hbase_soft/hbase-/bin/../lib/core-.jar:/root/hbase_soft/hbase-/bin/../lib/guava-.jar:/root/hbase_soft/hbase-/bin/../lib/hadoop-core-.jar:/root/hbase_soft/hbase-/bin/../lib/hamcrest-core-/bin/../lib/high-scale-lib-.jar:/root/hbase_soft/hbase-/bin/../lib/httpclient-.jar:/root/hbase_soft/hbase-/bin/../lib/httpcore-.jar:/root/hbase_soft/hbase-/bin/../lib/jackson-core-asl-.jar:/root/hbase_soft/hbase-/bin/../lib/jackson-jaxrs-.jar:/root/hbase_soft/hbase-/bin/../lib/jackson-mapper-asl-.jar:/root/hbase_soft/hbase-/bin/../lib/jackson-xc-.jar:/root/hbase_soft/hbase-/bin/../lib/jamon-runtime-.jar:/root/hbase_soft/hbase-/bin/../lib/jasper-compiler-.jar:/root/hbase_soft/hbase-/bin/../lib/jasper-runtime-.jar:/root/hbase_soft/hbase-/bin/../lib/jaxb-api-/bin/../lib/jaxb-impl--.jar:/root/hbase_soft/hbase-/bin/../lib/jersey-core-/bin/../lib/jersey-json-/bin/../lib/jersey-server-/bin/../lib/jettison-/bin/../lib/jetty-.jar:/root/hbase_soft/hbase-/bin/../lib/jetty-util-.jar:/root/hbase_soft/hbase-/bin/../lib/jruby-complete-.jar:/root/hbase_soft/hbase-/bin/../lib/jsp-.jar:/root/hbase_soft/hbase-/bin/../lib/jsp-api-.jar:/root/hbase_soft/hbase-/bin/../lib/jsr305-.jar:/root/hbase_soft/hbase-/bin/../lib/junit-/bin/../lib/libthrift-.jar:/root/hbase_soft/hbase-/bin/../lib/log4j-.jar:/root/hbase_soft/hbase-/bin/../lib/metrics-core-.jar:/root/hbase_soft/hbase-/bin/../lib/netty-.Final.jar:/root/hbase_soft/hbase-/bin/../lib/protobuf-java-/bin/../lib/servlet-api-.jar:/root/hbase_soft/hbase-/bin/../lib/slf4j-api-.jar:/root/hbase_soft/hbase-/bin/../lib/slf4j-log4j12-.jar:/root/hbase_soft/hbase-/bin/../lib/snappy-java-

终端使用:

[root@localhost hbase-]# ./bin/hbase shell
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version , rfb434617716493eac82b55180b0bbd653beb90bf, Thu Mar  :: UTC 

hbase(main)::> create 'test', 'cf'
 row(s) in 1.6440 seconds

hbase(main)::> list 'table'
TABLE
 row(s) in 0.0220 seconds

hbase(main)::> put 'test', 'row1', 'cf:a', 'value1'
 row(s) in 0.1190 seconds

hbase(main)::> put 'test', 'row2', 'cf:b', 'value2'
 row(s) in 0.0140 seconds

hbase(main)::> put 'test', 'row3', 'cf:c', 'value3'
 row(s) in 0.0090 seconds

hbase(main)::> scan 'test'
ROW                                    COLUMN+CELL
 row1                                  column=cf:a, timestamp=, value=value1
 row2                                  column=cf:b, timestamp=, value=value2
 row3                                  column=cf:c, timestamp=, value=value3
 row(s) in 0.0790 seconds

hbase(main)::> get 'test', 'row1'
COLUMN                                 CELL
 cf:a                                  timestamp=, value=value1
 row(s) in 0.0250 seconds

hbase(main)::> 

若要进行分布式,可修改 hbase-site.xml文件如下:

<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.rootdir</name>
<value>hdfs://hadoop:9000/hbase</value>
</property>

连接的客户端代码如下:(我没跑成功,求高人指导)

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.");
        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();
        }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();
        }
    }
}

参考:

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