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

HbaseAPI操作之DDL与DML

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

HbaseAPI操作之DDL与DML

一、导入依赖

    
        org.apache.hbase
        hbase-server
        2.0.5
        
            
                org.glassfish
                javax.el
            
        
    
    
        org.apache.hbase
        hbase-client
        2.0.5
    
    
        org.glassfish
        javax.el
        3.0.1-b06
    
二、DDL
package com.hpu;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;


public class hbase_DDL {
    //单例连接
    public static Connection connection = null;

    static {
        //创建连接的配置对象
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
        try {
            //创建连接
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            System.out.println("初始化连接错误");
        }
    }

    public static void close() throws IOException {
        if (connection != null) {
            connection.close();
        }
    }

    //创建命名空间
    public static void createNamespace(String namespace) throws IOException {
        //获取admin
        Admin admin = connection.getAdmin();
        //调用方法创建命名空间
        NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(namespace);
        //对命名空间添加自定义的键值对
        builder.addConfiguration("user", "atguigu");
        try {
            admin.createNamespace(builder.build());
        } catch (NamespaceExistException e) {
            System.out.println("命名空间已经存在");
        } catch (IOException e) {
            e.printStackTrace();
        }
        admin.close();
    }

    //判断表是否存在
    public static boolean isTableExists(String namespace, String tableName) throws IOException {
        Admin admin = connection.getAdmin();
        boolean b = false;

        try {
            b = admin.tableExists(TableName.valueOf(namespace,tableName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        admin.close();
        return b;
    }

    //创建表
    public static void createTable(String namespace,String tableName,String... families) throws IOException {
        if (families.length == 0){
            System.out.println("需要一个列族");
            return;
        }
        if (isTableExists(namespace,tableName)){
            System.out.println("表格已经存在");
            return;
        }

        Admin admin = connection.getAdmin();
        //工厂模式 创建描述
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace, tableName));
        for (String family : families) {
            //创建列族描述
            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family));
            //修改单个列族的参数
            columnFamilyDescriptorBuilder.setMaxVersions(5);
            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
        }
        //调用方法创建表格
        try {
            admin.createTable(tableDescriptorBuilder.build());
        } catch (IOException e) {
            e.printStackTrace();
        }
        admin.close();

    }
    public static void main(String[] args) throws IOException {
        System.out.println(connection);
        createNamespace("bigdata");
        System.out.println(isTableExists("bigdata","stu"));
        createTable("bigdata","stu","info");
        System.out.println(isTableExists("bigdata","stu"));
        close();
    }
}
三、DML
package com.hpu;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;


import java.io.IOException;


public class hbase_DML {
    public static Connection connection = hbase_DDL.connection;

    //插入
    public static void putCell(String namespace,String tablename,String rowkey,String family,String column,String value) throws IOException {
        //得到表
        Table table = connection.getTable(TableName.valueOf(namespace, tablename));
        //调用方法写入数据
        Put put = new Put(Bytes.toBytes(rowkey));
        //给put添加数据
        put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
        try {
            table.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        }
        table.close();

    }
    //查询
    public static void getcells(String namespace,String tablename,String rowkey,String family,String column) throws IOException {
        Table table = connection.getTable(TableName.valueOf(namespace, tablename));
        //调用方法get数据
        Get get = new Get(Bytes.toBytes(rowkey));
        //设置get能够读取多个版本的数据
        get.readAllVersions();
        //添加信息可以读一列的数据
        get.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
        //如果不给get添加列的信息,可以直接读取一行的数据
        try {
            Result result = table.get(get);
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println(new String(CellUtil.clonevalue(cell)));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        table.close();

    }
    //删除
    public static void deleteCells(String namespace,String tablename,String rowkey,String family,String column) throws IOException {
        Table table = connection.getTable(TableName.valueOf(namespace, tablename));

        Delete delete = new Delete(Bytes.toBytes(rowkey));
        //delete.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
        delete.addColumns(Bytes.toBytes(family),Bytes.toBytes(column));
        try {
            table.delete(delete);
        } catch (IOException e) {
            e.printStackTrace();
        }
        table.close();
    }
    //扫描
    public static void scanTable(String namespace,String tablename,String startRow,String stopRow) throws IOException {
        Table table = connection.getTable(TableName.valueOf(namespace, tablename));
        Scan scan = new Scan();
         填写开始的rowKey范围和结束的rowKey范围,默认使用左闭右开
        scan.withStartRow(Bytes.toBytes(startRow));
        scan.withStopRow(Bytes.toBytes(stopRow));
        // 开启raw原始数据模式,可以读取多个版本的数据,超过列族维护数据的限制
        scan.setRaw(true);
        scan.readVersions(5);
        try {
            ResultScanner scanner = table.getScanner(scan);
            for (Result result : scanner) {
                Cell[] cells = result.rawCells();
                for (Cell cell : cells) {
                    System.out.print(new String(CellUtil.cloneRow(cell)) + "-"+new String(CellUtil.cloneFamily(cell))+"-" +new String(CellUtil.cloneQualifier(cell))+"-"+new String(CellUtil.clonevalue(cell))+"t");
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        table.close();
    }
    public static void main(String[] args) throws IOException {
        putCell("bigdata","stu","1001","info","age","18");
        getcells("bigdata","stu","1001","info","age");
        deleteCells("bigdata","stu","1001","info","age");
        getcells("bigdata","stu","1001","info","age");
        scanTable("default","stu","1001","1006");
    }
}

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

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

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