zoukankan      html  css  js  c++  java
  • hbase的API

    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 java.util.ArrayList;
    
    
    public class HbaseClients {
        public static  Connection connection;
        public static void main(String[] args) throws Exception {
            create("kgc","cf1","cf2","cf3");
            //delete("kgc");
            //scan("test02");
        }
        //初始化的参数,连接对象
        static {
            //conf
            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "192.168.159.110");
            conf.set("hbase.zookeeper.property.clientPort","2181");
            //获取hbase链接对象ConnectionFactory
            try {
                connection = ConnectionFactory.createConnection(conf);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //创建表
        public static void create(String table,String ... familys) throws Exception {
            //首先要拿到admin
            Admin admin = connection.getAdmin();
            //new
            HTableDescriptor htable = new HTableDescriptor(TableName.valueOf(table));
            //htable
            for (String family : familys) {
                htable.addFamily(new HColumnDescriptor(family));
            }
            admin.createTable(htable);
            System.out.println("创建成功");
        }
        //删除表
        public static void delete(String table) throws Exception {
            Admin admin = connection.getAdmin();
            //判断表是否存在
            if(admin.tableExists(TableName.valueOf(table))){
                //下线了
                admin.disableTable(TableName.valueOf(table));
                //删除
                admin.deleteTable(TableName.valueOf(table));
                System.out.println("删除成功");
            }else{
                System.out.println("对不起,表不存在");
            }
        }
        //添加数据
        public static void adddata(String table,String rowkey,String columnFamily,String column,String value) throws Exception {
            Table table1 = connection.getTable(TableName.valueOf(table));
            //NEW  PUT
            Put puts = new Put(Bytes.toBytes(rowkey));
            //puts
            puts.add(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
            table1.put(puts);
            System.out.println("添加数据成功");
        }
        //删除一行(rowkey)数据
        public static void delterow(String table,String  rowkey) throws Exception {
            Table table1 = connection.getTable(TableName.valueOf(table));
            //new Delete
            Delete deletes = new Delete(Bytes.toBytes(rowkey));
            //删除
            table1.delete(deletes);
            System.out.println("删除一行数据成功");
        }
        //删除多行数据
        public static void deleterows(String table,String ... rowkeys) throws Exception {
            Table table1 = connection.getTable(TableName.valueOf(table));
            //new List
            ArrayList<Delete> list = new ArrayList<>();
            for (String rowkey : rowkeys) {
                list.add(new Delete(Bytes.toBytes(rowkey)));
            }
            table1.delete(list);
            System.out.println("删除多行成功");
        }
        //scan扫描
        public static void scan(String table) throws Exception {
            Table table1 = connection.getTable(TableName.valueOf(table));
            //new Scan
            Scan scan = new Scan();
            //results 就是所有的rowkey的集合
            ResultScanner results = table1.getScanner(scan);
            for (Result result : results) {
                //cells 的集合
                Cell[] cells = result.rawCells();
                for (Cell cell : cells) {
                    System.out.println(Bytes.toString(cell.getRow()));
                    System.out.println(Bytes.toString(cell.getFamily()));
                    System.out.println(Bytes.toString(cell.getQualifier()));
                    System.out.println(Bytes.toString(cell.getValue()));
                }
            }
        }
        //get
        public static void get(String table,String rowkey) throws Exception {
            Table table1 = connection.getTable(TableName.valueOf(table));
            //new Get
            Get get = new Get(Bytes.toBytes(rowkey));
            Result result = table1.get(get);
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println(Bytes.toString(cell.getRow()));
                System.out.println(Bytes.toString(cell.getFamily()));
                System.out.println(Bytes.toString(cell.getQualifier()));
                System.out.println(Bytes.toString(cell.getValue()));
            }
        }
    }
  • 相关阅读:
    SVN配置文件详解
    让Linux开机运行命令
    find命令用法介绍
    关于js优化和css优化
    弹性盒布局、头尾固定中间部分自适应布局
    css样式重置(初始化)收集
    动态嵌套form,使用Stimulus Js库(前后端不分离)
    给Mac的Dictionary添加其他原装词典
    使用rvm关联ruby版本和rails版本。
    Rails6.0 Beta版本1: ActionText的简单使用
  • 原文地址:https://www.cnblogs.com/wangshuang123/p/10981008.html
Copyright © 2011-2022 走看看