Hbase与Maven工程的Spring配置笔记
1、HBase基本操作
hbase shell: 连接到正在运行的HBase实例
help: 显示一些基本的使用信息以及命令示例。 需要注意的是: 表名, 行, 列都必须使用引号括起来
create 'test', 'cf': 创建一个新表, 必须要指定表明和列族名
list 'test': 列出 test 表的信息
put 'test', 'row1', 'cf:a', 'value1' 往表中插入数据,
put 'test', 'row2', 'cf:b', 'value2' 我们插入了三行数据, 第一行的row key 是 row1, 列是 cf:a, 其值是 value1.HBase 中的列是由列族前缀, 冒号以及列名后缀组成
put 'test', 'row3', 'cf:c', 'value3'
scan 'test' 一次扫描 HBase 表中的所有数据
get 'test', 'row1' 一次获取一行数据
disable 'test' 在某些情况下如果你想要删除表或是改变其设置, 需要先禁用表
enable 'test' 启用表
drop 'test' 删除表
quit: 退出HBase Shell, 但是 HBase 实例仍然在后台运行
stop-hbase.sh bin/start-hbase.sh 脚本可以很方便的启动所有 HBase 守护进程, 同样的, bin/stop-hbase.sh 脚本可以很方便的停止所有 HBase 守护进程
jps 来确保 HMaster 和 HRegionServer 进程都已经关闭
2、示例工程配置
【pom.xml】
自己结合工程:
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hbase.version>2.5.1</hbase.version>
<spring-data-hadoop.version>2.4.0.RELEASE</spring-data-hadoop.version>
<!-- Hadoop-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-hadoop</artifactId>
<version>2.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.1</version>
</dependency>
网上例子:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hbase.version>1.2.2</hbase.version>
<spring-data-hadoop.version>2.4.0.RELEASE</spring-data-hadoop.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-hadoop</artifactId>
<version>${spring-data-hadoop.version}</version>
</dependency>
</dependencies>
【Spring配置文件-直接指定】
web.xml中到applicationContext.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
创建一个 Spring 配置文件 spring-hbase.xml,在该文件中配置与 HBase 连接相关的信息。直接指定 HDFS 地址以及 ZooKeeper 的地址和端口号。
自己结合工程
写到了applicationContext.xml中,不建立spring-hbase.xml:
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xsi:schemaLocation中加:
http://www.springframework.org/schema/hadoop
http://www.springframework.org/schema/hadoop/spring-hadoop.xsd
<!-- 配置hbase连接 -->
<!-- HDFS配置 -->
<hdp:configuration id="hadoopConfiguration">fs.default.name=hdfs://192.168.1.100:9001</hdp:configuration>
<!-- HBase连接配置 -->
<hdp:hbase-configuration id="hbaseConfiguration" zk-quorum="192.168.1.101,192.168.1.101,192.168.1.102" zk-port="2181"/>
<!-- HbaseTemplate Bean配置-->
<bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<property name="configuration" ref="hbaseConfiguration"></property>
</bean>
网上例子:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hdp="http://www.springframework.org/schema/hadoop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop-2.3.xsd">
<!-- HDFS配置 -->
<hdp:configuration id="hadoopConfiguration">
fs.defaultFS="hdfs://localhost:9000"
</hdp:configuration>
<!-- HBase连接配置 -->
<hdp:hbase-configuration id="hbaseConfiguration" zk-quorum="127.0.0.1" zk-port="2181"/>
<!-- HbaseTemplate Bean配置-->
<bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate"
p:configuration-ref="hbaseConfiguration"/>
</beans>
3、测试代码
自己结合工程-测试:
package com.whut.monitor.hbase.test;
import 略;
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class HBaseTest {
private static final String TABLE_NAME = "test";
private static final String ROW_KEY = "row1";
private static final String COLUMN_FAMILY = "cf";
private static final String QUALIFIER = "a";
@Test
public void test() {
// 加载Spring配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取HbaseTemplate
HbaseTemplate hbaseTemplate = (HbaseTemplate) applicationContext.getBean("hbaseTemplate");
// 通过表名和rowKey获取最近一行数据
String result = hbaseTemplate.get(TABLE_NAME, ROW_KEY, new RowMapper<String>() {
public String mapRow(Result result, int rowNum) throws Exception {
return Bytes.toString(result.getValue(COLUMN_FAMILY.getBytes(), QUALIFIER.getBytes()));
}
});
System.out.println(result); // 输出结果是:value1
}
}
自己结合工程-IHBaseDao.java中写法:
package com.whut.monitor.dao;
public interface IHBaseDao {
String find(String tableName, String key, final String family, final String qualifier);
}
HBaseDaoImpl.java:
package com.whut.monitor.dao.impl;
import 略;
@Component
public class HBaseDaoImpl implements IHBaseDao {
@Resource(name="hbaseTemplate")
private HbaseTemplate hbaseTemplate;
@Override
public String find(String tableName, String key, final String family, final String qualifier) {
String result = hbaseTemplate.get(tableName, key, new RowMapper<String>() {
public String mapRow(Result result, int rowNum) throws Exception {
return Bytes.toString(result.getValue(family.getBytes(), qualifier.getBytes()));
}
});
return result;
}
}
添加数据:
public Boolean execute(String tableName, final String key,final String family,final String qualifier,final String value) {
return hbaseTemplate.execute(tableName, new TableCallback<Boolean>() {
public Boolean doInTable(HTableInterface table) throws Throwable {
boolean flag = false;
try{
byte[] rowkey = key.getBytes();
Put put = new Put(rowkey);
put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
flag = true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
});
System.out.println("test poi jfz sandeepin");;
return true;
}
可以测试了,以上是最简单的调通流程,真正使用得还好翻官方文档,不断深入!
Hbase与Maven工程的Spring配置笔记的更多相关文章
- 整合多个maven工程时Spring配置加载JDBC问题
问题叙述: 两个工程都通过JDBC访问mysql数据库,各自运行OK, 但合并成一个maven工程后,发现前一个工程访问数据库异常,貌似拿不到自己的DAO. 解决办法: 发现这两个工程的xml配置中, ...
- Maven 工程下 Spring MVC 站点配置 (三) C3P0连接池与@Autowired的应用
Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 前两篇文章主要是对站点和数据库操作配置进行了演示,如果单 ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- Maven 工程下 Spring MVC 站点配置 (一)
最近,查找一些具体资料时,虽然会有很多,但是系统的却很少,尤其是对maven 下 spring mvc 站点搭建的配置,总是说的很多但让新手一目了然的步骤却少之又少. 对此闲暇时整理了一下,做了一套较 ...
- spring 学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试
spring学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试 ioc 概念 控制反转(Inversion of Control,缩写为IOC),是面向 ...
- Maven工程搭建spring boot+spring mvc+JPA
添加Spring boot支持,引入相关包: 1.maven工程,少不了pom.xml,spring boot的引入可参考官网: <parent> <groupId>org.s ...
- 使用netbeans 搭建maven工程 整合spring springmvc框架
使用netbeans7.4 自带的tomcat7.0 所以jdk选择7.xx 然后等待生成空的工程,会缺一些文件夹,和文件,后续需要的时候补齐 然后修改pom.xml添加引用,直接覆盖dependen ...
- javaEE中的spring配置笔记
0 JavaEE的工程目录 0.1 WebContent 项目的主目录,在eclipse新建工程时可以自己命名,部署时会把该文件夹的内容发布到tomcat的webapps里. 该目录下可以建立 ...
- idea破解版安装、配置jdk以及建立一个简单的maven工程
idea破解版安装.配置jdk,配置jdk环境变量以及建立一个简单的maven工程 一.idea破解版以及配置文件下载 下载网址:https://pan.baidu.com/s/1yojA51X1RU ...
随机推荐
- JQ绑定事件的叠加和解决,index()方法的坑
JQ绑定事件的叠加和解决,index()方法的坑 前言 在做过几个不大不小的项目后,发现技术这种东西,必须要多多实践,才能发现各种问题,理论的知识掌握的再好终究是纸上谈兵. 因此目前感觉有两点是必须要 ...
- 2019-8-31-dotnet-删除只读文件
title author date CreateTime categories dotnet 删除只读文件 lindexi 2019-08-31 16:55:58 +0800 2019-02-28 1 ...
- [luogu1908]逆序对(upper_bound)
对于给定的一段正整数序列,逆序对就是序列中ai>aj且i<j的有序对 用upper_bound法求逆序对,Code很棒 据说有用树状数组和线段树写逆序对的,这里用upper_bound水一 ...
- 游戏《Minecraft》或其他应用程序 实现 自动更新 客户端版本
本渣又来写(水)博客了. 先说一下,我这个解决方案的安全性并不是企业级的,咱们就是一群穷开服的Minecraft玩家. 如果你要投入到企业级应用(容易被黑客攻击的场景),请自己写,思路凑合看看.不然安 ...
- codeforces 220B . Little Elephant and Array 莫队+离散化
传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...
- Asp.NetCore3.1版本的CodeFirst与经典的三层架构与AutoFac批量注入
Core3.1 CodeFirst与AutoFac批量注入(最下面附GitHub完整 Demo,由于上传网速较慢,这里就直接压缩打包上传了) ===Core3.1 CodeFirst 数据库为远程阿里 ...
- 前端——css属性方法
目录 标签快捷写法 宽和高 字体 1.文字字体 2.字体大小 3.字体粗细 4.字体颜色 5.字体对齐 6.文字下划线控制 7.文字首行缩进 8.字体英文换大写 背景属性 1.背景颜色 2.背景图片 ...
- DEVOPS技术实践_17:Jenkins使用扩展邮件功能发送邮件
一 环境准备 1.1 安装插件Email Extension 系统管理-管理插件-安装Email Extension插件 1.2 配置 配置jenkins邮箱的全局配置:系统管理-系统设置-完成邮箱配 ...
- Flink State Backends (状态后端)
State Backends 的作用 有状态的流计算是Flink的一大特点,状态本质上是数据,数据是需要维护的,例如数据库就是维护数据的一种解决方案.State Backends 的作用就是用来维护S ...
- ECShop二次开发指南-文件结构(二)
ecshop文件架构说明 注意:因各版权不一,大概参考/* ECShop 2.5.1 的结构图及各文件相应功能介绍 ECShop2.5.1_Beta upload 的目录 ┣ activity.p ...