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】

自己结合工程:

    UTF-8
    2.5.1
    2.4.0.RELEASE
    
  
    
      org.springframework.data
      spring-data-hadoop
      2.4.0.RELEASE
    
   
      org.apache.hbase
      hbase-client
      1.2.1
    

网上例子:

    
        UTF-8
        1.2.2
        2.4.0.RELEASE
    

    
        
            org.apache.hbase
            hbase-client
            ${hbase.version}
        

        
            org.springframework.data
            spring-data-hadoop
            ${spring-data-hadoop.version}
        
    

【Spring配置文件-直接指定】

  web.xml中到applicationContext.xml:

  
    contextConfigLocation
    classpath:applicationContext.xml
  
  创建一个 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

	
	
	fs.default.name=hdfs://192.168.1.100:9001
	
	
	
	
		
	

网上例子:




    
    
        fs.defaultFS="hdfs://localhost:9000"
    

    
    

    
    

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() {
            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() {
			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() {  
            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配置笔记)