这篇文章将为大家详细讲解有关怎么在IDEA中远程连接HBase,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
开放端口
安全组没开放端口是原罪!!!导致好多BUG费时费力。Hbase悄悄咪咪的用了好多端口,比如被我抓到的42239,直接搜索报错药不对症。
Hbase安装
下载压缩包
可以去官网下载http://hbase.apache.org/downloads.html
也可以去镜像下载历史版本http://archive.apache.org/dist/hbase/
以1.3.2版本为例:
直接下载或者下载到本地再上传都行,看你哪个快。
wget http://archive.apache.org/dist/hbase/1.3.2/hbase-1.3.2-bin.tar.gztar -zxvf hbase-1.3.2-bin.tar.gz #解压mv hbase-1.3.2-bin /urs/local/hbase
配置hbase-site.xml
cd /usr/local/hbase/confvi 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>
配置hbase-env.sh
cd /usr/local/hbase/confecho $JAVA_HOME #若没有安装jdk可百度(偷懒)vi hbase-env.sh #添加要一致export JAVA_HOME=/usr/local/java
运行测试
cd /usr/local/hbase/bin./start-hbase.sh
ip:16010访问
域名配置
服务器 vi /etc/hosts
私网ip master
本地 C:\Windows\System32\drivers\etc\hosts
公网ip master
IDEA源码
目录结构:
创建一个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时查看。
(插播反爬信息 )博主CSDN地址:https://wzlodq.blog.csdn.net/
log4j.properties配置
log4j.rootLogger=debug, stdout, Rlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p - %m%nlog4j.appender.R=org.apache.log4j.RollingFileAppenderlog4j.appender.R.File=firestorm.loglog4j.appender.R.MaxFileSize=100KBlog4j.appender.R.MaxBackupIndex=1log4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=%p %t %c - %m%nlog4j.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(); } } 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); //建表 } } 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(); } 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就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。