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 ...
随机推荐
- 002.MFC_对话框_静态文本_编辑框
一.建立 名为dialogAndCtl的MFC工程,并添加如图控件 1.将上方static text 控件 Caption属性设置为在文本框中如数文本,可以统计字符 2.edit control控件属 ...
- unity3D 自定义公告牌
前言 有时候我们希望公告牌跟随镜头旋转永远平行面向屏幕,同时跟随镜头缩放缩放大小不变(镜头远离物体,正常物体视觉效果变小,但公告牌视觉大小比例不变),或者跟随镜头缩放变化,本文记录C#脚本的两种实现方 ...
- javaweb项目部署到tomcat之后java文件没有编译
1.选中你的项目==>选择Project 2.将Build Automatcally前的对号去掉后再Clean一下你的项目 这样就可以了,
- Java操作Jxl实现导出数据生成Excel表格数据文件
实现:前台用的框架是Easyui+Bootstrap结合使用,需要引入相应的Js.Css文件.页面:Jsp.拦截请求:Servlet.逻辑处理:ClassBean.数据库:SQLserver. 注意: ...
- Hive/Impala批量插入数据
问题描述 现有几千条数据,需要插入到对应的Hive/Impala表中.安排给了一个同事做,但是等了好久,反馈还没有插入完成--看到他的做法是:对每条数据进行处理转换为对应的insert语句,但是,实际 ...
- (一)Django项目架构介绍
项目的架构为: 1.虚拟环境virtualenv 安装Django==2.1.3 安装pymysql 安装mysqlclient 安装其他等 2.项目结构为: 应用APP: blog -- 管理博客 ...
- 【笔试/面试题】中科创达——9.28(持续更新ing)
1. 线程与进程的区别 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独 ...
- Scanner使用方法
import java.util.Scanner; //导入包 public void main (String args[]){ Scanner a=new Scanner(System.in); ...
- C语言之数组用法总结
一维数组的定义:1.数组的数据类型:每一元素占内存空间的字节数.2.数组的存储类型:内存的动态. 静态存储区或CPU的寄存器.3.一维数组在内存中占用的字节数为:数组长度X sizeof (基类型). ...
- 案例分析丨谷歌设计冲刺 4 天决定 Clips 的功能特性
这次为大家带来Google冲刺日程案例,之前案例分析可以戳对应文字直达:ING集团.音乐流媒体Spotify.美国知名超市Target 作为一款工具类产品,谷歌十分注重产品候选人的技术背景,同时也看重 ...