栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HBase JAVA API

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HBase JAVA API

导包


    
        org.apache.hbase
        hbase-client
        1.4.6
    
    
        junit
        junit
        4.8.1
    

注意:

1、import org.apache.hadoop.hbase.util.Bytes;

用到该类下的 Bytes.toString() 将byte-->String

2、一般用 CellUtil 获取数据,而非scan 

将一条数据所有的cell列举出来,使用CellUtil从每一个cell中取出数据,不需要考虑每条数据的结构

rs.listCells()

CellUtil.clonevalue(cell)

package com.shujia;

import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Deemo02API {
    Connection conn;
    Admin admin;

    @Before
    public void createconn() throws IOException {
        //1.创建一个配置文件
        Configuration conf = HbaseConfiguration.create();
        //配置ZK的地址,通过ZK可以找到Hbase
        conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");

        //2.创建连接
        conn = ConnectionFactory.createConnection(conf);

        //3.如果需要对表结构操作 getAdmin
        //          对数据进行操作 getTable
        admin = conn.getAdmin();
    }

    
    @Test
    public void list() throws IOException {
        TableName[] tableNames = admin.listTableNames();
        for (TableName name : tableNames) {
            System.out.println(name.getNameAsString());
        }
    }


    
    @Test
    public void put() throws IOException {
        Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
        Put put = new Put("0002".getBytes());
        //相当于插入一列(一个cell)数据
        put.addColumn("cf1".getBytes(), "name".getBytes(), "李四".getBytes());
        put.addColumn("cf1".getBytes(), "age".getBytes(), "28".getBytes());
        put.addColumn("cf1".getBytes(), "phone".getBytes(), "1888887".getBytes());
        testAPI.put(put);
    }

    
    @Test
    public void get() throws IOException {
        Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
        Get get = new Get("0002".getBytes());
        Result rs = testAPI.get(get);
        //获取rk
        byte[] rk = rs.getRow();
        System.out.println(rk);
        System.out.println(Bytes.toString(rk));
        //获取value
        byte[] name = rs.getValue("cf1".getBytes(), "name".getBytes());
        System.out.println(name);
        System.out.println(Bytes.toString(name));

    }

    
    @Test
    public void createtable() throws IOException {
        HTableDescriptor student = new HTableDescriptor(TableName.valueOf("student"));
        HColumnDescriptor info = new HColumnDescriptor("info");
        student.addFamily(info);
        admin.createTable(student);
    }

    
    @Test
    public void droptable() throws IOException {
        //判断表是否存在
        TableName test1 = TableName.valueOf("test1");
        if (admin.tableExists(test1)) {
            admin.disableTable(test1);
            admin.deleteTable(test1);
        }
    }

    
    @Test
    public void modifyTable() throws IOException {
        TableName test = TableName.valueOf("test");
        //获取表原有的结构
        HTableDescriptor tableDescriptor = admin.getTableDescriptor(test);
        //在表原有的结构中 修改列簇属性
        HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
        //遍历表中原有的列簇
        for (HColumnDescriptor cf : columnFamilies){
            //对原有的cf1列簇进行修改
            if("cf1".equals(cf.getNameAsString())){
                cf.setTimeToLive(10000);
            }
        }
        //增加新的列簇
        HColumnDescriptor info = new HColumnDescriptor("info");
        tableDescriptor.addFamily(info);
        admin.modifyTable(test,tableDescriptor);
    }

    
    @Test
    public void putAll() throws IOException {
        
        BufferedReader br = new BufferedReader(new FileReader("data/students.txt"));
        
        //与Hbase中的student表建立连接
        Table student = conn.getTable(TableName.valueOf("student"));

        String line;
       
        //用集合进行批量写入
        ArrayList puts = new ArrayList();
        int batchsize=10;

        while((line=br.readLine())!=null){
            //写入Hbase
            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            String age = split[2];
            String gender = split[3];
            String clazz = split[4];
            
            //id作为rowkey
            Put put = new Put(id.getBytes());
            byte[] info = "info".getBytes();
            //添加单元格(列簇,列名,值)
            put.addColumn(info,"name".getBytes(),name.getBytes());
            put.addColumn(info,"age".getBytes(),age.getBytes());
            put.addColumn(info,"gender".getBytes(),gender.getBytes());
            put.addColumn(info,"clazz".getBytes(),clazz.getBytes());

            //每条数据都会执行依次,效率很慢
//            student.put(put);

            //将每个Put对象加入puts集合
            puts.add(put);
            //当puts集合的大小同batchsize大小一致时,则调用HTable的put方法批量写入
            if(puts.size()==batchsize){
                student.put(puts);
                //清空集合
                puts.clear();
            }
        }
        
        //当batchsize的大小同数据条数不成正比的时候,可能会造成最后几条数据不能写入
        //手动去判断puts集合是否为空,不为空则将其写入Hbase
        if(!puts.isEmpty()){
            student.put(puts);
        }

        br.close();
    }

    
    @Test
    public void getscan() throws IOException {
        Table student = conn.getTable(TableName.valueOf("student"));

        //scan可以指定rowkey的范围进行查询,或者是限制返回的条数
        Scan scan = new Scan();
        scan.withStartRow("1500100100".getBytes());
        scan.withStopRow("1500100111".getBytes());
        
        ResultScanner scanner = student.getScanner(scan);
        for (Result rs : scanner) {
            //获取rowkey
            String id = Bytes.toString(rs.getRow());
            //获取值 (列簇,列名)
            String name = Bytes.toString(rs.getValue("info".getBytes(), "name".getBytes()));
            String age = Bytes.toString(rs.getValue("info".getBytes(), "age".getBytes()));
            String gender = Bytes.toString(rs.getValue("info".getBytes(), "gender".getBytes()));
            String clazz = Bytes.toString(rs.getValue("info".getBytes(), "clazz".getBytes()));
            System.out.println(id + "," + name + "," + age + "," + gender + "," + clazz);
        }
    }

    
    @Test
    public void CellUtil() throws IOException {
        Table student = conn.getTable(TableName.valueOf("student"));

        //scan可以指定rowkey的范围进行查询,或者是限制返回的条数
        Scan scan = new Scan();
        scan.withStartRow("1500100100".getBytes());
        scan.withStopRow("1500100111".getBytes());
        scan.setLimit(5);

        for (Result rs : student.getScanner(scan)) {
            //将一条数据所有的cell列举出来
            //使用CellUtil从每一个cell中取出数据
            //不需要考虑每条数据的结构
            
            String id = Bytes.toString(rs.getRow());
            System.out.print(id+" ");
            
            List cells = rs.listCells();
            for (Cell cell : cells) {
                String val = Bytes.toString(CellUtil.clonevalue(cell));
                System.out.print(val+" ");
            }
            System.out.println();
        }
    }


    @After
    public void close() throws IOException {
        //用完记得关闭连接
        admin.close();
        conn.close();
    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632346.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号