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;
    }
    //=============================
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章