java api远程操作hbase

环境:
   ~~ 1. hbase是运行在服务器的docker上的(为啥不直接部署在服务器上?之前在服务器上部署了hadoop+hbase结果被利用hadoop的漏洞注入了挖矿的病毒…不想重新安装所有服务也没法完全清除病毒,于是就重装系统,愉快的下了一个docker…)
   ~~ 2. 本地(mac)代码连接服务器

主要参考

  1. http://www.voidcn.com/article/p-ddrdpdpc-bqt.html
  2. https://www.cnblogs.com/jiuyang/p/10728246.html

遇到的问题

  1. hbase的版本要和hbase-client版本一致,不一致的话会出现方法的异常。
    我的hbase是1.3.2的,所以hbase-client也要是1.3.2。后面代码里有体现。

  2. 地址映射,要在本地(mac)映射服务器上的主机为myhbase(docker上运行的hbase的主机名,运行容器时的配置,可用docker inspect 容器名查看Hostname)
    mac用户请sudo vi /etc/hosts
    windows用户请搜索如何修改本地hosts映射
    在这里插入图片描述

  3. admin的tableExists方法会导致Connection refused异常,解决方法:造轮子2333
    在这里插入图片描述

  4. 在插入数据的时候不断循环Connecting to myhbase/39.99.XXX.XXX:16020这是因为hbase默认的hbase.regionserver.port端口为106020,只要在容器里面修改一下hbase-site.xml改成实际运行的端口号就行了。

<property>
    <name>hbase.regionserver.port</name>
    <value>localhost:16201</value>
  </property>

下面给出代码


maven配置

<?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>org.example</groupId>
  <artifactId>hbase</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>hbase</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

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

  <build>
    <pluginManagement>
      <plugins>
       
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
package org.example;

import java.io.IOException;

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;

public class Hbase {
    static Configuration conf=null;
    static {
        conf= HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","39.99.156.XXX");
        conf.set("hbase.zookeeper.property.clientPort","2181");
    }
    public static void show()throws  IOException {
        Connection connection=ConnectionFactory.createConnection(conf);
        Admin admin=connection.getAdmin();
        TableName[] tableNames = admin.listTableNames();
        for (TableName ts : tableNames) {
            System.out.println(ts.toString());
        }
    }
    public static boolean tableExists(String tableName) throws IOException {
        Connection connection=ConnectionFactory.createConnection(conf);
        Admin admin=connection.getAdmin();
        TableName[] tableNames = admin.listTableNames();
        for(TableName ts : tableNames) {
            if(ts.toString().equals(tableName)) {
                return true;
            }
        }
        return false;
    }
    public  static  void delectTable(String tableName) throws IOException {
        Connection connection=ConnectionFactory.createConnection(conf);
        Admin admin=connection.getAdmin();
        if(!tableExists(tableName)) {
            System.out.println("not exist!");
            return;
        }
        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));
        System.out.println("delected "+ tableName);
    }
    public static void createTable(String tableName,String... families) throws Exception{
        HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
        try{
            Connection connection=ConnectionFactory.createConnection(conf);
            Admin admin=connection.getAdmin();
            for(String family:families){
                tableDescriptor.addFamily(new HColumnDescriptor(family));
            }
            admin.createTable(tableDescriptor);   
            System.out.println("Create table Success!!!Table Name:["+tableName+"]");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        Hbase.createTable("tt", "name", "class");
        Hbase.show();
        Hbase.delectTable("tt");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39921637/article/details/105824920