Hbase与Maven工程的Spring配置笔记
来源:互联网 发布:手机淘宝排行榜 编辑:程序博客网 时间:2024/06/06 20:31
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/hadoophttp://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 略;@Componentpublic class HBaseDaoImpl implements IHBaseDao {@Resource(name="hbaseTemplate")private HbaseTemplate hbaseTemplate;@Overridepublic 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; }可以测试了,以上是最简单的调通流程,真正使用得还好翻官方文档,不断深入!
0 0
- Hbase与Maven工程的Spring配置笔记
- maven与spring的配置
- maven与spring的配置
- maven工程的一些配置
- Maven的初学笔记-安装与配置
- spring的maven配置
- Spring的Maven配置
- IDEA17用Maven创建SpringMVC+Spring+Mybatis工程及整合配置与实例
- Maven学习笔记(二) :Maven的安装与配置
- Maven实战学习笔记 2.Maven的安装与配置
- Maven学习笔记一:Maven的安装与配置
- JAVA程序操作hbase的Maven配置
- 用maven创建spring的hello工程
- maven 创建spring工程遇到的问题
- Spring Boot+Maven 工程配置h2内存数据库
- Spring Boot+Maven 工程配置H2内存数据库
- MyEclipse + Maven开发Web工程的配置
- MyEclipse + Maven开发Web工程的配置
- java中的md5加密
- javaScript 引用位置
- 算法导论二叉搜索树
- 历届试题 城市建设 (两次Kruskal)
- 归并排序
- Hbase与Maven工程的Spring配置笔记
- 2017跟着小虎玩着去软考--项目管理师上午考试试题之六(解析2016下半年项目管理师上午试题20-23题)
- 第一章 数据库系统引论
- 从Slice_Header学习H.264(二)--片头的子语法项目
- JAVA——多态
- 【Python学习】pyinstaller使用
- 对Java的内存分配的简单了解(1)
- 一种批量删除数据的方法
- 网易17春招笔试题