当前位置: 首页 > news >正文

郑州网站商城建设广东seo加盟

郑州网站商城建设,广东seo加盟,发视频可以赚钱的自媒体平台,校园兼职网站开发用例图SpringBoot使用Hbase 文章目录 SpringBoot使用Hbase一#xff0c;引入依赖二#xff0c;配置文件添加自己的属性三#xff0c;配置类注入HBASE配置四#xff0c;配置Hbase连接池五#xff0c;配置操作服务类 一#xff0c;引入依赖 dependencygroupIdorg…SpringBoot使用Hbase 文章目录 SpringBoot使用Hbase一引入依赖二配置文件添加自己的属性三配置类注入HBASE配置四配置Hbase连接池五配置操作服务类 一引入依赖 dependencygroupIdorg.apache.hbase/groupIdartifactIdhbase-client/artifactIdversion2.3.2/versionexclusionsexclusiongroupIdorg.slf4j/groupIdartifactIdslf4j-log4j12/artifactId/exclusion/exclusions/dependency二配置文件添加自己的属性 hbase:zookeeper:quorum: 10.xxx.xx.153,10.xxx.xx.154,10.xxx.xx.155property:clientPort: 2181master:port: 9001三配置类注入HBASE配置 package com.hbase.config;import org.apache.hadoop.hbase.HBaseConfiguration; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** ClassName: HBaseConfig* author: Leemon* Description: TODO* date: 2023/4/12 18:06* version: 1.0*/ Configuration RefreshScope public class HBaseConfig {Value(${hbase.zookeeper.quorum})private String zookeeperQuorum;Value(${hbase.zookeeper.property.clientPort})private String clientPort;Value(${hbase.master.port})private String masterPort;Beanpublic org.apache.hadoop.conf.Configuration hbaseConfiguration() {org.apache.hadoop.conf.Configuration conf HBaseConfiguration.create();conf.set(hbase.zookeeper.quorum, zookeeperQuorum);conf.set(hbase.zookeeper.property.clientPort, clientPort);// 如果hbase是集群这个必须加上// 这个ip和端口是在hadoop/mapred-site.xml配置文件配置的conf.set(hbase.master, zookeeperQuorum : masterPort);conf.set(hbase.client.keyvalue.maxsize, 20971520);conf HBaseConfiguration.create(conf);return conf;}} 四配置Hbase连接池 这里没有使用懒加载模式减少启动后第一次访问时访问时间过长 package com.hbase.config;import lombok.extern.slf4j.Slf4j; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.springframework.stereotype.Component;import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.io.IOException; import java.util.Enumeration; import java.util.Vector;/*** ClassName: HbaseConnectionPool* author: Leemon* Description: TODO* date: 2023/4/13 9:45* version: 1.0*/ Component Slf4j public class HbaseConnectionPool {/*** 连接池最大的大小*/private int nMaxConnections 20;/*** 连接池自动增加的大小*/private int nIncrConnectionAmount 3;/*** 连接池的初始大小*/private int nInitConnectionAmount 3;/*** 存放连接池中数据库连接的向量,初始时为null*/private Vector vcConnections null;Resourceprivate Configuration hbaseConfiguration;PostConstructpublic void init() {try {vcConnections new Vector();createConnections(nInitConnectionAmount);} catch (Exception e) {e.printStackTrace();}}public synchronized Connection getConnection() {// 确保连接池己被创建if (vcConnections null) {// 连接池还没创建,则返回nullreturn null;}// 获得一个可用的数据库连接Connection conn getFreeConnection();// 如果目前没有可以使用的连接即所有的连接都在使用中while (conn null) {// 等一会再试try {wait(250);} catch (InterruptedException e) {e.printStackTrace();}// 重新再试直到获得可用的连接如果getFreeConnection()返回的为null,则表明创建一批连接后也不可获得可用连接conn getFreeConnection();}// 返回获得的可用的连接return conn;}/*** 本函数从连接池向量 connections 中返回一个可用的的数据库连接如果* 当前没有可用的数据库连接本函数则根据 incrementalConnections 设置* 的值创建几个数据库连接并放入连接池中。* 如果创建后所有的连接仍都在使用中则返回 null* return* 返回一个可用的数据库连接*/private Connection getFreeConnection() {// 从连接池中获得一个可用的数据库连接Connection conn findFreeConnection();if (conn null) {// 如果目前连接池中没有可用的连接// 创建一些连接try {createConnections(nIncrConnectionAmount);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();log.error(create new connection fail., e);}// 重新从池中查找是否有可用连接conn findFreeConnection();if (conn null) {// 如果创建连接后仍获得不到可用的连接则返回 nullreturn null;}}return conn;}/*** 创建由 numConnections 指定数目的数据库连接 , 并把这些连接* 放入 connections 向量中* param _nNumConnections 要创建的数据库连接的数目* throws Exception*/private void createConnections(int _nNumConnections) throws Exception {// 循环创建指定数目的数据库连接for (int x 0; x _nNumConnections; x) {// 是否连接池中的数据库连接的数量己经达到最大最大值由类成员 maxConnections// 指出如果 maxConnections 为 0 或负数表示连接数量没有限制。// 如果连接数己经达到最大即退出。if (this.nMaxConnections 0 this.vcConnections.size() this.nMaxConnections) {log.warn(已达到最大连接数不能再增加连接);throw new Exception(已达到最大连接数 nMaxConnections不能再增加连接);}// 增加一个连接到连接池中向量 connections 中vcConnections.addElement(new ConnectionWrapper(newConnection()));log.info(HBase数据库连接己创建 ...... x);}}/*** 查找池中所有的連接查找一个可用的數據庫連接* 如果没有可用的連結返回null* return* 返回一個可用的數據庫連接*/private Connection findFreeConnection() {Connection conn null;ConnectionWrapper connWrapper null;//獲得連接池向量中所有的對象Enumeration enumerate vcConnections.elements();//遍歷所有的对象看是否有可用的連接while (enumerate.hasMoreElements()) {connWrapper (ConnectionWrapper) enumerate.nextElement();if (!connWrapper.isBusy()) {//如果此對象不忙則獲得它的數據庫連接并把它設為忙conn connWrapper.getConnection();connWrapper.setBusy(true);// 己经找到一个可用的連接退出break;}}// 返回找到的可用連接return conn;}/***创建一个新的数据库连接并返回它* return* 返回一个新创建的数据库连接*/private Connection newConnection() {/** hbase 连接 */Connection conn null;// 创建一个数据库连接try {conn ConnectionFactory.createConnection(hbaseConfiguration);} catch (IOException e) {log.error(创建HBase数据库连接失败!);e.printStackTrace();}// 返回创建的新的数据库连接return conn;}public synchronized void releaseConnection(Connection conn) {if (this.vcConnections null) {log.info(连接池不存在无法返回此连接到连接池中!!);} else {ConnectionWrapper connWrapper null;Enumeration enumerate this.vcConnections.elements();while(enumerate.hasMoreElements()) {connWrapper (ConnectionWrapper) enumerate.nextElement();if (conn connWrapper.getConnection()) {connWrapper.setBusy(false);break;}}}}class ConnectionWrapper {/*** 数据库连接*/private Connection connection null;/*** 此连接是否正在使用的标志默认没有正在使用*/private boolean busy false;/*** 构造函数根据一个 Connection 构告一个 PooledConnection 对象*/public ConnectionWrapper(Connection connection) {this.connection connection;}/*** 返回此对象中的连接*/public Connection getConnection() {return connection;}/*** 设置此对象的连接*/public void setConnection(Connection connection) {this.connection connection;}/*** 获得对象连接是否忙*/public boolean isBusy() {return busy;}/*** 设置对象的连接正在忙*/public void setBusy(boolean busy) {this.busy busy;}}}init()方法实现在初始化连接池的时候创建默认数值的连接。 五配置操作服务类 操作类接口 HbaseService.java package com.hbase.service;import org.apache.hadoop.hbase.client.Scan;import java.util.Map;/*** InterfaceName: HbaseService* author: Leemon* Description: TODO* date: 2023/4/12 18:11* version: 1.0*/ public interface HbaseService {MapString,MapString,String getResultScanner(String tableName, String startRowKey, String stopRowKey);MapString,String getRowData(String tableName, String rowKey);MapString,String getFamilyValue(String tableName, String rowKey, String familyName);String getColumnValue(String tableName, String rowKey, String familyName, String columnName);MapString,MapString,String queryData(String tableName, Scan scan);}接口实现类 HbaseServiceImpl.java package com.hbase.service.impl;import com.hbase.config.HbaseConnectionPool; import com.hbase.service.HbaseService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.PrefixFilter; import org.apache.hadoop.hbase.util.Bytes; import org.springframework.stereotype.Service;import javax.annotation.Resource; import java.io.IOException; import java.text.MessageFormat; import java.util.*;/*** ClassName: HbaseServiceImpl* author: Leemon* Description: TODO* date: 2023/4/12 18:13* version: 1.0*/ Slf4j Service public class HbaseServiceImpl implements HbaseService {Resourceprivate HbaseConnectionPool pool;Overridepublic MapString,MapString,String getResultScanner(String tableName, String startRowKey, String stopRowKey){Scan scan new Scan();if(StringUtils.isNotBlank(startRowKey) StringUtils.isNotBlank(stopRowKey)){scan.withStartRow(Bytes.toBytes(startRowKey));scan.withStopRow(Bytes.toBytes(stopRowKey));}return this.queryData(tableName,scan);}public MapString,MapString,String getResultScannerPrefixFilter(String tableName, String prefix){Scan scan new Scan();if(StringUtils.isNotBlank(prefix)){Filter filter new PrefixFilter(Bytes.toBytes(prefix));scan.setFilter(filter);}return this.queryData(tableName,scan);}Overridepublic MapString,MapString,String queryData(String tableName, Scan scan){MapString,MapString,String result new HashMap();ResultScanner rs null;// 获取表Table table null;Connection connection null;try {connection pool.getConnection();table getTable(connection, tableName);rs table.getScanner(scan);for (Result r : rs) {//每一行数据MapString,String columnMap new HashMap();String rowKey null;for (Cell cell : r.listCells()) {if(rowKey null){rowKey Bytes.toString(cell.getRowArray(),cell.getRowOffset(),cell.getRowLength());}columnMap.put(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()), Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));}if(rowKey ! null){result.put(rowKey,columnMap);}}}catch (IOException e) {log.error(MessageFormat.format(遍历查询指定表中的所有数据失败,tableName:{0},tableName),e);}finally {close(null, rs, table, connection);}return result;}Overridepublic MapString,String getRowData(String tableName, String rowKey){//返回的键值对MapString,String result new HashMap();Get get new Get(Bytes.toBytes(rowKey));// 获取表Table table null;Connection connection null;try {connection pool.getConnection();table getTable(connection, tableName);Result hTableResult table.get(get);if (hTableResult ! null !hTableResult.isEmpty()) {for (Cell cell : hTableResult.listCells()) {result.put(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()), Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));}// 某些应用场景需要插入到数据库的时间if (hTableResult.listCells().size() 0) {result.put(Timestamp, hTableResult.listCells().get(0).getTimestamp() );}}}catch (IOException e) {log.error(MessageFormat.format(查询一行的数据失败,tableName:{0},rowKey:{1},tableName,rowKey),e);}finally {close(null,null, table, connection);}return result;}Overridepublic MapString,String getFamilyValue(String tableName, String rowKey, String familyName){//返回的键值对MapString,String result new HashMap(2);Get get new Get(Bytes.toBytes(rowKey));get.addFamily(Bytes.toBytes(familyName));// 获取表Table table null;Connection connection null;try {connection pool.getConnection();table getTable(connection, tableName);Result getResult table.get(get);if (getResult ! null !getResult.isEmpty()) {for (Cell cell : getResult.listCells()) {result.put(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()), Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));}}} catch (IOException e) {log.error(MessageFormat.format(查询指定单元格的数据失败,tableName:{0},rowKey:{1},familyName:{2}, tableName, rowKey, familyName), e);}finally {close(null,null, table, connection);}return result;}Overridepublic String getColumnValue(String tableName, String rowKey, String familyName, String columnName){String str null;Get get new Get(Bytes.toBytes(rowKey));// 获取表Table table null;Connection connection null;try {connection pool.getConnection();table getTable(connection, tableName);Result result table.get(get);if (result ! null !result.isEmpty()) {Cell cell result.getColumnLatestCell(Bytes.toBytes(familyName), Bytes.toBytes(columnName));if(cell ! null){str Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());}}} catch (IOException e) {log.error(MessageFormat.format(查询指定单元格的数据失败,tableName:{0},rowKey:{1},familyName:{2},columnName:{3},tableName,rowKey,familyName,columnName),e);}finally {close(null,null, table, connection);}return str;}private Table getTable(Connection connection, String tableName) throws IOException {Table table connection.getTable(TableName.valueOf(tableName));return table;}private void close(Admin admin, ResultScanner rs, Table table, Connection connection){if(admin ! null){try {admin.close();} catch (IOException e) {log.error(关闭Admin失败,e);}}if(rs ! null){rs.close();}if(table ! null){try {table.close();} catch (IOException e) {log.error(关闭Table失败,e);}}// 释放连接if (Objects.nonNull(connection)) {pool.releaseConnection(connection);}}}ok现在就可以操作使用了。 以前都是在非Spring环境下使用Hbase的一开始会出现当服务使用时间过久某些会使用hbase的接口调用次数过多的时候会报【已超过最大的连接数】只能每一次调用接口后最后一行加上释放连接。以前的做法每次调用都要在代码里手动获取一个连接 这次将释放连接都集成在操作服务类的实现方法中避免了开发接口可能遗漏的错误可能不会再出现这个问题。
http://www.yingshimen.cn/news/97713/

