单机HBase部署以及Java远程连接单机HBase

HBase单机环境搭建

环境说明

  • Red Hat 4.8.5-36
  • JDK1.8
  • HBase2.1.1

部署过程

1、下载 hbase-2.1.1-bin.tar.gz
2、解压文件 tar -zxvf hbase-2.1.1-bin.tar.gz
3、修改配置文件$HBASE_HOME/conf/hbase-site.xml 添加如下参数

<!-- HBase存储数据的路径 默认在tmp路径下,不安全 -->
<configuration>
        <property>
                <name>hbase.rootdir</name>
                <value>PATH</value>
        </property>
</configuration>

启动测试

1、进入$HBASE_HOME/bin路径下./start-hbase.sh
2、./hbase shell 进入hbase shell形式,进行命令行操作

Java连接HBase单机服务

环境配置

1、因为hbase默认通过hostname去找ip然后将这个ip注册到zookeeper中作为hbase单机服务的ip地址。所以我们需要修改/etc/hosts文件。我本地虚拟机环境的hostnamelocalhost.localdomain,原本指向127.0.0.1,这里修改成虚拟机网卡
ip地址。并设置本地域名为hbaseserver

#127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.2.52.138 hbaseserver localhost.localdomain

2、在windows端修改hosts文件 文件路径: C:\Windows\System32\drivers\etc
添加上这一行:
10.2.52.138 hbaseserver

hbase-site.xml配置

<configuration>
        <property>
          <name>hbase.zookeeper.property.clientPort</name>
          <value>2181</value>
        </property>

        <property>
          <name>hbase.master.info.port</name>
          <value>16010</value>
        </property>
        
		<!--客户端基于zookeeper连接hbase的zookeeper地址  其他的都是默认的端口号-->
        <property>
          <name>hbase.zookeeper.quorum</name>
          <value>hbaseserver</value>
        </property>

        <property>
            <name>hbase.regionserver.port</name>
            <value>16020</value>
        </property>

        <property>
          <name>hbase.regionserver.info.port</name>
          <value>16030</value>
        </property>
    
  </configuration>

pom.xml

<!-- hbase java client -->
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>2.1.1</version>
</dependency>

Java连接代码demo

package com.example.demo.hbase;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Appender;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;

public class HBaseClientDemo {

    private HBaseClientDemo() {}

    private static Connection connection;

