HBaseDemo一例

HBaseDemo一例
连接的是阿里去的HBaseServerless版本2.0数据库

测试执行输出的结果为:

tablename=mytable
this is value
Found row : keyvalues={rowkey1/cf:col1/1584855631541/Put/vlen=13/seqid=0}
列  族:cf
列  名:col1
列  值:this is value
时间戳:1584855631541

java代码如下所示:

package com.cwgis.hbase;

import com.cwgis.restApp;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HBaseDemo {
    private static final String TABLE_NAME = "mytable";
    private static final String CF_DEFAULT = "cf";
    public static final byte[] QUALIFIER = "col1".getBytes();
    private static final byte[] ROWKEY = "rowkey1".getBytes();
    //
    public static void test(String[] args) {
        Configuration config = HBaseConfiguration.create();
        //
        String zkAddress = restApp.hbase_url;
        config.set(HConstants.ZOOKEEPER_QUORUM, zkAddress);
        // 设置用户名密码,默认root:root,可根据实际情况调整
        config.set("hbase.client.username", restApp.hbase_user);
        config.set("hbase.client.password", restApp.hbase_password);
        config.set("hbase.client.connection.impl", AliHBaseUEClusterConnection.class.getName());
        //
        Connection connection = null;
        try {
            //创建一个连接到hbase对象,根据zookeeper集群参数
            connection = ConnectionFactory.createConnection(config);
            //创建一个mytable表(cf族)
            HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
            tableDescriptor.addFamily(new HColumnDescriptor(CF_DEFAULT));
            Admin admin = connection.getAdmin();
            if(admin.tableExists(TableName.valueOf(TABLE_NAME))==false) {
                System.out.print("Creating table. ");
                admin.createTable(tableDescriptor);
                System.out.println(" Done.");
            }
            Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
            try {
                HTableDescriptor[] tables=admin.listTables();
                if(tables!=null && tables.length>0)
                {
                    String tabName="";
                    for(HTableDescriptor t_table :tables)
                    {
                        tabName=t_table.getTableName().toString();
                        System.out.println("tablename="+tabName);
                    }
                }
                //添加一行rowkey1关健值和向cf族添加一个字段col1列值为"this is value"
                Put put = new Put(ROWKEY);   //ROWKEY="rowkey1"
                put.addColumn(CF_DEFAULT.getBytes(), QUALIFIER, "this is value".getBytes());
                table.put(put);
                //获取hbase表mytable中一行为rowkey1的记录中的cf族下col1列的当前值"this is value"并输出显示
                Get get = new Get(ROWKEY);
                Result r = table.get(get);
                byte[] b = r.getValue(CF_DEFAULT.getBytes(), QUALIFIER);  // returns current version of value
                System.out.println(new String(b));
                //测试扫描过滤器功能
                scan_filter(TABLE_NAME,connection);
                //测试获取行值功能
                getRow(TABLE_NAME,"rowkey1",connection);
            } finally {
                if (table != null) table.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void createTable(String tableName, List<String> columnFamilyList, Admin admin) {
        try {
            //创建表TableDescriptor----------2
            HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
            //增加列族-----------------------3
            for (String columnFamilyName : columnFamilyList) {
                tableDescriptor.addFamily(new HColumnDescriptor(columnFamilyName));
            }
            //建表---------------------------4
            admin.createTable(tableDescriptor);
        } catch (ZooKeeperConnectionException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public static void addRecord(String tableName, String rowKey, String family, String qualifier1, String value1,
                                 String qualifier2, String value2, String qualifier3, String value3,Connection connection) throws Exception {
        try {
            //获取连接
            //建立表对象,用于和HBase通信-------1
            //过期获取方式:HTable table = new HTable(configuration, tablename);
            Table table = connection.getTable(TableName.valueOf(tableName));
            //创建Put对象-----------------------2
            Put put = new Put(Bytes.toBytes(rowKey));
            //----------------------------------3
            //添加数据数据,add()函数三个参数分别代表:列族、列、值
            //put.add()方法已过期,最新使用addColumn()代替
            put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier1), Bytes.toBytes(value1));
            put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier2), Bytes.toBytes(value2));
            put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier3), Bytes.toBytes(value3));
            //保存------------------------------4
            table.put(put);
            //别忘了关闭------------------------5
            table.close();
            System.out.println("insert recored " + rowKey + " to table " + tableName + " ok.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void deleteRow(String tablename, String rowkey,Connection connection) {
        try {
            //获取连接
            //建立表对象,用于和HBase通信-------1
            Table table = connection.getTable(TableName.valueOf(tablename));
            List list = new ArrayList();
            Delete d1 = new Delete(rowkey.getBytes());
            //删除多行则将多行rowkey加入列表----2
            list.add(d1);
            //删除------------------------------3
            table.delete(list);
            //----------------------------------4
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void dropTable(String tableName,Admin admin) {
        try {
            //获取连接,连接可以在一批操作前创建并复用,此处为方便演示
            //实例化admin--------------------1
            //关闭表-------------------------2
            TableName tabName=TableName.valueOf(tableName);
            admin.disableTable(tabName);
            //删除表-------------------------3
            admin.deleteTable(tabName);
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //修改表格数据(表中某一行的某一列)
    public static void updateTable(String tbName,String colFamily,String column,String rowKey,String strNewValue,Connection connection)
            throws IOException
    {
        //表名
        //String tbName = "tableName";
        //列族
        //String colFamily = "colFamily";
        //列名
        //String column = "columnName";
        //rowKey
        //String rowKey = "rowKey";
        //更新值
        //String strNewValue = "NewValues";
        //获取连接
        //建立表对象,用于和HBase通信-------1
        Table table = connection.getTable(TableName.valueOf(tbName));
        //----------------------------------2
        Put put = new Put(Bytes.toBytes(rowKey));
        //插入操作(列族,列,新值)--------3
        put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(column), Bytes.toBytes(strNewValue));
        //----------------------------------4
        table.put(put);
        //----------------------------------5
        table.close();
    }

    public static void getRow(String tbName,String rowKey,Connection connection)
            throws IOException
    {
        //表名
        //String tbName = "tableName";
        //rowKey
        //String rowKey = "rowKey";
        //-----------------------------------1
        Table table =connection.getTable(TableName.valueOf(tbName));

        //查询器,查询指定行-----------------2
        Get get = new Get(Bytes.toBytes(rowKey));
        //获取结果---------------------------3
        Result result = table.get(get);
        //指定行的所有列
        List<Cell> listCells = result.listCells();

        //遍历取值---------------------------4
        for (Cell cell : listCells) {
            System.out.println("列  族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列  名:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("列  值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("时间戳:" + cell.getTimestamp());
        }
        //关闭table-------------------------5
        table.close();
    }

    public static  void scan(String tableName,Connection connection) throws IOException{
        //获取连接

        //建立表对象,用于和HBase通信--------1
        Table table = connection.getTable(TableName.valueOf(tableName));
        //建立Scan对象----------------------2
        Scan scan = new Scan();
        // 设置要查询的列族、列--------------3
        scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"));
        // 获取结果-------------------------4
        ResultScanner resultScanner = table.getScanner(scan);
        // 读取结果-------------------------5
        for (Result result:resultScanner)
            System.out.println("Found row : " + result);

        //关闭ResultScanner----------------6
        resultScanner.close();
    }

    public static void scan_filter(String tableName,Connection connection)
            throws IOException
    {
        //获取连接
        //建立表对象,用于和HBase通信--------1
        Table table = connection.getTable(TableName.valueOf(tableName));
        //建立Scan对象----------------------2
        Scan scan = new Scan();
        // 设置要查询的列簇、列--------------3
        scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"));

        //创建Filter------------------------3.1
        Filter filter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
                new BinaryComparator(Bytes.toBytes("rowkey1")));
        //----------------------------------3.2
        scan.setFilter(filter);

        // 获取结果-------------------------4
        ResultScanner resultScanner = table.getScanner(scan);
        // 读取结果-------------------------5
        for (Result result : resultScanner)
            System.out.println("Found row : " + result);
        //关闭ResultScanner----------------6
        resultScanner.close();
    }
}

pom.xml配置信息如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<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.cwgis</groupId>
  <artifactId>rest</artifactId>
  <version>1.0</version>
  <name>rest</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.cwgis.net</url>

  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->
      <java.version>1.8</java.version>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.16</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>RELEASE</version>
    </dependency>
    <!--
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-logging</artifactId>
        <version>RELEASE</version>
    </dependency> -->

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>RELEASE</version>
    </dependency>
  <!-- spring boot -->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
          <!--排除安全包 -->
          <exclusion>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-security</artifactId>
          </exclusion>
      </exclusions>
  </dependency>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-logging</artifactId>
      <!--
      <exclusions>
          <exclusion>
              <groupId>*</groupId>
              <artifactId>*</artifactId>
          </exclusion>
      </exclusions> -->
  </dependency>
  <!-- 添加fastjson 依赖包. -->
  <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.15</version>
  </dependency>

  <!-- spring boot devtools 依赖包. -->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
      <scope>compile</scope>
  </dependency>

  <!--数据库mysql sqlite postsql-->
  <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.23</version>
  </dependency>
  <dependency>
      <groupId>org.xerial</groupId>
      <artifactId>sqlite-jdbc</artifactId>
      <version>3.7.2</version>
  </dependency>
  <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>42.1.1</version>
  </dependency>
    <!--hbase aliyun-->
    <dependency>
        <groupId>com.aliyun.hbase</groupId>
        <artifactId>alihbase-client</artifactId>
        <version>1.1.12</version>
        <exclusions>
            <exclusion>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
            </exclusion>
       </exclusions>
    </dependency>
    <dependency>
        <groupId>com.alibaba.hbase</groupId>
        <artifactId>alihbase-connector</artifactId>
        <version>1.0.12</version>
    </dependency>
    <!--
    <dependency>
        <groupId>jdk.tools</groupId>
        <artifactId>jdk.tools</artifactId>
        <version>1.6</version>
        <scope>system</scope>
        <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
    </dependency> -->


    <!--swagger-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.8.0</version>
    </dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  <plugins>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
              <source>1.8</source>
              <target>1.8</target>
              <encoding>UTF-8</encoding>
          </configuration>
      </plugin>
      <!-- 以下配置为打包时把资源根jar包分离 mvn clean package -DiskTests -->
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
              <includes>
                  <include>
                      <groupId>null</groupId>
                      <artifactId>null</artifactId>
                  </include>
              </includes>
              <layout>JAR</layout>
              <addResources>true</addResources>
          </configuration>
          <executions>
              <execution>
                  <goals>
                      <goal>repackage</goal>
                  </goals>
                  <configuration>
                      <!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
                      <!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
                      <!--<classifier>run</classifier>-->
                  </configuration>
              </execution>
          </executions>
      </plugin>

      <!--打包jar-->
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <configuration>
              <!--不打包资源文件-->
              <excludes>
                  <exclude>*.**</exclude>
                  <exclude>*/*.xml</exclude>
              </excludes>
              <archive>
                  <manifest>
                      <addClasspath>true</addClasspath>
                      <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                      <classpathPrefix>lib/</classpathPrefix>
                      <!--jar包不包含唯一版本标识-->
                      <useUniqueVersions>false</useUniqueVersions>
                      <!--指定入口类-->
                      <mainClass>com.cwgis.restApp</mainClass>
                  </manifest>
                  <manifestEntries>
                      <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
                      <Class-Path>./resources/</Class-Path>
                  </manifestEntries>
              </archive>
              <outputDirectory>${project.build.directory}</outputDirectory>
          </configuration>
      </plugin>

      <!--拷贝依赖 copy-dependencies-->
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
          <executions>
              <execution>
                  <id>copy-dependencies</id>
                  <phase>package</phase>
                  <goals>
                      <goal>copy-dependencies</goal>
                  </goals>
                  <configuration>
                      <outputDirectory>
                          ${project.build.directory}/lib/
                      </outputDirectory>
                  </configuration>
              </execution>
          </executions>
      </plugin>

      <!--拷贝资源文件 copy-resources-->
      <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <executions>
              <execution>
                  <id>copy-resources</id>
                  <phase>package</phase>
                  <goals>
                      <goal>copy-resources</goal>
                  </goals>
                  <configuration>
                      <resources>
                          <resource>
                              <directory>src/main/resources</directory>
                          </resource>
                      </resources>
                      <outputDirectory>${project.build.directory}/resources</outputDirectory>
                  </configuration>
              </execution>
          </executions>
      </plugin>
  </plugins>
</pluginManagement>
</build>

</project>

@Override
    public  void open()
            throws Exception
    {
        //if (m_Conn!=null && m_Conn.isClosed() == true) {
        //    this.dispose();
        //}
        //Class.forName(this.dbDriver);  //加载驱动程序
        //this.m_Conn = DriverManager.getConnection(this.dbUrl, this.dbUser, this.dbPwd);
        Configuration config = HBaseConfiguration.create();
        //
        String zkAddress = this.dbUrl; //restApp.hbase_url;
        config.set(HConstants.ZOOKEEPER_QUORUM, zkAddress);
        // 设置用户名密码,默认root:root,可根据实际情况调整
        config.set("hbase.client.username", this.dbUser);  //restApp.hbase_user
        config.set("hbase.client.password", this.dbPwd);  //restApp.hbase_password
        if(this.config_ds.dbSchema.equals("aliyun")==true) {
            config.set("hbase.client.connection.impl", AliHBaseUEClusterConnection.class.getName());
        }
        //
        //创建一个连接到hbase对象,根据zookeeper集群参数
        conn_hbase = ConnectionFactory.createConnection(config);
    }
    @Override
    public void dispose()
            throws Exception
    {
        if (this.conn_hbase != null) {
            try {
                this.conn_hbase.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //=============================OK
    @Override
    public List<String> getTableNames() throws Exception
    {
        List<String> t=new ArrayList<>();
        Admin admin = this.conn_hbase.getAdmin();
        HTableDescriptor[] tables=admin.listTables();
        if(tables!=null && tables.length>0)
        {
            String tabName="";
            for(HTableDescriptor t_table :tables)
            {
                tabName=t_table.getTableName().toString();
                //System.out.println("tablename="+tabName);
                t.add(tabName);
            }
        }
        return t;
    }
    //=============================OK
    //规范获取表字段结构信息
    @Override
    public dbtable getDbTableInfo(String tablename) throws Exception
    {
        dbtable tab=new dbtable();
        tab.name=tablename;
        tab.aliasName=tablename;
        Table table=this.conn_hbase.getTable(TableName.valueOf(tablename));
        List<String> listColF=new ArrayList<>();
        HTableDescriptor hTableDescriptor=table.getTableDescriptor();
        for(HColumnDescriptor fdescriptor : hTableDescriptor.getColumnFamilies()){
            listColF.add(fdescriptor.getNameAsString());  //添加列簇
        }
        HashMap<String,List<dbfield>> tableMap=new HashMap<String,List<dbfield>>();
        String colF="";
        String colName="";
        //扫描全表
        Scan scan = new Scan();
        ResultScanner scanResult= table.getScanner(scan);
        for (Result result: scanResult)
        {
            //遍历行的所有列
            List<Cell> listCells = result.listCells();
            //遍历取值
            for (Cell cell : listCells) {
                colF=Bytes.toString(CellUtil.cloneFamily(cell));
                colName=Bytes.toString(CellUtil.cloneQualifier(cell));
                dbfield fd=new dbfield();
                fd.colF=colF;
                fd.name=colName;
                fd.aliasName=colName;
                fd.type=dbfieldtype.string;
                fd.length=65532;
                if(tableMap.containsKey(colF)==false)
                {   //不存在列族
                    List<dbfield> fdList=new ArrayList();
                    fdList.add(fd);
                    tableMap.put(colF,fdList);
                }
                else
                {   //存在列簇
                    List<dbfield> fdList=tableMap.get(colF);
                    boolean isExitFd=false;
                    for(dbfield t_fd : fdList)
                    {
                         if(t_fd.name.equals(colName)==true)
                         {
                             isExitFd=true;
                             break;
                         }
                    }
                    if(isExitFd==false) {
                        fdList.add(fd);
                    }
                    tableMap.put(colF,fdList);
                }
            }
        }
        for(int i=0;i<listColF.size();i++) {
            //System.out.println(listColF.get(i));
            //
            List<dbfield> fdList=tableMap.get(listColF.get(i));
            for(dbfield t_fd : fdList) {
                tab.fields.add(t_fd);
            }
        }
        return tab;
    }
    //=============================
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页