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映射
    java api远程操作hbase_第1张图片

  3. admin的tableExists方法会导致Connection refused异常,解决方法:造轮子2333
    java api远程操作hbase_第2张图片

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

<property>
    <name>hbase.regionserver.portname>
    <value>localhost:16201value>
  property>

下面给出代码


maven配置



<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.0modelVersion>

  <groupId>org.examplegroupId>
  <artifactId>hbaseartifactId>
  <version>1.0-SNAPSHOTversion>

  <name>hbasename>
  
  <url>http://www.example.comurl>

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

  <dependencies>
    <dependency>
      <groupId>org.apache.hbasegroupId>
      <artifactId>hbase-clientartifactId>
      <version>1.3.2version>
    dependency>
  dependencies>

  <build>
    <pluginManagement>
      <plugins>
       
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-jar-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
        <plugin>
          <artifactId>maven-site-pluginartifactId>
          <version>3.7.1version>
        plugin>
        <plugin>
          <artifactId>maven-project-info-reports-pluginartifactId>
          <version>3.0.0version>
        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");
    }
}

你可能感兴趣的:(java api远程操作hbase)