HBase 通过 Maven 用 Java API 进行增删查put/get/delete

本文介绍了如何通过Java API结合Maven配置,实现对HBase数据库的增删查操作。包括读写源码的详细步骤和pom.xml文件的配置说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 读写源码

注意加注释的地方!!!
/**
 * App.java
 */
package jiecxy;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;

public class App
{
    public static void main( String[] args )
    {
        // 建立连接
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");

        Connection conn = null;
        Admin admin = null;
        try {
            conn = ConnectionFactory.createConnection(conf);
            admin = conn.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String TABLE_NAME = "test";
        String COLUMN_FAMILY_NAME = "columnfamily";
        String COLUMN_FAMILY_NAME2 = "columnfamily1";
        String[] COLUMNS = {"column0", "column1", "column2"};
        // Some data
        String ROW_KEYS = "row0";
        String[] VALUES = {"value0", "value1", "value2"};

        System.out.println("CREATE TABLE ==========================");
        // 创建表
        TableName tableName = TableName.valueOf(TABLE_NAME);
        try {
            // 存在则删除
            if (admin.tableExists(tableName)) {
                admin.disableTable(tableName);
                admin.deleteTable(tableName);
            }
            TableDescriptor descriptor = TableDescriptorBuilder.newBuilder(tableName)
                    .addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(COLUMN_FAMILY_NAME.getBytes()).build())
                    .addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(COLUMN_FAMILY_NAME2.getBytes()).build())
                    .build();
            admin.createTable(descriptor);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("PUT VALUE ==========================");
        // 添加数据
        try {
            Table table = conn.getTable(tableName);
            Put put = new Put(ROW_KEYS.getBytes());
            put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[0].getBytes(), VALUES[0].getBytes());
            put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[1].getBytes(), VALUES[1].getBytes());
            put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[2].getBytes(), VALUES[2].getBytes());
            table.put(put);
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("DELETE VALUE ==========================");
        // 删除数据: 删除row0的column2列
        try {
            Table table = conn.getTable(tableName);
            Delete delete = new Delete(ROW_KEYS.getBytes());
            delete.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[2].getBytes());
            table.delete(delete);
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("GET VALUE ==========================");
        // 查询数据
        try {
            Table table = conn.getTable(tableName);
            Get get = new Get(ROW_KEYS.getBytes());
            //get.addFamily(COLUMN_FAMILY_NAME.getBytes());                         // 获取特定列族的数据
            //get.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[1].getBytes());  // 获取特定列的数据
            Result result = table.get(get);
            // get column family
            result.getFamilyMap(COLUMN_FAMILY_NAME.getBytes()).forEach((k,v) -> System.out.println(new String(k) + ":" + new String(v)));
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭连接
        try {
            if (admin != null) {
                admin.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

运行结果:
这里写图片描述

2. pom.xml文件配置

注意:
    1. 使用shade方式防止打包失败
    2. 这里的dependency只需引用 `hbase-client`
<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>HBaseTest</groupId>
  <artifactId>HBaseTest</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

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

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

  <dependencies>
      <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>2.0.0-alpha3</version>
      </dependency>
  </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <!-- Do not copy the signatures in the META-INF folder.
                                    Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>

                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>jiecxy.App</mainClass>
                                </transformer>
                            </transformers>

                            <createDependencyReducedPom>false</createDependencyReducedPom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值