温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在IDEA中远程连接HBase

发布时间:2021-04-17 15:42:58 来源:亿速云 阅读:1484 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关怎么在IDEA中远程连接HBase,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

开放端口


安全组没开放端口是原罪!!!导致好多BUG费时费力。Hbase悄悄咪咪的用了好多端口,比如被我抓到的42239,直接搜索报错药不对症。

怎么在IDEA中远程连接HBase
怎么在IDEA中远程连接HBase

Hbase安装


下载压缩包
可以去官网下载http://hbase.apache.org/downloads.html
也可以去镜像下载历史版本http://archive.apache.org/dist/hbase/
以1.3.2版本为例:

怎么在IDEA中远程连接HBase

直接下载或者下载到本地再上传都行,看你哪个快。

wget http://archive.apache.org/dist/hbase/1.3.2/hbase-1.3.2-bin.tar.gz tar -zxvf hbase-1.3.2-bin.tar.gz #解压 mv hbase-1.3.2-bin /urs/local/hbase

怎么在IDEA中远程连接HBase

配置hbase-site.xml

cd /usr/local/hbase/conf vi hbase-site.xml
<property>         <name>hbase.cluster.distributed</name>         <value>true</value>     </property>     <property>         <name>hbase.rootdir</name>         <value>/hbase-data</value>     </property>     <property>         <name>hbase.zookeeper.quorum</name>         <value>master:2181</value>     </property>

怎么在IDEA中远程连接HBase

配置hbase-env.sh

cd /usr/local/hbase/conf echo $JAVA_HOME #若没有安装jdk可百度(偷懒) vi hbase-env.sh #添加要一致 export JAVA_HOME=/usr/local/java

怎么在IDEA中远程连接HBase
怎么在IDEA中远程连接HBase

运行测试

cd /usr/local/hbase/bin ./start-hbase.sh

怎么在IDEA中远程连接HBase
怎么在IDEA中远程连接HBase

ip:16010访问

怎么在IDEA中远程连接HBase

域名配置


服务器 vi /etc/hosts
私网ip master

怎么在IDEA中远程连接HBase

本地 C:\Windows\System32\drivers\etc\hosts
公网ip master

怎么在IDEA中远程连接HBase

怎么在IDEA中远程连接HBase

IDEA源码


目录结构:

怎么在IDEA中远程连接HBase

创建一个maven项目并在pom.xml添加依赖:

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

版本是1.3.2,注意和你自己的一致,可以登录hbase shell时查看。

怎么在IDEA中远程连接HBase

插播反爬信息 )博主CSDN地址:https://wzlodq.blog.csdn.net/

log4j.properties配置

log4j.rootLogger=debug, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=firestorm.log log4j.appender.R.MaxFileSize=100KB log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n log4j.logger.com.codefutures=DEBUG

hbase-site.xml配置

<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl" rel="external nofollow" ?> <configuration>     <property>         <name>hbase.cluster.distributed</name>         <value>true</value>     </property>     <property>         <name>hbase.rootdir</name>         <value>/hbase-data</value>     </property>     <property>         <name>hbase.zookeeper.quorum</name>         <value>master:2181</value>     </property> </configuration>

HBaseCRUD.java

package ex3; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import java.io.IOException; public class HbaseCRUD {     private static Configuration configuration;     private static Connection connection;     private static Admin admin;     /**      * 建立连接      */     public static void init(){         configuration=HBaseConfiguration.create();         configuration.set("hbase.zookeeper.quorum","121.36.240.205"); // 换成你自己的IP         configuration.set("hbase.zookeeper.property.clientPort","2181");         try{             connection=ConnectionFactory.createConnection(configuration);             admin=connection.getAdmin();         }catch (IOException e){             e.printStackTrace();         }     }     /**      * 关闭连接      */     public static void close(){         try{             if(admin!=null)                 admin.close();         }catch (IOException e){             e.printStackTrace();         }     }     /**      * 创建表      * @param myTableName 表名      * @param colFamily 列族数组      * @throws IOException      */     public static void createTable(String myTableName,String[]colFamily)throws IOException{         TableName tablename = TableName.valueOf(myTableName);         if(admin.tableExists(tablename)){             System.out.println("表名已存在!");         }         else{             HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);             for(String str:colFamily){  //增加一列                 HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);                 hTableDescriptor.addFamily(hColumnDescriptor);             }             admin.createTable(hTableDescriptor); //建表         }     }     /**      * 添加数据      * @param tablename 表名      * @param rowKey 行键      * @param colFamily 列族      * @param col 列限定符      * @param val 数据      * @throws IOException      */     public static void insertData(String tablename,String rowKey,String colFamily,String col,String val)throws IOException{         Table table = connection.getTable(TableName.valueOf(tablename));//获取表名         Put put = new Put(rowKey.getBytes());         put.addColumn(colFamily.getBytes(),col.getBytes(),val.getBytes());         table.put(put);         table.close();     }     /**      * 获取数据      * @param tablename 表名      * @param rowKey 行键      * @param colFamily 列族      * @param col 列限定符      * @throws IOException      */     public static void getData(String tablename,String rowKey,String colFamily,String col)throws IOException{         Table table = connection.getTable(TableName.valueOf(tablename));         Get get = new Get(rowKey.getBytes());         get.addColumn(colFamily.getBytes(),col.getBytes());         Result result = table.get(get);         String val = new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes()));         System.out.println("值为:"+val);         table.close();     } }

Client.java

package ex3; import org.apache.log4j.BasicConfigurator; import java.io.IOException; public class Client {     public static void main(String[] args) throws IOException {         HbaseCRUD.init();         //建表         HbaseCRUD.createTable("student",new String[]{"score"});         //插入数据         HbaseCRUD.insertData("student","lisa","score","Java","85");         HbaseCRUD.insertData("student","lisa","score","C/C++","80");         HbaseCRUD.insertData("student","lisa","score","MySQL","82");         HbaseCRUD.insertData("student","jerry","score","Java","65");         //查询数据         HbaseCRUD.getData("student","lisa","score","Java");         HbaseCRUD.getData("student","lisa","score","MySQL");         HbaseCRUD.getData("student","jerry","score","Java");         HbaseCRUD.close();         System.out.println("记得一键三连~");     } }

点击运行后需要点耐心等待,灰色INFO、DEBUG信息都可不用管,报错的话会是红色字体(评论区交流),有tomcat内味了。

怎么在IDEA中远程连接HBase


关于怎么在IDEA中远程连接HBase就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI