欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

聊聊springboot?整合?hbase的問(wèn)題

 更新時(shí)間:2021年11月30日 11:22:30   作者:不想做咸魚的王富貴  
這篇文章主要介紹了springboot?整合?hbase的問(wèn)題,文中給大家提到配置linux服務(wù)器hosts及配置window?hosts的相關(guān)知識(shí),需要的朋友可以參考下

springboot 整合 hbase

要確定這三個(gè)端口外包可以訪問(wèn)
如果是127.0.0.1 可以參考修改
Linux下Hbase安裝配置

<property>
<name>hbase.master.ipc.address</name>
<value>0.0.0.0</value>
</property>
<property>
<name>hbase.regionserver.ipc.address</name>
<value>0.0.0.0</value>
</property>

在這里插入圖片描述

配置linux服務(wù)器hosts

	 vim /etc/hosts
127.0.0.1 VM-16-8-centos VM-16-8-centos 
127.0.0.1  localhost.localdomain localhot
127.0.0.1  localhost6.localdomain6 localhost6
ip vm-16-8-centos  

配置window hosts

ip vm-16-8-centos  

項(xiàng)目結(jié)構(gòu)

在這里插入圖片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.hbase</groupId>
	<artifactId>hbase</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>hbase</name>
	<description>springBoot_hBase Demo</description>

	<properties>
		<java.version>11</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!--HBase -->
		<dependency>
			<groupId>org.apache.hbase</groupId>
			<artifactId>hbase-client</artifactId>
			<version>2.4.7</version>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.4</version>
		</dependency>



	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

HbaseConfig

package com.springboot.hbase.config.hbase;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * Hbase配置類
 * @author sixmonth
 * @Date 2019年5月13日
 *
 */
@Configuration
@ConfigurationProperties(prefix = HbaseConfig.CONF_PREFIX)
public class HbaseConfig {

    public static final String CONF_PREFIX = "hbase.conf";

    private Map<String,String> confMaps;

    public Map<String, String> getconfMaps() {
        return confMaps;
    }
    public void setconfMaps(Map<String, String> confMaps) {
        this.confMaps = confMaps;
    }
}

HBaseUtils

package com.springboot.hbase.config.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;

import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * hbase工具類
 * @author sixmonth
 * @Date 2019年5月13日
 *
 */
@DependsOn("springContextHolder")//控制依賴順序,保證springContextHolder類在之前已經(jīng)加載
@Component
public class HBaseUtils {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    //手動(dòng)獲取hbaseConfig配置類對(duì)象
    private static HbaseConfig hbaseConfig = SpringContextHolder.getBean("hbaseConfig");

    private static Configuration conf = HBaseConfiguration.create();
    private static ExecutorService pool = Executors.newScheduledThreadPool(20);	//設(shè)置hbase連接池
    private static Connection connection = null;
    private static HBaseUtils instance = null;
    private static Admin admin = null;

    private HBaseUtils(){
        if(connection == null){
            try {
                //將hbase配置類中定義的配置加載到連接池中每個(gè)連接里
                Map<String, String> confMap = hbaseConfig.getconfMaps();
                for (Map.Entry<String,String> confEntry : confMap.entrySet()) {
                    conf.set(confEntry.getKey(), confEntry.getValue());
                }
                connection = ConnectionFactory.createConnection(conf, pool);
                admin = connection.getAdmin();
            } catch (IOException e) {
                logger.error("HbaseUtils實(shí)例初始化失??!錯(cuò)誤信息為:" + e.getMessage(), e);
            }
        }
    }

    //簡(jiǎn)單單例方法,如果autowired自動(dòng)注入就不需要此方法
    public static synchronized HBaseUtils getInstance(){
        if(instance == null){
            instance = new HBaseUtils();
        }
        return instance;
    }


    /**
     * 創(chuàng)建表
     *
     * @param tableName         表名
     * @param columnFamily      列族(數(shù)組)
     */
    public void createTable(String tableName, String[] columnFamily) throws IOException{
        TableName name = TableName.valueOf(tableName);
        //如果存在則刪除
        if (admin.tableExists(name)) {
            admin.disableTable(name);
            admin.deleteTable(name);
            logger.error("create htable error! this table {} already exists!", name);
        } else {
            HTableDescriptor desc = new HTableDescriptor(name);
            for (String cf : columnFamily) {
                desc.addFamily(new HColumnDescriptor(cf));
            }
            admin.createTable(desc);
        }
    }

    /**
     * 插入記錄(單行單列族-多列多值)
     *
     * @param tableName         表名
     * @param row               行名
     * @param columnFamilys     列族名
     * @param columns           列名(數(shù)組)
     * @param values            值(數(shù)組)(且需要和列一一對(duì)應(yīng))
     */
    public void insertRecords(String tableName, String row, String columnFamilys, String[] columns, String[] values) throws IOException {
        TableName name = TableName.valueOf(tableName);
        Table table = connection.getTable(name);
        Put put = new Put(Bytes.toBytes(row));
        for (int i = 0; i < columns.length; i++) {
            put.addColumn(Bytes.toBytes(columnFamilys), Bytes.toBytes(columns[i]), Bytes.toBytes(values[i]));
            table.put(put);
        }
    }

    /**
     * 插入記錄(單行單列族-單列單值)
     *
     * @param tableName         表名
     * @param row               行名
     * @param columnFamily      列族名
     * @param column            列名
     * @param value             值
     */
    public void insertOneRecord(String tableName, String row, String columnFamily, String column, String value) throws IOException {
        TableName name = TableName.valueOf(tableName);
        Table table = connection.getTable(name);
        Put put = new Put(Bytes.toBytes(row));
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
        table.put(put);
    }

    /**
     * 刪除一行記錄
     *
     * @param tablename         表名
     * @param rowkey            行名
     */
    public void deleteRow(String tablename, String rowkey) throws IOException {
        TableName name = TableName.valueOf(tablename);
        Table table = connection.getTable(name);
        Delete d = new Delete(rowkey.getBytes());
        table.delete(d);
    }

    /**
     * 刪除單行單列族記錄
     * @param tablename         表名
     * @param rowkey            行名
     * @param columnFamily      列族名
     */
    public void deleteColumnFamily(String tablename, String rowkey, String columnFamily) throws IOException {
        TableName name = TableName.valueOf(tablename);
        Table table = connection.getTable(name);
        Delete d = new Delete(rowkey.getBytes()).addFamily(Bytes.toBytes(columnFamily));
        table.delete(d);
    }

    /**
     * 刪除單行單列族單列記錄
     *
     * @param tablename         表名
     * @param rowkey            行名
     * @param columnFamily      列族名
     * @param column            列名
     */
    public void deleteColumn(String tablename, String rowkey, String columnFamily, String column) throws IOException {
        TableName name = TableName.valueOf(tablename);
        Table table = connection.getTable(name);
        Delete d = new Delete(rowkey.getBytes()).addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
        table.delete(d);
    }


    /**
     * 查找一行記錄
     *
     * @param tablename         表名
     * @param rowKey            行名
     */
    public static String selectRow(String tablename, String rowKey) throws IOException {
        String record = "";
        TableName name=TableName.valueOf(tablename);
        Table table = connection.getTable(name);
        Get g = new Get(rowKey.getBytes());
        Result rs = table.get(g);
        NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = rs.getMap();
        for (Cell cell : rs.rawCells()) {
            StringBuffer stringBuffer = new StringBuffer()
                    .append(Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength())).append("\t")
                    .append(Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength())).append("\t")
                    .append(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())).append("\t")
                    .append(Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())).append("\n");
            String str = stringBuffer.toString();
            record += str;
        }
        return record;
    }

    /**
     * 查找單行單列族單列記錄
     *
     * @param tablename         表名
     * @param rowKey            行名
     * @param columnFamily      列族名
     * @param column            列名
     * @return
     */
    public static String selectValue(String tablename, String rowKey, String columnFamily, String column) throws IOException {
        TableName name=TableName.valueOf(tablename);
        Table table = connection.getTable(name);
        Get g = new Get(rowKey.getBytes());
        g.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
        Result rs = table.get(g);
        return Bytes.toString(rs.value());
    }

    /**
     * 查詢表中所有行(Scan方式)
     *
     * @param tablename
     * @return
     */
    public String scanAllRecord(String tablename) throws IOException {
        String record = "";
        TableName name=TableName.valueOf(tablename);
        Table table = connection.getTable(name);
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        try {
            for(Result result : scanner){
                for (Cell cell : result.rawCells()) {
                    StringBuffer stringBuffer = new StringBuffer()
                            .append(Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength())).append("\t")
                            .append(Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength())).append("\t")
                            .append(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())).append("\t")
                            .append(Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())).append("\n");
                    String str = stringBuffer.toString();
                    record += str;
                }
            }
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        return record;
    }

    /**
     * 根據(jù)rowkey關(guān)鍵字查詢報(bào)告記錄
     *
     * @param tablename
     * @param rowKeyword
     * @return
     */
    public List scanReportDataByRowKeyword(String tablename, String rowKeyword) throws IOException {
        ArrayList<Object> list = new ArrayList<Object>();

        Table table = connection.getTable(TableName.valueOf(tablename));
        Scan scan = new Scan();

        //添加行鍵過(guò)濾器,根據(jù)關(guān)鍵字匹配
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(rowKeyword));
        scan.setFilter(rowFilter);

        ResultScanner scanner = table.getScanner(scan);
        try {
            for (Result result : scanner) {
                //TODO 此處根據(jù)業(yè)務(wù)來(lái)自定義實(shí)現(xiàn)
                list.add(null);
            }
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        return list;
    }

    /**
     * 根據(jù)rowkey關(guān)鍵字和時(shí)間戳范圍查詢報(bào)告記錄
     *
     * @param tablename
     * @param rowKeyword
     * @return
     */
    public List scanReportDataByRowKeywordTimestamp(String tablename, String rowKeyword, Long minStamp, Long maxStamp) throws IOException {
        ArrayList<Object> list = new ArrayList<Object>();

        Table table = connection.getTable(TableName.valueOf(tablename));
        Scan scan = new Scan();
        //添加scan的時(shí)間范圍
        scan.setTimeRange(minStamp, maxStamp);

        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(rowKeyword));
        scan.setFilter(rowFilter);

        ResultScanner scanner = table.getScanner(scan);
        try {
            for (Result result : scanner) {
                //TODO 此處根據(jù)業(yè)務(wù)來(lái)自定義實(shí)現(xiàn)
                list.add(null);
            }
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        return list;
    }


    /**
     * 刪除表操作
     *
     * @param tablename
     */
    public void deleteTable(String tablename) throws IOException {
        TableName name=TableName.valueOf(tablename);
        if(admin.tableExists(name)) {
            admin.disableTable(name);
            admin.deleteTable(name);
        }
    }
}

SpringContextHolder

package com.springboot.hbase.config.hbase;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring的ApplicationContext的持有者,可以用靜態(tài)方法的方式獲取spring容器中的bean
 */
@Component
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        assertApplicationContext();
        return applicationContext;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        assertApplicationContext();
        return (T) applicationContext.getBean(beanName);
    }

    public static <T> T getBean(Class<T> requiredType) {
        assertApplicationContext();
        return applicationContext.getBean(requiredType);
    }

    private static void assertApplicationContext() {
        if (SpringContextHolder.applicationContext == null) {
            throw new RuntimeException("applicaitonContext屬性為null,請(qǐng)檢查是否注入了SpringContextHolder!");
        }
    }

}

application.properties

hbase.conf.confMaps.hbase.zookeeper.quorum= vm-16-8-centos:2181

HBaseApplicationTests

package com.springboot.hbase;

import com.springboot.hbase.config.hbase.HBaseUtils;
import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
@Log4j2
class HBaseApplicationTests {
    @Autowired
    private HBaseUtils hBaseUtils;


    @Test
    void deleteTable() throws IOException {
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            String str = hBaseUtils.scanAllRecord("SYSTEM.TASK");//掃描表
            System.out.println("獲取到hbase的內(nèi)容:"+str);
            map.put("hbaseContent",str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(map);
    }

}

執(zhí)行測(cè)試類
成功如下

在這里插入圖片描述

到此這篇關(guān)于springboot?整合?hbase的文章就介紹到這了,更多相關(guān)springboot?整合?hbase內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java用撲克牌計(jì)算24點(diǎn)

    java用撲克牌計(jì)算24點(diǎn)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)24點(diǎn)撲克牌游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • SpringCloud之分布式配置中心Spring Cloud Config高可用配置實(shí)例代碼

    SpringCloud之分布式配置中心Spring Cloud Config高可用配置實(shí)例代碼

    這篇文章主要介紹了SpringCloud之分布式配置中心Spring Cloud Config高可用配置實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • idea更改項(xiàng)目(模塊)JDK版本的操作步驟

    idea更改項(xiàng)目(模塊)JDK版本的操作步驟

    idea很多地方都設(shè)置了jdk版本,不同模塊的jdk版本也可能不一樣,下面這篇文章主要給大家介紹了關(guān)于idea更改項(xiàng)目(模塊)JDK版本的操作步驟,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • 一文詳細(xì)springboot實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的整合步驟

    一文詳細(xì)springboot實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的整合步驟

    Spring Boot可以很方便地與MySQL數(shù)據(jù)庫(kù)進(jìn)行整合,下面這篇文章主要給大家介紹了關(guān)于springboot實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的整合步驟,文中通過(guò)圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • Java實(shí)現(xiàn)優(yōu)先隊(duì)列式廣度優(yōu)先搜索算法的示例代碼

    Java實(shí)現(xiàn)優(yōu)先隊(duì)列式廣度優(yōu)先搜索算法的示例代碼

    這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)優(yōu)先隊(duì)列式廣度優(yōu)先搜索算法,文中通過(guò)一個(gè)示例帶大家具體了解了實(shí)現(xiàn)的方法,需要的可以參考一下
    2022-08-08
  • Java常用工具類—集合排序

    Java常用工具類—集合排序

    這篇文章主要介紹了Java集合排序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Mybatis Select Count(*)的返回值類型介紹

    Mybatis Select Count(*)的返回值類型介紹

    這篇文章主要介紹了Mybatis Select Count(*)的返回值類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 利用Spring Cloud Config結(jié)合Bus實(shí)現(xiàn)分布式配置中心的步驟

    利用Spring Cloud Config結(jié)合Bus實(shí)現(xiàn)分布式配置中心的步驟

    這篇文章主要介紹了利用Spring Cloud Config結(jié)合Bus實(shí)現(xiàn)分布式配置中心的相關(guān)資料,文中通過(guò)示例代碼將實(shí)現(xiàn)的步驟一步步介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來(lái)一起看看吧
    2018-05-05
  • Java wait和notify虛假喚醒原理

    Java wait和notify虛假喚醒原理

    這篇文章主要介紹了Java wait和notify虛假喚醒,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • java開發(fā)中的誤區(qū)和細(xì)節(jié)整理

    java開發(fā)中的誤區(qū)和細(xì)節(jié)整理

    這篇文章給大家整理了關(guān)于JAVA開發(fā)中的細(xì)節(jié)以及經(jīng)常進(jìn)入的誤區(qū)整理,希望我們整理的內(nèi)容能夠給大家提供到幫助。
    2018-04-04

最新評(píng)論