相关文章:

  • 镇江个人网站制作企业建立自己的网站
  • 郑州 网站建设 东区江西南昌网站建设公司哪家好
  • 网络营销方式思维导图网站优化方案怎么写
  • 自适应网站是什么在网上做黑彩网站会怎样
  • 金坛做网站的聚美优品网站开发时间进度表
  • 建设厅网站密码忘了怎么办网站源码爬取
  • 网站专题建设合同企业年报系统登录
  • 网站的栏目设计怎么做如何在亚马逊上开店卖到国外
  • 手机上如何制作网站江门seo排名优化
  • 广告文案优秀网站酒泉百度做网站多少钱
  • 开发一个手机软件app需要多少钱爱采购seo
  • 苏州公司企业网站建设黑白摄影网站
  • 个人可以做电视台网站吗免费crm手机版
  • 阿里云创建网站wordpress缩略图圆角阴影
  • 网站见建设哪些人需要建网站
  • dw怎么做购物网站文案推广发布网站大全
  • 做基因结构可以用哪个网站国外网站设计公司
  • 做企业网站建设挣钱吗博客可以做seo吗
  • 网站建设都需要哪些资质千博企业网站管理系统旗舰版
  • seo网站优化知识客户管理系统排行榜
  • 做网站的公司 贵阳网页设计与网站建设中的热点是什么
  • 个人网站备案内容描述wordpress主题 水墨
  • 广东网站建设服务wordpress 域名米表
  • 婚纱摄影网站的设计校园 网站建设 知乎
  • 网站模板整站资源2020网页游戏
  • 朝阳网站建设公司广东省医院建设协会网站首页
  • 网站公司建设个服务号多少钱wordpress大学视频教程
  • 3g版和wap网站苏州网站建设外包
  • 镇江市建设局网站好的手机端网站模板下载
  • 贵州城乡住房建设厅网站凡天网网站建设