• 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()));
            }
        }
    }
  • 相关阅读:
    往鼠标位置写入 诗词
    shell条件判断
    shell 案例一
    Shell中的变量
    echo e 命令详解
    Docker 安装RedisJSON 与使用
    Python 插入数据库的各种方式
    Python压缩文件/文件夹
    shell运算符
    关于CAN总线简单总结
  • 原文地址:https://www.cnblogs.com/wangshuang123/p/10981008.html
Copyright © 2020-2023  润新知