hbase伪分布式配置

1.在单机模式的基础上进行配置,打开hbase-env.sh。

vim /usr/local/hbase/conf/hbase-env.sh

2.配置HBASE_CLASSPATH为hadoop安装目录下的conf目录,即 /usr/local/hadoop/conf。JAVA_HOME、HBASE_MANAGES_ZK之前已经配置好了。

export HBASE_CLASSPATH=/usr/local/hadoop/conf 

3.打开hbase-site.xml文件,hbase.rootdir指定hbase数据在HDFS的存储路径;将hbase.cluter.distributed设置为true。

vim /usr/local/hbase/conf/hbase-site.xml

配置信息如下

<configuration>
        <property>
                <name>hbase.rootdir</name>
                <value>hdfs://localhost:9000/hbase</value>#指定HBase的存储目录。
        </property>
        <property>
                <name>hbase.cluster.distributed</name>#设置集群处于分布式模式
                <value>true</value>
        </property>
        <property>
        <name>hbase.unsafe.stream.capability.enforce</name>#避免出现启动错误。
        <value>false</value>
    </property>
</configuration>

4.Hbase的启动测试,先启动hadoop,在启动hbase。

ssh localhost
cd /usr/local/hadoop
./sbin/start-dfs.sh
jps
cd /usr/local/hbase
bin/start-hbase.sh
jps

最终看到如下信息,则成功开启。
在这里插入图片描述
5.关闭, 先关闭hbase,在关闭hadoop。

bin/stop-hbase.sh
cd /usr/local/hadoop
./sbin/stop-dfs.sh

6.Hbase shell 命令

  • 进入hbase的shell
hbase shell
在这里插入代码片

7.Java API编程

  • 导入hbase的lib目录下的所有jar包。
package my.hbase;
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 HbaseCode {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    public static void main(String[] args)throws IOException{
        init();
        createTable("student-info",new String[]{"score"});
        insertData("student-info","zhangsan","score","English","69");
        insertData("student-info","zhangsan","score","Math","86");
        insertData("student-info","zhangsan","score","Computer","77");
        getData("student-info", "zhangsan", "score","English");
        close();
    }
 
    public static void init(){
        configuration  = HBaseConfiguration.create();
        
        //设置hbase数据存储的根路径,放在分布式文件系统hdfs目录下
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
        	//建立链接
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
 
    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
 
    public static void createTable(String myTableName,String[] colFamily) throws IOException 
    {
    	TableName tableName = TableName.valueOf(myTableName);
        if(admin.tableExists(tableName))
        {
            System.out.println("talbe is exists!");
        }else {
        	
        	//管理表的信息
            TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
            for(String str:colFamily)
            {
            	//管理列族的类
                ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
                tableDescriptor.setColumnFamily(family);
            }
            admin.createTable(tableDescriptor.build());
        } 
    }
 
    public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException 
    { 
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(colFamily.getBytes(),col.getBytes(), val.getBytes());
        table.put(put);
        table.close(); 
    }
 
    public static void getData(String tableName,String rowKey,String colFamily, String col)throws  IOException
    { 
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(colFamily.getBytes(),col.getBytes());
        Result result = table.get(get);
        System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
        table.close(); 
    }
}

运行结果查看如下student-info表。
在这里插入图片描述

8.源链接
厦门大学数据库实验室

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秘境之眼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值