SpringBoot 中项目中使用Habse

引入POM

<dependency>

 <groupId>org.apache.hbase</groupId>

 <artifactId>hbase-client</artifactId>

 <version>1.1.3</version>

 <exclusions>

 <exclusion>

 <groupId>org.slf4j</groupId>

 <artifactId>slf4j-log4j12</artifactId>

 </exclusion>

 <exclusion>

 <groupId>org.mortbay.jetty</groupId>

 <artifactId>servlet-api-2.5</artifactId>

 </exclusion>

 <exclusion>

 <groupId>org.mortbay.jetty</groupId>

 <artifactId>servlet-api-2.5-6.1.14</artifactId>

 </exclusion>

 <exclusion>

 <groupId>com.google.guava</groupId>

 <artifactId>guava</artifactId>

 </exclusion>

 </exclusions>

 </dependency>

 <dependency>

 <groupId>org.springframework.data</groupId>

 <artifactId>spring-data-hadoop-boot</artifactId>

 <version>[2.5.0.RELEASE](2.5.0.RELEASE)</version>

 <exclusions>

 <exclusion>

 <groupId>javax.servlet</groupId>

 <artifactId>servlet-api</artifactId>

 </exclusion>

 </exclusions>

 </dependency>

 <dependency>

 <groupId>org.springframework.data</groupId>

 <artifactId>spring-data-hadoop</artifactId>

 <version>[2.5.0.RELEASE](2.5.0.RELEASE)</version>

 <exclusions>

 <exclusion>

 <groupId>org.slf4j</groupId>

 <artifactId>slf4j-log4j12</artifactId>

 </exclusion>

 <exclusion>

 <groupId>log4j</groupId>

 <artifactId>log4j</artifactId>

 </exclusion>

 <exclusion>

 <groupId>javax.servlet</groupId>

 <artifactId>servlet-api</artifactId>

 </exclusion>

 </exclusions>

 </dependency>

 <dependency>

 <groupId>com.google.guava</groupId>

 <artifactId>guava</artifactId>

 <version>22.0</version>

 </dependency>

配置文件中 加入

hbase.config.hbase.zookeeper.quorum: XXX hbase.config.hbase.zookeeper.property.clientPort: XXXX

因为 pom Hbase引入了Guava 是13 的低版本 如果项目中引入了Guava 高版本的 需要在项目中重写一个类 Stopwatch

package com.google.common.base;

package com.google.common.base;

/*

Copyright (C) 2008 The Guava Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import com.google.common.annotations.GwtCompatible;

import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Ticker;

import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static java.util.concurrent.TimeUnit.*;

/**
 * An object that measures elapsed time in nanoseconds. It is useful to measure
 * elapsed time using this class instead of direct calls to {@link
 * System#nanoTime} for a few reasons:
 * An alternate time source can be substituted, for testing or performance
 * reasons.
 * As documented by {@code nanoTime}, the value returned has no absolute
 * meaning, and can only be interpreted as relative to another timestamp
 * returned by {@code nanoTime} at a different time. {@code Stopwatch} is a
 * more effective abstraction because it exposes only these relative values,
 * not the absolute ones.
 * Basic usage:
 * <p>
 * Stopwatch stopwatch = Stopwatch.{@link #createStarted createStarted}();
 * doSomething();
 * stopwatch.{@link #stop stop}(); // optional
 * long millis = stopwatch.elapsed(MILLISECONDS);
 * log.info("time: " + stopwatch); // formatted string like "12.3 ms"
 * Stopwatch methods are not idempotent; it is an error to start or stop a
 * <p>
 * stopwatch that is already in the desired state.
 * When testing code that uses this class, use
 * <p>
 * {@link #createUnstarted(Ticker)} or {@link #createStarted(Ticker)} to
 * supply a fake or mock ticker.
 * This allows you to
 * simulate any valid behavior of the stopwatch.
 * Note: This class is not thread-safe.
 *
 * @author Kevin Bourrillion
 * @SInCE 10.0
 */
@Beta
@GwtCompatible(emulated = true)
public final class Stopwatch {
    private final Ticker ticker;
    private boolean isRunning;
    private long elapsedNanos;
    private long startTick;

    /**
     * Creates (but does not start) a new stopwatch using {@link System#nanoTime}
     * as its time source.
     *
     * @SInCE 15.0
     */
    public static com.google.common.base.Stopwatch createUnstarted() {
        return new com.google.common.base.Stopwatch();
    }

