首页 课程 师资 教程 报名

Java API连接hbase示例

  • 2022-09-30 09:55:50
  • 1286次 动力节点

首先pom.xml里添加hbase-client依赖:

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.4.13</version>
</dependency>

然后将hbase-site.xml,core-site.xml复制到本地(如果实在本地运行的话)

package com.dkl.blog.hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
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.apache.hadoop.hbase.io.compress.Compression.Algorithm;
/**
 *Java API 连接 HBASE
 */
public class JavaHbaseExample {
    private static final String TABLE_NAME = "MY_TABLE_NAME_TOO";
    private static final String CF_DEFAULT = "DEFAULT_COLUMN_FAMILY";
    public static void createOrOverwrite(Admin admin, HTableDescriptor table) throws IOException {
        if (admin.tableExists(table.getTableName())) {//如果表已存在
            if (admin.isTableEnabled(table.getTableName())) {//如果表状态为Enabled
                admin.disableTable(table.getTableName());
            }
            admin.deleteTable(table.getTableName());
        }
        admin.createTable(table);
    }
    public static void createSchemaTables(Configuration config) throws IOException {
        try (Connection connection = ConnectionFactory.createConnection(config);
             Admin admin = connection.getAdmin()) {
            HTableDescriptor table = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
            table.addFamily(new HColumnDescriptor(CF_DEFAULT).setCompressionType(Algorithm.NONE));
            System.out.print("Creating table. ");
            createOrOverwrite(admin, table);
            System.out.println(" Done.");
        }
    }
    public static void modifySchema(Configuration config) throws IOException {
        try (Connection connection = ConnectionFactory.createConnection(config);
             Admin admin = connection.getAdmin()) {
            TableName tableName = TableName.valueOf(TABLE_NAME);
            if (!admin.tableExists(tableName)) {
                System.out.println("Table does not exist.");
                System.exit(-1);
            }
            HTableDescriptor table = admin.getTableDescriptor(tableName);
            // Update existing table
            HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF");
            newColumn.setCompactionCompressionType(Algorithm.GZ);
            newColumn.setMaxVersions(HConstants.ALL_VERSIONS);
            //admin.addColumn(tableName, newColumn); //官方文档代码,这里我理解的是应该要添加列簇,但是该方法不生效,
            // 导致抛出异常org.apache.hadoop.hbase.InvalidFamilyOperationException:
            // Family 'DEFAULT_COLUMN_FAMILY' is the only column family in the table, so it cannot be deleted,
            // 用我下面这行代码
            table.addFamily(newColumn);
            // Update existing column family
            HColumnDescriptor existingColumn = new HColumnDescriptor(CF_DEFAULT);
            existingColumn.setCompactionCompressionType(Algorithm.GZ);
            existingColumn.setMaxVersions(HConstants.ALL_VERSIONS);
            table.modifyFamily(existingColumn);
            admin.modifyTable(tableName, table);
            // Disable an existing table
            admin.disableTable(tableName);
            // Delete an existing column family
            admin.deleteColumn(tableName, CF_DEFAULT.getBytes("UTF-8"));
            // Delete a table (Need to be disabled first)
            admin.deleteTable(tableName);
        }
    }
    public static void main(String... args) throws IOException {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", "192.168.44.128");  //hbase 服务地址,如果在hbase-site.xml里有配置,可以不用这行代码
//        config.set("hbase.zookeeper.property.clientPort","2181"); //默认2181端口
//        System.out.println(System.getenv("HBASE_CONF_DIR"));
        //Add any necessary configuration files (hbase-site.xml, core-site.xml)
//        config.addResource(new Path(System.getenv("HBASE_CONF_DIR"), "hbase-site.xml")); //官方文档代码,需要配置环境变量
        config.addResource(new Path("D:\data\conf\hbase", "hbase-site.xml"));
//        config.addResource(new Path(System.getenv("HADOOP_CONF_DIR"), "core-site.xml"));//官方文档代码,需要配置环境变量
        config.addResource(new Path("D:\data\conf\hadoop", "core-site.xml"));
        createSchemaTables(config);
        modifySchema(config);
    }
}

异常及解决

java.net.ConnectException: Connection refused: no further information

java.net.ConnectException: Connection refused: no further information

解决方法:

1.添加里添加

config.set("hbase.zookeeper.quorum", "192.168.44.128");    //官网代码里没有这行

2.程序里用到的hbase-site.xml里添加 (如果hbase-site.xml没有下面的配置,单机配置时没有添加)

<property>
    <name>hbase.zookeeper.quorum</name>
    <value>192.168.44.128</value>
</property>

Call to localhost/127.0.0.1:16201 failed on connection exception

java.net.SocketTimeoutException: callTimeout=60000, callDuration=62839: Call to localhost/127.0.0.1:16201  failed on connection exception

没有设置host 和 hostname 验证:

netstat -nautlp|grep 16201
tcp6       0      0 127.0.0.1:16201         :::*                    LISTEN      69041/java          
tcp6       0      0 127.0.0.1:34889         127.0.0.1:16201         ESTABLISHED 68947/java          
tcp6       0      0 127.0.0.1:16201         127.0.0.1:34889         ESTABLISHED 69041/java          
tcp6       0      0 127.0.0.1:34885         127.0.0.1:16201         ESTABLISHED 68947/java          
tcp6       0      0 127.0.0.1:16201         127.0.0.1:34885         ESTABLISHED 69041/java

修改:

vim /etc/hosts
192.168.44.128 master
vim /etc/hostname
master

然后重启hbase和程序,验证

netstat -nautlp|grep 16201
tcp6       0      0 192.168.44.128:16201    :::*                    LISTEN      66641/java          
tcp6       0      0 192.168.44.128:16201    192.168.44.128:40589    ESTABLISHED 66641/java          
tcp6       0      0 192.168.44.128:40589    192.168.44.128:16201    ESTABLISHED 66546/java

java.net.SocketTimeoutException: callTimeout=60000, callDuration=79924: can not resolve master,16201,1597395570081

这个是本地运行程序且没有配置本地host时产生的,解决方法为编辑C:WindowsSystem32driversetchosts,添加

192.168.44.128 master

 

选你想看

你适合学Java吗?4大专业测评方法

代码逻辑 吸收能力 技术学习能力 综合素质

先测评确定适合在学习

在线申请免费测试名额
价值1998元实验班免费学
姓名
手机
提交