    //我这里将异常捕获住,将本来是因为用户配置错误导致的IO异常 改为空指针异常,并打印异常信息  正常这种异常应该抛出
    private static void initConnectionInstance() {
        if(connection == null) {
            synchronized (HBaseClientDemo.class) {
                if(connection == null) {
                    try {
                        Configuration config = HBaseConfiguration.create();
                        System.out.println("hbase.regionserver.lease.period: " + config.getLong(HConstants.HBASE_REGIONSERVER_LEASE_PERIOD_KEY,-1));
                        config.set("hbase.zookeeper.quorum", "hbaseserver");
                        config.set("hbase.zookeeper.property.clientPort", "2181");
                        //config.set("hbase.regionserver.dns.nameserver","10.2.52.138");
                        connection = ConnectionFactory.createConnection(config);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 创建HTable
     * @param tableNameStr
     * @param columnFamilies
     * @throws Exception
     */
    public static void createTable(String tableNameStr, List<String> columnFamilies) throws Exception {
        if(tableNameStr == null || tableNameStr.equals("") || columnFamilies ==null || columnFamilies.size() == 0) {
            throw new RuntimeException("parameter is empty or illegal");
        }
        if(connection == null) {
            initConnectionInstance();
        }
        Admin admin = null;
        try {
            admin = connection.getAdmin();
            TableName tableName = TableName.valueOf(tableNameStr);
            List<ColumnFamilyDescriptor> familyDescriptors = new ArrayList<>(columnFamilies.size());
            columnFamilies.forEach(cf -> {
                familyDescriptors.add(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)).build());
            });
            TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
                    .setColumnFamilies(familyDescriptors)
                    .build();
            if(admin.tableExists(tableName)) {
                return;
            }
            admin.createTable(tableDescriptor);
        } catch(Exception e) {
            throw e;
        } finally {
            admin.close();
        }
    }

    public static void putExample(String tableName, String primaryRow, String columnFamily, String qualify, String value) throws Exception {
        Table table = null;
        try {
            table = connection.getTable(TableName.valueOf(tableName));
            Put put = new Put(Bytes.toBytes(primaryRow));
            put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify),Bytes.toBytes(value));
            table.put(put);

        } catch(Exception e) {
            throw e;
        } finally {
            if(table != null) {
                table.close();
            }
        }
    }

    public static void getExample(String tableName) throws Exception {
        Table table = null;
        try {
            table = connection.getTable(TableName.valueOf(tableName));
            Get get = new Get(Bytes.toBytes("primaryRow1"));

            get.addColumn(Bytes.toBytes("testColumnFamily"),Bytes.toBytes("qualityOne"));
            Result result = table.get(get);
            byte[] value = result.getValue(Bytes.toBytes("testColumnFamily"),Bytes.toBytes("qualityOne"));
            System.out.println("value: " + Bytes.toString(value));
            NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> resultMap = result.getMap();
            System.out.println(result);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if(table != null) {
                table.close();
            }

        }
    }

    public static void deleteExample(String tableName, String rowKey, String columnFamily, String qualify) {
        Table table = null;
        Delete delete = null;
        try {
            table = connection.getTable(TableName.valueOf(tableName));
            delete = new Delete(Bytes.toBytes(rowKey));
            //只删除当前列的最新版本数据
            //delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify));
            //删除当前列全部版本数据
            delete.addColumns(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify));

            RowMutations mutations = new RowMutations(Bytes.toBytes("primaryRow2"));
            mutations.add(delete);
            //Mutate 1 successful: false
            //checkAndMutate (行键,列族,列分隔) =>比对操作<= (期望值) ====> T mutations F not mutations
            //(china mobile 1)<(val1) 为假没有进行更新
            boolean res1 = table.checkAndMutate(Bytes.toBytes("primaryRow2"), Bytes.toBytes("testColumnFamily"), Bytes.toBytes("qualityTwo"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("value"), mutations);
            System.out.println("Mutate 1 result: " + res1);

            //table.delete(delete);
            //Boolean flag = table.checkAndMutate(Bytes.toBytes("primaryRow1"),Bytes.toBytes("testColumnFamily")).qualifier(Bytes.toBytes("qualityTwo")).ifEquals(Bytes.toBytes("value2")).ifMatches(CompareOperator.EQUAL,Bytes.toBytes("")).thenDelete(delete);
            //System.out.println("delete result: " + flag);
        } catch (Exception e) {
            e.printStackTrace();
            if(delete != null) {
                System.out.println("failed delete: " + delete);
            }
        } finally {
            if(table != null) {
                try {
                    table.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    public static void batch(String tableName) {
        //在一个批处理事务中  当table没有调用close方法时,get请求是获取不到put的数据,除非使用getBufferWriter
        Table table = null;
        String columnFamily = "testColumnFamily";
        String primaryKey = "primaryRow3";
        String qualify = "qualifyThree";
        try {
            table = connection.getTable(TableName.valueOf(tableName));
            List<Row> batch = new ArrayList<>();
            for (int i = 0; i< 200; i++) {
                Put put = new Put(Bytes.toBytes(primaryKey+i));
                put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualify),Bytes.toBytes("batchValue"+i));
                batch.add(put);
            }


            /*Put put2 = new Put(Bytes.toBytes(primaryKey));
            put2.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify),Bytes.toBytes("newBatchValue"));
            batch.add(put2);*/

            /*Get get = new Get(Bytes.toBytes(primaryKey));
            get.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify));
            batch.add(get);

            Delete delete = new Delete(Bytes.toBytes(primaryKey));
            delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify));
            batch.add(delete);*/

            Object[] results = new Object[batch.size()];

            table.batch(batch,results);

            for (int i = 0; i < results.length; i++) {
                System.out.println("result " + i +": " + results[i]);
            }

            //table.put(put);

        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if(table != null) {
                try {
                    table.close();
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }


    }

    public static void scan(String tableName) {
        Logger log = Logger.getLogger("hbase");
        final int[] counters = new int[2];

        Appender appender = new AppenderSkeleton() {

            @Override
            protected void append(LoggingEvent loggingEvent) {
                String msg = loggingEvent.getMessage().toString();
                if(msg != null && msg.contains("Call: next")) {
                    counters[0]++;
                }
            }

            @Override
            public void close() {}

            @Override
            public boolean requiresLayout() { return false; }
        };

        log.removeAllAppenders();
        log.setAdditivity(false);
        log.addAppender(appender);
        log.setLevel(Level.DEBUG);

        log.info("Call: next");
        String family = "testColumnFamily";
        Table table = null;
        try {
            table = connection.getTable(TableName.valueOf(tableName));
            Scan scan = new Scan();
            scan.setCaching(10);
            scan.setBatch(10);
            scan.addFamily(Bytes.toBytes(family));
            ResultScanner rs = table.getScanner(scan);

            for(Result result : rs) {
                counters[1]++;
                System.out.println(result);
            }
            rs.close();

            System.out.println("rpcTimes: " + counters[0]);
            System.out.println("resultTimes: " + counters[1]);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if(table != null) {
                try {
                    table.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }



    public static void main(String[] args) throws Exception {
        String tableNameStr = "testTable";
        /*List<String> columnFamilies = new ArrayList(){
            {
                add("testColumnFamily");
            }
        };

        createTable(tableNameStr,columnFamilies);

        ;*/

        initConnectionInstance();

        //putExample(tableNameStr,"primaryRow1","testColumnFamily","qualityOne","value2");

        //getExample(tableNameStr);

        //deleteExample(tableNameStr,"primaryRow2","testColumnFamily","qualityTwo");

        scan(tableNameStr);
    }
}

bug场景与解决方案

java.net.ConnectException: Call to localhost/127.0.0.1:16000 failed on connection exception: 
org.apache.hbase.thirdparty.io.netty.channel.AbstractChannel$AnnotatedConnectException:
Connection refused: no further information: localhost/127.0.0.1:16000

产生如上报错,是因为没有修改HBase服务器中hosts文件,没有将hostname与网卡ip对应,解决方案已在环境部署中说明

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值