springboot hbase 操作 demo

数据存储

2018-08-28

2395

0

技术:springboot + java8 + hbase

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

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

applicationContext.xml

 

hbase-site.xml


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

测试类

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() {
            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