• hbase之java api实战一


    相关接口文档:

    https://hbase.apache.org/book.html#arch.overview

    https://hbase.apache.org/apidocs/index.html

    http://hbase.apache.org/2.1/apidocs/index.html   //我的2.1.8 ,重点参考这个

    https://www.docs4dev.com/docs/zh/apache-hbase/2.1/reference   中文文档-用户手册

    第1步:创建maven项目

    创建项目hbase

    pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

      <modelVersion>4.0.0</modelVersion>

     

      <groupId>com.scitc</groupId>

      <artifactId>hbase</artifactId>

      <version>0.0.1-SNAPSHOT</version>

      <packaging>jar</packaging>

     

      <name>hbase</name>

      <url>http://maven.apache.org</url>

     

      <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <hbase.version>2.1.8</hbase.version>

      </properties>

     

      <dependencies>

     

          <dependency>

              <groupId>org.apache.hbase</groupId>

              <artifactId>hbase-server</artifactId>

              <version>${hbase.version}</version>

          </dependency>

     

          <dependency>

              <groupId>org.apache.hbase</groupId>

              <artifactId>hbase-client</artifactId>

              <version>${hbase.version}</version>

          </dependency>

     

    <!-- 这个依赖没用上

      <dependency>

        <groupId>org.apache.hbase</groupId>

        <artifactId>hbase-common</artifactId>

        <version>${hbase.version}</version>

    </dependency>  -->

     

          <dependency> 

          <groupId>jdk.tools</groupId> 

          <artifactId>jdk.tools</artifactId> 

          <version>1.8</version> 

          <scope>system</scope> 

          <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> 

       </dependency>

     

        <dependency>

          <groupId>junit</groupId>

          <artifactId>junit</artifactId>

          <version>3.8.1</version>

          <scope>test</scope>

        </dependency>

      </dependencies>

     

        <build>

        <plugins>

            <plugin>

             <groupId>org.apache.maven.plugins</groupId>

             <artifactId>maven-compiler-plugin</artifactId>

             <configuration>

             <source>1.8</source>

             <target>1.8</target>

             </configuration>

       </plugin>

      

       <plugin>

                 <artifactId>maven-assembly-plugin</artifactId>

                 <configuration>

                     <descriptorRefs>

                         <descriptorRef>jar-with-dependencies</descriptorRef>

                     </descriptorRefs>

                     <archive>

                         <manifest>

                             <mainClass></mainClass>

                         </manifest>

                     </archive>

                 </configuration>

                 <executions>

                     <execution>

                         <id>make-assembly</id>

                         <phase>package</phase>

                         <goals>

                             <goal>single</goal>

                         </goals>

                     </execution>

                 </executions>

             </plugin>

      

        </plugins>

    </build>

    </project>

    第2步:HbaseOperation

    HbaseOperation.java代码如下:

    package com.scitc.hbase;

    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;

    import java.util.List;

     

    /**

     * @author geiliHe

     * @date 2020-06-09

     */

    public class HbaseOperation {

       public static Configuration configuration;

        public static Connection connection;

        public static Admin admin;

        public static TableDescriptor tableDescriptor;

        //建立连接

        public static void init(){

            configuration  = HBaseConfiguration.create();

            configuration.set("hbase.rootdir","hdfs://192.168.56.110:9000/hbase");

            configuration.set("hbase.zookeeper.quorum", "master,slave1,slave2");

            configuration.set("hbase.zookeeper.property.client", "2181");

            try{

                connection = ConnectionFactory.createConnection(configuration);

                admin = connection.getAdmin();

            }catch (IOException e){

                e.printStackTrace();

            }

        }

        //关闭连接

        public static void close(){

            try{

                if(admin != null){

                    admin.close();

                }

                if(null != connection){

                    connection.close();

                }

            }catch (IOException e){

                e.printStackTrace();

            }

        }

       

        /**1:创建表-pass

         * @param tableName   表名

         * @param colFamily   列族

         * @throws IOException

         */

       public static void createTable(String tableName,String[] colFamily) throws IOException {

           init();

            TableName mytable = TableName.valueOf(tableName);

            if(admin.tableExists(mytable)){

                System.out.println("talbe is exists!");

            }else {

              //表描述器构造器

                TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(mytable);

                //创建集合用于存放列描述对象

                List<ColumnFamilyDescriptor> families = new ArrayList<>();

                //将每个列族对应的ColumnFamilyDescriptor对象添加到集合中

                for(String cols:colFamily) {

                   families.add(ColumnFamilyDescriptorBuilder.newBuilder(cols.getBytes()).build());

                }

                tableDescriptor = tdb.setColumnFamilies(families).build();

               //创建表

                admin.createTable(tableDescriptor);;

            }

            close();

        }

       /**

        * 2:实现单个数据插入-pass

        * 修改数和插入数据一样,不会覆盖原来版本,通过时间戳区别

        * @param tableName

        * @param rowKey

        * @param colFamily

        * @param col

        * @param val

        * @throws IOException

        */

        public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {

               init();

               Table table = connection.getTable(TableName.valueOf(tableName));

               Put put = new Put(rowKey.getBytes());

               put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());

               table.put(put);

               table.close();

               close();

           }

        /**

         * 3-1:通过行键-列族-列:获取单个数据-pass

         * @param tableName

         * @param rowKey

         * @param colFamily

         * @param col

         * @throws IOException

         */

        public static void getData(String tableName,String rowKey,String colFamily,String col)throws  IOException{

               init();

               Table table = connection.getTable(TableName.valueOf(tableName));

               Get get = new Get(rowKey.getBytes());

               get.addColumn(colFamily.getBytes(),col.getBytes());

               Result result = table.get(get);

               showCell(result);

               table.close();

               close();

           }

        /**

         * 3-2:通过行键获取值-pass

         * @param tableName

         * @param rowKey

         * @throws IOException

         */

        public static void getDataByRowkey(String tableName,String rowKey)throws  IOException{

               init();

               Table table = connection.getTable(TableName.valueOf(tableName));

               //获得一行

               Get get = new Get(Bytes.toBytes(rowKey));

               Result set = table.get(get);

               Cell[] cells = set.rawCells();

               System.out.print("查询结果");

               for (Cell cell: cells){

                   System.out.println(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()) + "::" +

                   Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));

                 }

               table.close();

               close();

               }

        /**

         * 4:给制定表添加列族

         * @param tableName

         * @param familyName

         * @throws IOException

         */

        public static void addColumnFamily(String tableName, String familyName) throws IOException {

             init();

             TableName tn = TableName.valueOf(tableName);

      

             // 通过新增的familyName来构建ColumnFamilyDescriptor对象

             ColumnFamilyDescriptor columnFamily = ColumnFamilyDescriptorBuilder.newBuilder(familyName.getBytes()).build();

             admin.addColumnFamily(tn, columnFamily);

             close();

           }

        

        /**

         * 5:删除单个数据-pass

         * @param tableName

         * @param rowKey

         * @param colFamily

         * @param col

         * @throws IOException

         */

        public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {

               init();

               Table table = connection.getTable(TableName.valueOf(tableName));

               Delete delete = new Delete(rowKey.getBytes());

               //删除指定列族

               //delete.addFamily(Bytes.toBytes(colFamily));

               //删除指定列

               //delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));

               table.delete(delete);

               table.close();

               close();

           }

        

         /**

          * 格式化输出获取的数据

          * @param result

          */

        public static void showCell(Result result){

               Cell[] cells = result.rawCells();

               for(Cell cell:cells){

                   System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");

                   System.out.println("Timetamp:"+cell.getTimestamp()+" ");

                   System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");

                   System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");

                   System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");

               }

           }

        /**

         * 6:查看已有的表-pass

         * @throws IOException

         */

           public static void listTables() throws IOException {

               init();

               TableName[] tables = admin.listTableNames();

               for(TableName table:tables) {

                 System.out.println("hbase数据库的表:"+table.toString());

               }

               close();

           }

           /**

            * 7:删除一张表

            * @param tableName

            * @throws IOException

            */

           public static void deleteTable(String tableName) throws IOException {

              init();

              TableName tn = TableName.valueOf(tableName);

              if (!admin.tableExists(tn)) {

                 System.out.print("要删除的表不存在");

              }else {

                      admin.disableTable(tn);

                      admin.deleteTable(tn);

                   }

               close();

           }

       public static void main(String[] args) throws IOException {

          //createTable("user",new String[]{"address","info"});

            //insertRow("user", "tom", "info", "age", "26");

            //insertRow("user", "tom", "info", "birthday", "1998-04-05");

            //getData("user", "tom", "info", "age");

          //deleteRow("user","tom","info","birthday"); 

          //listTables();

          getDataByRowkey("user","tom");

       }

    }

     

    参考文献:

    1:https://www.cnblogs.com/swordfall/p/10301707.html

    2:http://dblab.xmu.edu.cn/blog/1590-2/

    3:https://blog.csdn.net/snihytn/article/details/105544169

  • 相关阅读:
    nginx用户统计
    mysql用户和授权
    memcache操作
    nginx memcache缓存
    memcached安装启动
    Unity 菜单拓展
    用C#创建XML, XML格式化输出
    解析swf文件头,获取flash的原始尺寸
    kinematic与static刚体不会触发任何接触回调
    git使用
  • 原文地址:https://www.cnblogs.com/hemomo/p/13074133.html
Copyright © 2020-2023  润新知