博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HBase API 操 作
阅读量:7238 次
发布时间:2019-06-29

本文共 7538 字,大约阅读时间需要 25 分钟。

 

 

依赖:

4.0.0
com.atlxl
Hbase01
1.0-SNAPSHOT
org.apache.hbase
hbase-server
1.3.1
org.apache.hbase
hbase-client
1.3.1
commons-collections
commons-collections
3.1
commons-configuration
commons-configuration
1.5
commons-codec
commons-codec
1.10
commons-lang
commons-lang
2.5

 

 

代码:

package com.atlxl;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 java.io.IOException;import static com.sun.xml.internal.fastinfoset.alphabet.BuiltInRestrictedAlphabets.table;public class TestHbase {    private static Admin admin = null;    private static Connection connection = null;    private static Configuration configuration =null;    static {        //Hbase配置文件//        HBaseConfiguration configuration = new HBaseConfiguration();        configuration = HBaseConfiguration.create();        configuration.set("hbase.zookeeper.quorum", "192.168.192.102");        //获取连接对象        try {            connection = ConnectionFactory.createConnection(configuration);            admin = connection.getAdmin();        } catch (IOException e) {            e.printStackTrace();        }    }    private static void close(Connection conn,Admin admin) {        if (conn!=null) {            try {                conn.close();            } catch (IOException e) {                e.printStackTrace();            }        }        if (admin!=null) {            try {                admin.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }    //判断表是否存在    public static boolean tableExist(String tableName) throws IOException {        //获取HBase管理员对象//        HBaseAdmin admin = new HBaseAdmin(configuration);        //执行//        boolean tableExists = admin.tableExists(tableName);        boolean tableExists = admin.tableExists(TableName.valueOf(tableName));        //关闭资源        admin.close();        return tableExists;    }    //创建表    public static void createTable(String tableName, String... cfs) throws IOException {        if (tableExist(tableName)) {            System.out.println("表已存在!!");            return;        }        //创建表描述器        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));        //添加列族        for (String cf : cfs) {            //创建列描述器            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);            hTableDescriptor.addFamily(hColumnDescriptor);        }        //创建表的操作        admin.createTable(hTableDescriptor);        System.out.println("表创建成功!");    }    //删除表    public static void deleteTable(String tableName) throws IOException {        if (!tableExist(tableName)) {            return;        }        //删除表之前先使表不可用(下线)        admin.disableTable(TableName.valueOf(tableName));        //执行删除操作        admin.deleteTable(TableName.valueOf(tableName));        System.out.println("表已删除!!");    }    //增//改    public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {        //获取表对象        Table table = connection.getTable(TableName.valueOf(tableName));//        HTable table = new HTable(configuration, TableName.valueOf(tableName));        //创建Put对象        Put put = new Put(Bytes.toBytes(rowKey));        put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));        //执行添加操作        table.put(put);    }    //删    public static void delete(String tableName, String rowKey, String cf, String cn) throws IOException {        //获取table对象        Table table = connection.getTable(TableName.valueOf(tableName));        //创建delete对象        Delete delete = new Delete(Bytes.toBytes(rowKey));        delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));//        delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));        //执行删除操作        table.delete(delete);        table.close();    }    //查    //全表扫描    public static void scanTable(String tableName) throws IOException {        //获取table对象        Table table = connection.getTable(TableName.valueOf(tableName));        //构建扫描器        Scan scan = new Scan();//        scan.setStartRow()//        scan.setStopRow()        ResultScanner results = table.getScanner(scan);        //遍历数据并打印        for (Result result : results) {            Cell[] cells = result.rawCells();            for (Cell cell : cells) {                System.out.println("RK:" + Bytes.toString( CellUtil.cloneRow(cell)) + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))                        + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));            }        }        table.close();    }    //获取指定列族:列的数据    public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException {        //获取表对象        Table table = connection.getTable(TableName.valueOf(tableName));        //创建一个Get对象        Get get = new Get(Bytes.toBytes(rowKey));        get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));        //指定版本//        get.setMaxVersions();        //指定列族//        get.addFamily();        //获取数据的操作        Result result = table.get(get);        Cell[] cells = result.rawCells();        for (Cell cell : cells) {            System.out.println("RK:" + Bytes.toString( CellUtil.cloneRow(cell)) + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))                    + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));        }        table.close();    }    public static void main(String[] args) throws IOException {//        System.out.println(tableExist("student"));//        System.out.println(tableExist("staff"));        //调用创建表//        createTable("staff", "info");        //调用删除表//        deleteTable("staff");        //判断表是否存在//        System.out.println(tableExist("staff"));        //插入数据//        putData("student", "1002", "info", "name", "zhangsan");//        putData("student", "1002", "info", "sex", "female");//        putData("student", "1002", "info", "age", "18");//        putData("student", "1003", "info", "name", "lisi");//        putData("student", "1003", "info", "sex", "male");//        putData("student", "1004", "info", "age", "18");//        putData("student", "1004", "info", "name", "wanwu");//        delete("student", "1001", "info", "sex");//        scanTable("student");        getData("student", "1003", "info", "name");        close(connection, admin);    }}

 

转载于:https://www.cnblogs.com/LXL616/p/11013545.html

你可能感兴趣的文章
Eclipse 常用设置总结
查看>>
linux schedule() 方法浅析
查看>>
Arrays(理解)
查看>>
多线程笔记
查看>>
tomcat 常用调整
查看>>
我心目中的编程语言
查看>>
Ubuntu 常见错误--Could not get lock /var/lib/dpkg/lock
查看>>
android布局管理---第二篇表格布局
查看>>
一些经验总结
查看>>
DMURLConnection
查看>>
iframe 与主框架相互访问方法
查看>>
first
查看>>
mysql存储过程调试工具
查看>>
nginx
查看>>
解决jenkins启动完会kill掉的衍生进程
查看>>
关于Linux下s、t、i、a权限
查看>>
js 获取CSS样式
查看>>
Symfony2安装时欢迎页面CSS混乱的解决方案
查看>>
Selenium-webdriver 系列Python教程(3)————如何执行一段JS
查看>>
Apple 企业开发者账号&邓白氏码申请记录
查看>>