    /**
     * Creates (but does not start) a new stopwatch, using the specified time
     * source.
     *
     * @SInCE 15.0
     */
    public static com.google.common.base.Stopwatch createUnstarted(Ticker ticker) {
        return new com.google.common.base.Stopwatch(ticker);
    }

    /**
     * Creates (and starts) a new stopwatch using {@link System#nanoTime}
     * as its time source.
     *
     * @SInCE 15.0
     */
    public static com.google.common.base.Stopwatch createStarted() {
        return new com.google.common.base.Stopwatch().start();
    }

    /**
     * Creates (and starts) a new stopwatch, using the specified time
     * source.
     *
     * @SInCE 15.0
     */
    public static com.google.common.base.Stopwatch createStarted(Ticker ticker) {
        return new com.google.common.base.Stopwatch(ticker).start();
    }

    /**
     * Creates (but does not start) a new stopwatch using {@link System#nanoTime}
     * as its time source.
     *
     * @deprecated Use {@link com.google.common.base.Stopwatch#createUnstarted()} instead.
     */
    @Deprecated
    public Stopwatch() {
        this(Ticker.systemTicker());
    }

    /**
     * Creates (but does not start) a new stopwatch, using the specified time
     * source.
     *
     * @deprecated Use {@link com.google.common.base.Stopwatch#createUnstarted(Ticker)} instead.
     */
    @Deprecated
    Stopwatch(Ticker ticker) {
        this.ticker = checkNotNull(ticker, "ticker");
    }

    /**
     * Returns {@code true} if {@link #start()} has been called on this stopwatch,
     * and {@link #stop()} has not been called since the last call to {@code
     * start()}.
     */
    public boolean isRunning() {
        return isRunning;
    }

    /**
     * Starts the stopwatch.
     *
     * @return this {@code Stopwatch} instance
     * @throws IllegalStateException if the stopwatch is already running.
     */
    public com.google.common.base.Stopwatch start() {
        checkState(!isRunning, "This stopwatch is already running.");
        isRunning = true;
        startTick = ticker.read();
        return this;
    }

    /**
     * Stops the stopwatch. Future reads will return the fixed duration that had
     * elapsed up to this point.
     *
     * @return this {@code Stopwatch} instance
     * @throws IllegalStateException if the stopwatch is already stopped.
     */
    public com.google.common.base.Stopwatch stop() {
        long tick = ticker.read();
        checkState(isRunning, "This stopwatch is already stopped.");
        isRunning = false;
        elapsedNanos += tick - startTick;
        return this;
    }

    /**
     * Sets the elapsed time for this stopwatch to zero,
     * and places it in a stopped state.
     *
     * @return this {@code Stopwatch} instance
     */
    public com.google.common.base.Stopwatch reset() {
        elapsedNanos = 0;
        isRunning = false;
        return this;
    }

    private long elapsedNanos() {
        return isRunning ? ticker.read() - startTick + elapsedNanos : elapsedNanos;
    }

    /**
     * Returns the current elapsed time shown on this stopwatch, expressed
     * in the desired time unit, with any fraction rounded down.
     * Note that the overhead of measurement can be more than a microsecond, so
     * <p>
     * it is generally not useful to specify {@link TimeUnit#NANOSECONDS}
     * precision here.
     *
     * @SInCE 14.0 (since 10.0 as {@code elapsedTime()})
     */
    public long elapsed(TimeUnit desiredUnit) {
        return desiredUnit.convert(elapsedNanos(), NANOSECONDS);
    }

    /**
     * Returns a string representation of the current elapsed time.
     */
    @GwtIncompatible("String.format()")
    @Override
    public String toString() {
        long nanos = elapsedNanos();
        TimeUnit unit = chooseUnit(nanos);
        double value = (double) nanos / NANOSECONDS.convert(1, unit);

// Too bad this functionality is not exposed as a regular method call
        return String.format("%.4g %s", value, abbreviate(unit));
    }

