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配置笔记的更多相关文章

  1. 整合多个maven工程时Spring配置加载JDBC问题

    问题叙述: 两个工程都通过JDBC访问mysql数据库,各自运行OK, 但合并成一个maven工程后,发现前一个工程访问数据库异常,貌似拿不到自己的DAO. 解决办法: 发现这两个工程的xml配置中, ...

  2. Maven 工程下 Spring MVC 站点配置 (三) C3P0连接池与@Autowired的应用

    Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 前两篇文章主要是对站点和数据库操作配置进行了演示,如果单 ...

  3. Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作

    详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...

  4. Maven 工程下 Spring MVC 站点配置 (一)

    最近,查找一些具体资料时,虽然会有很多,但是系统的却很少,尤其是对maven 下 spring mvc 站点搭建的配置,总是说的很多但让新手一目了然的步骤却少之又少. 对此闲暇时整理了一下,做了一套较 ...

  5. spring 学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试

    spring学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试 ioc 概念 控制反转(Inversion of Control,缩写为IOC),是面向 ...

  6. Maven工程搭建spring boot+spring mvc+JPA

    添加Spring boot支持,引入相关包: 1.maven工程,少不了pom.xml,spring boot的引入可参考官网: <parent> <groupId>org.s ...

  7. 使用netbeans 搭建maven工程 整合spring springmvc框架

    使用netbeans7.4 自带的tomcat7.0 所以jdk选择7.xx 然后等待生成空的工程,会缺一些文件夹,和文件,后续需要的时候补齐 然后修改pom.xml添加引用,直接覆盖dependen ...

  8. javaEE中的spring配置笔记

    0 JavaEE的工程目录 0.1 WebContent     项目的主目录,在eclipse新建工程时可以自己命名,部署时会把该文件夹的内容发布到tomcat的webapps里. 该目录下可以建立 ...

  9. idea破解版安装、配置jdk以及建立一个简单的maven工程

    idea破解版安装.配置jdk,配置jdk环境变量以及建立一个简单的maven工程 一.idea破解版以及配置文件下载 下载网址:https://pan.baidu.com/s/1yojA51X1RU ...

随机推荐

  1. blink接收器

    blink: [autorun] OPEN="AutoInst.exe"  [AskRebootTitle] Dlg1=System Settings Change Dlg2=št ...

  2. 破解第一个程序----分析APK文件

    反编译APK成功后,在outdir目录下会生成一系列目录与文件. smali:程序所有的反汇编代码: res:程序中所有的资源文件: 如何寻找突破口是分析程序的关键.错误提示一般是指引关键代码的风向标 ...

  3. 浅析 Nginx 网络事件

    Nginx 是一个事件驱动的框架,所谓事件主要指的是网络事件,Nginx 每个网络连接会对应两个网络事件,一个读事件一个写事件.在深入了解 Nginx 各种原理及在极端场景下的一些错误场景处理时,需要 ...

  4. mac 访达修改所有文件夹默认排序方式

    先说个误区,下图只能改变当前目录的排序方式 修改所有目录的排序方式需要在顶部的“显示” 中修改

  5. springboot多租户设计

    1. 概述 根据不同用户的请求,选择不同的数据源,不同的数据源可以是Oracle.MySQL或者其它.用到的技术栈,没有什么复杂的技术,可以看到,依赖也就加了几个而已,如下: 2. 先睹为快 如下图, ...

  6. MySQL基础篇(03):系统和自定义函数总结,触发器使用详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.系统封装函数 MySQL 有很多内置的函数,可以快速解决开发中的一些业务需求,大概包括流程控制函数,数值型函数.字符串型函数.日期时间函数 ...

  7. lua字符串分割函数[适配中文特殊符号混合]

    lua的官方函数里无字符串分割,起初写了个简单的,随之发现如果是中文.字符串.特殊符号就会出现分割错误的情况,所以就有了这个zsplit. function zsplit(strn, chars) f ...

  8. .NET 半天搭建Jenkins持续集成与自动化部署系统

    前言 相信每一位程序员都经历过深夜加班上线的痛苦!而作为一个加班上线如家常便饭的码农,更是深感其痛.由于我们所做的系统业务复杂,系统庞大,设计到多个系统之间的合作,而核心系统更是采用分布式系统架构,由 ...

  9. Java 从入门到进阶之路(二十)

    在之前的文章我们介绍了一下 Java 中的包装类,本章我们来看一下 Java 中的日期操作. 在我们日常编程中,日期使我们非常常用的一个操作,比如读写日期,输出日志等,那接下来我们就看一下 Java ...

  10. 使用git pull拉取代码的时候,无法拉取最新代码,报"unable to update local ref"错误。

    使用git pull拉取代码的时候,无法拉取最新代码,报"unable to update local ref"错误. 除了重新clone一份代码外,还可以使用如下解决方案: .切 ...