hadoop 学习(1) hbase安装运行

1、首先现在hbase安装包,解压

tar -zxvf hbase-2.7.tar.gz

2、配置环境变量

export HBASE_HOME=/home/songjinbin/tools/hbase-1.0.0


3、添加hbase的配置

<configuration>
   <property>
    <name>hbase.rootdir</name>
    <value>/home/songjinbin/hadoop/hbase</value>
  </property>
  <property>
    <name>hbase.zookeeper.property.dataDir</name>
    <value>/home/songjinbin/hadoop/zookeeper</value>
  </property>
</configuration>

hbase.rootDIr 配置的是hbase写入的数据信息,默认是在/tmp下面,

注:本此配置的是单机模式,用的hbase自带的zookeeper


3、启动hbase

(1)启动hbase之前必须保证hadoop相关启动,先启动start_hdfs.sh start_yield.sh 

(2)执行

hbase shell


正常返回结果后我们就可以操作hbase命令了。

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/songjinbin/tools/hbase-1.0.0/lib/slf4j-log4j12-1.7.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/songjinbin/tools/hadoop-2.7.1/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 1.0.0, r6c98bff7b719efdb16f71606f3b7d8229445eb81, Sat Feb 14 19:49:22 PST 2015


这样我们可以使用命令模式使用Hbase,该命令封装 了JAVA客户端的api的JRuby应用程序。



4、hbase基本命令行操作


(1)创建数据库

create 'users','info'

hbase是一种面向列的数据库,按照行和列进行组织,列组成列族,users指的是表名,info就是列族。

(2)查询数据库

list

TABLE                                                                           
test                                                                            
users                                                                           
2 row(s) in 0.0080 seconds

=> ["test", "users"]
hbase(main):003:0> 
describe 命令,可以查询表内更多默认参数

hbase(main):004:0> describe 'users'
Table users is ENABLED                                                          
users                                                                           
COLUMN FAMILIES DESCRIPTION                                                     
{NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATIO
N_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL
 => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY =>
 'false', BLOCKCACHE => 'true'}                                                 
1 row(s) in 0.0880 seconds

5、使用java操作hbase

(1)添加maven依赖包

	<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.5.1</version>
        </dependency>
         <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
             <version>0.98.0-hadoop2</version> 
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.5.1</version>
        </dependency>
(2)连接操作

我们一般使用HBASE连接池创建连接,代码如下:

package com.hadoop.hbase;

import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HTablePool pool =new HTablePool();
		HTableInterface userTable =pool.getTable("users");
	}

}
(2)数据操作

 我们知道一般关系型数据库中都有主键的概念,同样,hbase数据库一般使用一个行健的概念特制一行数据的唯一标示,

hbase api通过put(放入)get(读)delete(删除)scan(扫描)increment(递增)几个接口操作数据库

首先创建一个行健

	Put p =new Put(Bytes.toBytes("testse"));

应为hbase只允许所有数据通过原始java字节类型数据传输,所以提供了一个公用类Bytes来处理

存入数据

		HTableInterface userTable =pool.getTable("users");
		Put p =new Put(Bytes.toBytes("testse"));
		p.add(Bytes.toBytes("info"), Bytes.toBytes("name"),Bytes.toBytes("jinbin"));
		p.add(Bytes.toBytes("info"), Bytes.toBytes("username"),Bytes.toBytes("jinbin"));
		p.add(Bytes.toBytes("info"), Bytes.toBytes("password"),Bytes.toBytes("12345"));
		userTable.put(p);
		userTable.close();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值