【HBase】通過Java代碼實現HBase數據庫中數據的增刪改查



創建maven工程,導入jar包
	<repositories>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
        </repository>
    </repositories>

    <dependencies>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0-mr1-cdh5.14.0</version>
        </dependency>


        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.0-cdh5.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.2.0-cdh5.14.0</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <!-- <verbal>true</verbal>-->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*/RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

java代碼實現創建hbase表
package cn.itcast.HBaseStudy;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class HBaseFirst {
    /** * 需求1:創建myuser表,包含f1和f2兩個列族 */
    @Test
    public void createTable() throws IOException {
        //連接HBase服務端
        Configuration configuration = HBaseConfiguration.create();
        /* 通信三要素:IP地址、端口號、傳輸協議 設置HBase連接ZK的地址 */
        configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");

        //獲取HBase數據庫連接對象
        Connection connection = ConnectionFactory.createConnection(configuration);
        //獲取管理員對象,用於創建表、刪除表等
        Admin admin = connection.getAdmin();
        //獲取HTableDescriptor對象
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("myuser"));
        //給表添加列族名需要創建HColumnDescriptor對象
        HColumnDescriptor f1 = new HColumnDescriptor("f1");
        HColumnDescriptor f2 = new HColumnDescriptor("f2");
        //先給表添加列族名
        hTableDescriptor.addFamily(f1);
        hTableDescriptor.addFamily(f2);
        //用管理員創建表
        admin.createTable(hTableDescriptor);

        //關閉admin
        admin.close();
        //關閉connection
        connection.close();

    }
}

java代碼實現向hbase表中插入數據
/** * 創建連接HBase服務器的方法 */
    private Connection connection;
    private Table table;

    @BeforeTest
    public void init() throws IOException {
        //獲取連接
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
        connection = ConnectionFactory.createConnection(configuration);
        //獲取表
        table = connection.getTable(TableName.valueOf("myuser"));
    }

    /** * 需求2:向表中添加數據 */
    @Test
    public void addData() throws IOException {

        //創建put對象,並指定rowkey
        Put put = new Put("0001".getBytes());
        put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes(1));
        put.addColumn("f1".getBytes(),"name".getBytes(), Bytes.toBytes("張三"));
        put.addColumn("f1".getBytes(),"age".getBytes(), Bytes.toBytes(18));

        put.addColumn("f2".getBytes(),"address".getBytes(), Bytes.toBytes("地球人"));
        put.addColumn("f2".getBytes(),"phone".getBytes(), Bytes.toBytes("15874102589"));
        //插入數據
        table.put(put);
    }

    /** * 關閉系統連接和表連接 */
    @AfterTest
    public void close() throws IOException {
        //關閉表
        table.close();
        connection.close();
    }

java代碼查詢hbase數據
/** * 查詢rowKey為0003的數據 */
@Test
public void searchData() throws IOException {
    //獲取get對象
    Get get = new Get(Bytes.toBytes("0003"));
    //通過get獲取數據 result封裝了所有結果數據
    Result result = table.get(get);

    //打印結果數據,或者這條數據所有的cell
    List<Cell> cells = result.listCells();

    for (Cell cell : cells) {
        //獲取所在列族
        byte[] family = cell.getFamily();
        //獲取所在列名
        byte[] qualifier = cell.getQualifier();
        //獲取值
        byte[] value = cell.getValue();

        if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(qualifier))) {
            System.out.println("列族名稱為" + Bytes.toString(family) + "列名稱為" + Bytes.toString(qualifier) + "列值為" + Bytes.toInt(value));
        } else {
            System.out.println("列族名稱為" + Bytes.toString(family) + "列名稱為" + Bytes.toString(qualifier) + "列值為" + Bytes.toString(value));
        }
    }
}

使用rowKey查詢指定列族指定列的值
/** * 使用rowKey查詢0003,f1列族,name列的值 **/

@Test
public void getColumn() throws IOException {
    //獲取get對象
    Get get = new Get("0003".getBytes());
    //拿到f1列族和name列
    get.addColumn("f1".getBytes(),"name".getBytes());

    //拿到以上要求的所有數據
    Result result = table.get(get);
    //將數據值放到一個數組中
    List<Cell> cells = result.listCells();
    for (Cell cell : cells) {
        byte[] family = cell.getFamily();
        byte[] qualifier = cell.getQualifier();
        byte[] value = cell.getValue();

        System.out.println("rk為0003,在f1列族name列下的數據值為"+Bytes.toString(value));
    }

}

通過startRowKey和endRowKey進行掃描
/** * 掃描rowKey為0004-0006的范圍值 */
  @Test
  public void rangeRowKey() throws IOException {
      //創建Scan對象,獲取到rk的范圍,如果不指定范圍就是全表掃描
      Scan scan = new Scan("0004".getBytes(),"0006".getBytes());

      //拿到了多條數據的結果
      ResultScanner scanner = table.getScanner(scan);
      //循環遍歷ResultScanner,將多條數據分成一條條數據
      for (Result result : scanner) {
          byte[] row = result.getRow();
          List<Cell> cells = result.listCells();
          for (Cell cell : cells) {
              byte[] family = cell.getFamily();
              byte[] qualifier = cell.getQualifier();
              byte[] value = cell.getValue();

              /** * 掃描rowKey為0004-0006的范圍值 */
    @Test
    public void rangeRowKey() throws IOException {
        //創建Scan對象,獲取到rk的范圍,如果不指定范圍就是全表掃描
        Scan scan = new Scan("0004".getBytes(),"0006".getBytes());

        //拿到了多條數據的結果
        ResultScanner scanner = table.getScanner(scan);
        //循環遍歷ResultScanner,將多條數據分成一條條數據
        for (Result result : scanner) {
            byte[] row = result.getRow();
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                byte[] family = cell.getFamily();
                byte[] qualifier = cell.getQualifier();
                byte[] value = cell.getValue();

                if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(qualifier))) {
                    System.out.println("列族名稱為" + Bytes.toString(family) + "列名稱為" + Bytes.toString(qualifier) + "列值為" + Bytes.toInt(value));
                } else {
                    System.out.println("列族名稱為" + Bytes.toString(family) + "列名稱為" + Bytes.toString(qualifier) + "列值為" + Bytes.toString(value));
                }
            }

        }
    }
          }

      }
  }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM