• 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()));
            }
        }
    }
  • 相关阅读:
    json数据在前端(javascript)和后端(php)转换
    几个提高效率的PHOTOSHOP秘密快捷键
    移动端ios针对input虚拟键盘挡住的问题
    前端适配移动端的方法
    完美兼容IE10以下所有版本
    vscode vue文件格式化没效果
    官网顶部的标题左移动
    模拟后台一次性返回所有数据
    关于上传图片的问题
    iframe标签在PC端的使用
  • 原文地址:https://www.cnblogs.com/wangshuang123/p/10981008.html
Copyright © 2020-2023  润新知