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 ...
随机推荐
- CP防火墙配置NAT
Static NAT配置 Step1:创建host对象并且配置static NAT,如下图: Step2:修改全局属性的NAT项的ARP代理选项,勾选即可,如下图: Step3:在网关的web por ...
- mybatis的核心配置文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC &q ...
- Vijos1917 艾酱最喜欢的数字 [求绝对众数]
1.题意:第一行一个数字N,表示一共有多少个数字,第二行N个数字,保证其中至少有一个数字出现次数超过一半,任务是求出这个出现最多的数. 2.分析:本题是明显的求众数的问题,常规思路为开一个大数组,在读 ...
- Ubuntu安装微信,解决deepin“版本过低”或NO_PUBKEY问题
在搜索引擎搜索Ubuntu安装微信,最多的结果是通过deepin安装 但是里面使用的deepin-for-ubuntu 安装之后微信扫码会提示版本过低 直接安装deepin.com.wechat_2. ...
- docker(整理中
docker镜像默认的下载地址就是docker的官网,而他们的官网在国内没有节点,时不时就被国家防火墙隔绝,会出现DNS解析不到,或者找不到镜像等狗血提示. 解决的方法有三个: 第一,就是不断尝试,因 ...
- DataTable转成实体列表 和 DataRow转成实体类
#region DataTale转为实体列表 /// <summary> /// DataTale转为实体列表 /// </summary> /// <typeparam ...
- Echarts大数据可视化物流航向省份流向迁徙动态图,开发全解+完美参数注释
最近在研究Echarts的相关案例,毕竟现在大数据比较流行,比较了D3.js.superset等相关的图表插件,还是觉得echarts更简单上手些. 本文是以原生JS为基础,如果使用Vue.js的话, ...
- TVP思享 | 四个全新维度,极限优化HTTP性能
导语 | 当产品的用户量不断翻番时,需求会倒逼着你优化HTTP协议.那么,要想极限优化HTTP性能,应该从哪些维度出发呢?本文将由TVP陶辉老师,为大家分享四个全新维度.「TVP思享」专栏,凝结大咖思 ...
- 开箱即用!使用Rancher 2.3 启用Istio初体验
本文来自Rancher Labs Rancher的理念是Run Kubernetes Everywhere,Rancher 2.3中许多重大更新,让这一理念的实现又向前一步. 其中,最重要的两个特性是 ...
- 2017CCPC杭州(ABCDJ)
所有的题目在这里<--- 待补... Problem A. HDU6264:Super-palindrome 题意: 题目定义了一个超级回文串,这个串满足:它的任一奇数长度的串都是回文串. 现在 ...