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

2019-03-16 13:42 

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiecxy/article/details/78116500

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>

发表评论

您必须 登录 才能发表留言!