springboot hbase 操作 demo

2018-08-28

1016

0

技术:springboot + java8 + hbase

运行环境:IDEA 15.2 + jdk8 + windows 10

demo功能:提供一个springboot操作hbase的入门demo

applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:hdp="http://www.springframework.org/schema/hadoop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.demo"/>
    <hdp:configuration id="hadoopConfiguration" resources="hbase-site.xml"/>
    <hdp:hbase-configuration id="hbaseConfiguration" configuration-ref="hadoopConfiguration"/>
    <bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
        <property name="configuration" ref="hbaseConfiguration"/>
    </bean>
</beans>

hbase-site.xml

<configuration>
    <property>
        <name>hbase.cluster.distributed</name>
        <value>true</value>
    </property>
    <property>
        <name>zookeeper.znode.parent</name>
        <value>/hbase</value>
    </property>
    <property>
        <name>hbase.rootdir</name>
        <value>hdfs://127.0.0.1/hbase</value>
    </property>
    <property>
        <name>hbase.zookeeper.quorum</name>
        <value>127.0.0.1</value>
    </property>
    <property>
        <name>hbase.zookeeper.property.clientPort</name>
        <value>2181</value>
    </property>
    <property>
        <name>hbase.client.retries.number</name>
        <value>3</value>
    </property>
</configuration>

测试类

package com.demo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.hadoop.hbase.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
public class BaseTest {
    private static final String TABLE_NAME = "vkpush-001";
    private static final String FAMILY = "family-001";
    private static final String COLUMN = "column-001";

    private static final String ZK_ADDRESS = "l-hbase-server1.pub.dev.ten.dm:2181,l-hbase-server2.pub.dev.ten.dm:2181,l-hbase-server3.pub.dev.ten.dm:2181";

    @Autowired
    private HbaseTemplate template;


    @Test
    public void testAddTable() throws IOException {
        addTable("rowkey-001".getBytes(), FAMILY.getBytes(), COLUMN.getBytes(), "hi world".getBytes());
    }


    @Test
    public void testPut() {
        template.put(TABLE_NAME, "rowkey-002", FAMILY, "name", Bytes.toBytes("Alice"));
    }

    @Test
    public void get() {
        String str = template.get(TABLE_NAME, "rowkey-002", new RowMapper<String>() {
            public String mapRow(Result result, int i) throws Exception {
                return new String(result.getValue(FAMILY.getBytes(), "name".getBytes()));
            }
        });
        Assert.assertTrue("Alice".equalsIgnoreCase(str));
    }

    public void addTable(byte[] rowKey, byte[] family, byte[] colum, byte[] value) throws IOException {
        Configuration config = HBaseConfiguration.create();
        config.set(HConstants.ZOOKEEPER_QUORUM, ZK_ADDRESS);
        TableName tn = TableName.valueOf(TABLE_NAME);
        try (Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(tn);) {
            HTableDescriptor tableDescriptor = new HTableDescriptor(tn);
            tableDescriptor.addFamily(new HColumnDescriptor(family));
            Admin admin = connection.getAdmin();
            if (!admin.tableExists(tn)) {
                admin.createTable(tableDescriptor);
            }
            Put put = new Put(rowKey);
            put.addColumn(family, colum, value);
            table.put(put);
        }
    }
}
 

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think