zoukankan      html  css  js  c++  java
  • HBase-Java Native API操作

    1.准备工作

      需要将Linux系统下的hbase-site.xml文件和hdfs-site.xml文件放置在项目的resources的路径下;

      

    2.导入依赖

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
    
        <dependency>
          <groupId>org.apache.hbase</groupId>
          <artifactId>hbase-client</artifactId>
          <version>1.3.1</version>
        </dependency>
        <dependency>
          <groupId>org.apache.hbase</groupId>
          <artifactId>hbase-server</artifactId>
          <version>1.3.1</version>
        </dependency>
        <dependency>
          <groupId>org.apache.hbase</groupId>
          <artifactId>hbase-common</artifactId>
          <version>1.3.1</version>
        </dependency>
        <dependency>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          <version>1.2</version>
        </dependency>
        <dependency>
          <groupId>org.apache.hadoop</groupId>
          <artifactId>hadoop-hdfs</artifactId>
          <version>2.5.1</version>
        </dependency>
        <dependency>
          <groupId>org.apache.hadoop</groupId>
          <artifactId>hadoop-client</artifactId>
          <version>2.5.1</version>
        </dependency>
        <dependency>
          <groupId>org.apache.hadoop</groupId>
          <artifactId>hadoop-common</artifactId>
          <version>2.5.1</version>
        </dependency>
        <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.17</version>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.7.12</version>
        </dependency>
    
      </dependencies>

    3.编写开关连接的方法

        static Configuration configuration;
        static Connection connection;
        static Admin admin;
        
        //初始化核心对象的方法
        public static void init() throws IOException {
            configuration= HBaseConfiguration.create();
            connection= ConnectionFactory.createConnection(configuration);
            admin=connection.getAdmin();
        }
        
        //关闭核心对象的方法
        public static void close() throws IOException {
            if (admin!=null){
                admin.close();
            }
            if (connection!=null){
                connection.close();
            }
        }

    4.创建数据表

        //创建数据表
        public static void createTable() throws IOException {
            //表名
            TableName tableName = TableName.valueOf("studentinfo");
            if (admin.tableExists(tableName)){
                System.out.println("表名已经存在!");
            }else{
                //创建表的描述信息
                HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
                //列族的描述类
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("stu".getBytes());
                //绑定
                hTableDescriptor.addFamily(hColumnDescriptor);
                //根据表的描述对象创建表
                admin.createTable(hTableDescriptor);
            }
        }
        public static void main(String[] args) throws IOException {
            init();     //初始化
            createTable();      //创建数据表
            close();    //关闭
        }

      

    5.新增数据

        //新增数据
        public static void insert() throws IOException {
            //获取表对象
            Table table = connection.getTable(TableName.valueOf("studentinfo"));
            Put put01 = new Put("1111".getBytes());
            put01.add("stu".getBytes(),"name".getBytes(),"zhangsan".getBytes());
            put01.add("stu".getBytes(),"age".getBytes(),"12".getBytes());
            put01.add("stu".getBytes(),"sex".getBytes(),"man".getBytes());
            Put put02 = new Put("2222".getBytes());
            put02.add("stu".getBytes(),"name".getBytes(),"lisi".getBytes());
            put02.add("stu".getBytes(),"age".getBytes(),"15".getBytes());
            put02.add("stu".getBytes(),"sex".getBytes(),"nv".getBytes());
            table.put(put01);
            table.put(put02);
            table.close();
        }
        public static void main(String[] args) throws IOException {
            init();     //初始化
            insert();  //新增数据
            close();    //关闭
        }

      

    6.查询表中所有的数据

        //查询表中所有数据
        public static void getData() throws IOException {
            //获取表对象
            Table table = connection.getTable(TableName.valueOf("studentinfo"));
            //创建ResultScanner对象
            ResultScanner scanner = table.getScanner(new Scan());
            for (Result result:scanner){
                for(Cell cell:result.rawCells()){
                    System.out.println("rowkey:"+Bytes.toString(result.getRow()));
                    System.out.print("family:"+Bytes.toString(CellUtil.cloneFamily(cell))+"	");
                    System.out.print("column:"+Bytes.toString(CellUtil.cloneQualifier(cell))+"	");
                    System.out.print("value:"+Bytes.toString(CellUtil.cloneValue(cell)));
                    System.out.println();
                    System.out.println();
                }
            }
            table.close();
        }
        public static void main(String[] args) throws IOException {
            init();     //初始化
            getData();  //查询表中所有数据
            close();    //关闭
        }

      

    7.查询指定Row Key中的数据

        //查询指定Row Key中的数据
        public static void getDataByRowKey() throws IOException {
            //获取表对象
            Table table = connection.getTable(TableName.valueOf("studentinfo"));
            Get get = new Get(Bytes.toBytes("1111"));
            //创建result
            Result result = table.get(get);
            for (Cell cell:result.rawCells()){
                System.out.println("rowkey:"+Bytes.toString(result.getRow()));
                System.out.print("family==>"+Bytes.toString(CellUtil.cloneFamily(cell))+"	");
                System.out.print("column==>"+Bytes.toString(CellUtil.cloneQualifier(cell))+"	");
                System.out.print("value==>"+Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println();
                System.out.println();
            }
            table.close();
        }
        public static void main(String[] args) throws IOException {
            init();     //初始化
            getDataByRowKey();  //查询指定Row Key中的数据
            close();    //关闭
        }

      

    8.删除指定Row Key中的数据

        //删除指定Row Key中的数据
        public static void deleteDataRowKey() throws IOException {
            //获取表对象
            Table table = connection.getTable(TableName.valueOf("studentinfo"));
            //设置要删除的rowkey
            Delete delete = new Delete(Bytes.toBytes("2222"));
            table.delete(delete);
            table.close();
        }
        public static void main(String[] args) throws IOException {
            init();     //初始化
            deleteDataRowKey(); //删除指定Row Key中的数据
            close();    //关闭
        }

      

    9.删除指定列族的的列

        //删除指定列族的数据
        public static void deleteDataColumnFamily() throws IOException {
            //获取表对象
            Table table = connection.getTable(TableName.valueOf("studentinfo"));
            //设置要删除的rowkey
            Delete delete = new Delete(Bytes.toBytes("1111"));
            //设置指定列族
            delete.addFamily(Bytes.toBytes("stu"));
            table.delete(delete);
            table.getClass();
        }
        public static void main(String[] args) throws IOException {
            init();     //初始化
            deleteDataColumnFamily();   //删除指定列族的数据
            close();    //关闭
        }

      

    10.删除表

        //删除表
        public static void deleteTable() throws IOException {
            TableName tableName = TableName.valueOf("studentinfo");
            //判断表是否存在
            if (admin.tableExists(tableName)){
                admin.disableTable(tableName);
                admin.deleteTable(tableName);
            }
        }
        public static void main(String[] args) throws IOException {
            init();     //初始化
            deleteTable();  //删除表
            close();    //关闭
        }

      

  • 相关阅读:
    OLEDB 数据变更通知
    Python处理正则表达式超时的办法
    OLEDB 静态绑定和数据转化接口
    Keepalived+LVS(DR)+MySQL
    Keepalived+Nginx实现Nginx的高可用
    ISCSI存储
    memcached随笔练习
    Varnish快速安装及测试
    Oracle及其相关软件历史版本下载地址
    使用VMwaver 克隆CentOS 6.9网卡配置报错
  • 原文地址:https://www.cnblogs.com/wnwn/p/12870217.html
Copyright © 2011-2022 走看看