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 ...
随机推荐
- HDU6579 2019HDU多校训练赛第一场1002 (线性基)
HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...
- form提交时accept-charset属性在IE及Edge下不起作用解决方案
问题描述 我的页面是utf-8编码 目标页面是EUC-JP编码 即使已经设置了accept-charset="EUC-JP",提交后IE 和 Edge下还是乱码 解决方案: 1. ...
- 深入Oracle的left join中on和where的区别详解
-- from http://blog.itpub.net/30175262/viewspace-1472060/ 今天遇到一个求某月所有天数的统计结果,如果某日的结果是0也需要显示出来,即: 日期 ...
- HDU1251 统计难题[map的应用][Trie树]
一.题意 给出一组单词,另给出一组单词用作查询,求解对于每个用于查询的单词,前一组中有多少个单词以其为前缀. 二.分析 根据题目很容易想到hash的方法,首先可以朴素的考虑将第一组中的所有单词的前缀利 ...
- JavaScript模块化演变 CommonJs,AMD, CMD, UMD(一)
原文链接:https://www.jianshu.com/p/33d53cce8237 原文系列2链接:https://www.jianshu.com/p/ad427d8879cb 前端完全手册: h ...
- 基于DDD的微服务设计和开发实战
你是否还在为微服务应该拆多小而争论不休?到底如何才能设计出收放自如的微服务?怎样才能保证业务领域模型与代码模型的一致性?或许本文能帮你找到答案. 本文是基于 DDD 的微服务设计和开发实战篇,通过借鉴 ...
- 使用 AT 指令进行 Socket 通信
BC26 支持使用 Socket 进行 TCP 和 UDP 协议通信,这两个协议也是 BC26 支持的众多通信协议的基础.本文讲解如何使用这两个协议与服务器端进行通信.在学习这篇文章前,请首先使用AT ...
- DEVOPS技术实践_14:使用docker部署jenkins
一 基础环境准备 [root@node6 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node6 ~]# yum -y ...
- 分析CPU使用率不断增加的原因
工程中发现引起的问题: 结合别的朋友的意见,我的优化思路是: 1.排查是否内存泄漏 经过反复查询代码,未发现有内存泄漏(可以自己百度搜索C#内存泄漏的原因).可以通过任务管理器分析是否有内存泄漏,打开 ...
- 在Asp.Net Core中使用ModelConvention实现全局过滤器隔离
从何说起 这来自于我把项目迁移到Asp.Net Core的过程中碰到一个问题.在一个web程序中同时包含了MVC和WebAPI,现在需要给WebAPI部分单独添加一个接口验证过滤器IActionFil ...