    private static TimeUnit chooseUnit(long nanos) {
        if (DAYS.convert(nanos, NANOSECONDS) > 0) {
            return DAYS;
        }
        if (HOURS.convert(nanos, NANOSECONDS) > 0) {
            return HOURS;
        }
        if (MINUTES.convert(nanos, NANOSECONDS) > 0) {
            return MINUTES;
        }
        if (SECONDS.convert(nanos, NANOSECONDS) > 0) {
            return SECONDS;
        }
        if (MILLISECONDS.convert(nanos, NANOSECONDS) > 0) {
            return MILLISECONDS;
        }
        if (MICROSECONDS.convert(nanos, NANOSECONDS) > 0) {
            return MICROSECONDS;
        }
        return NANOSECONDS;
    }

    private static String abbreviate(TimeUnit unit) {
        switch (unit) {
            case NANOSECONDS:
                return "ns";
            case MICROSECONDS:
                return "\u03bcs"; // μs
            case MILLISECONDS:
                return "ms";
            case SECONDS:
                return "s";
            case MINUTES:
                return "min";
            case HOURS:
                return "h";
            case DAYS:
                return "d";
            default:
                throw new AssertionError();
        }
    }
}


工具类

@Data
@Component
public class HbaseStorage {
    @Autowired(required = false)
    HbaseTemplate hbaseTemplate;
    private String familyName;

    public boolean save(String tableName,String rowKey,String familyName,Map<String,Object>  data) {
        if (StringUtils.isBlank(familyName)){
            throw new RuntimeException("请设置  默认 familyName 的值");
        }

        for (Map.Entry<String, Object> entry : data.entrySet()) {
            hbaseTemplate.put(tableName,rowKey,familyName,entry.getKey(), Bytes.toBytes(String.valueOf(entry.getValue())));
        }
        return true;
    }
    public <T>boolean save(String tableName,String rowKey,String familyName,T data) {
        Map<String, Object> dataMap = BaseBeanUtils.beanToMap(data);
        return save(tableName, rowKey, familyName, dataMap);
    }
    public <T>boolean save(String tableName,String rowKey,T data) {
        return save(tableName, rowKey, familyName, data);
    }



    public <T>List<T> getByPre (String tableName,String pre,Class<T> type){
        Map<String, Map<String,String>> resultMap = Maps.newHashMap();
        Scan scan = new Scan();
        Filter filter = new PrefixFilter(Bytes.toBytes(pre));
        scan.setFilter(filter);
        hbaseTemplate.find(tableName, scan, (RowMapper) (result, rowNum) -> {
            List<Cell> ceList = result.listCells();
            if (ceList != null && ceList.size() > 0) {
                for (Cell cell : ceList) {
                    String rowKey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                    String qualifierName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                    String qualifierValue = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                    if (!resultMap.containsKey(rowKey)){
                        resultMap.put(rowKey, Maps.newHashMap());
                    }
                    resultMap.get(rowKey).put(qualifierName, qualifierValue);
                }
            }
            return "";
        });

        List<T> list = Lists.newArrayList();
        for (Map.Entry<String, Map<String, String>> entry : resultMap.entrySet()) {
            T convert = BaseBeanUtils.convert(entry.getValue(), type);
            list.add(convert);
        }
        return list;
    }

    public static void main(String[] args) {
        Bean userContact = new Bean();
        userContact.setA("1");
        userContact.setB(1L);
        userContact.setC(1L);
        userContact.setF(1);
        userContact.setG(1);
        userContact.setH(new Object());
        userContact.setI(true);
        Map<String, String> result = Maps.newHashMap();
        Map<String, Object> stringObjectMap = BaseBeanUtils.beanToMap(userContact);
        System.out.println(stringObjectMap);
        for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {
            byte[] bytes = Bytes.toBytes(String.valueOf(entry.getValue()));
            result.put(entry.getKey(), Bytes.toString(bytes));
        }

        Bean convert = BaseBeanUtils.convert(result, Bean.class);
        System.out.println(convert);
    }



}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,027评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,975评论 6 342
  • 大学要找到自己真正感兴趣的知识,然后努力学习,不管知识是本专业还是非专业,没有任何的影响,但是关键点就是在于要学习...
    peterzyzson阅读 245评论 0 0
  • 求职平台提供的工作机会看似选择很多,其实别无选择。招聘方每天阅读的简历目不暇接,有幸被邀请的面试,基本都会安排在第...
    d2b4348a5c47阅读 450评论 0 1
  • 舅舅是乡村医生,一年四季都很忙,接到电话就要出门给别人看病。每年过年的几天,才能休息一下。 这几天,我在取暖屋里看...
    lucky乖乖鱼阅读 282评论 0 0