• 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;
        }  
      可以测试了,以上是最简单的调通流程,真正使用得还好翻官方文档,不断深入!
  • 相关阅读:
    preg_match()
    Ubuntu解压缩zip,tar,tar.gz,tar.bz2文件命令
    Couchbase集群
    画图工具
    谷歌打不开
    筛选重复数据的方法
    div垂直居中的N种方法 单行/多行文字(未知高度/固定高度)
    ie6 ol 序列号 bug
    IE6、7下li元素的子元素为dl,ul,ol时产生的bug
    ie6 line-height bug解决办法
  • 原文地址:https://www.cnblogs.com/sandeepin/p/12236525.html
Copyright © 2020-2023  润新知