开放端口
安全组没开放端口是原罪!!!导致好多bug费时费力。hbase悄悄咪咪的用了好多端口,比如被我抓到的42239,直接搜索报错药不对症。
hbase安装
下载压缩包
可以去官网下载http://hbase.apache.org/downloads.html
也可以去镜像下载历史版本http://archive.apache.org/dist/hbase/
以1.3.2版本为例:
直接下载或者下载到本地再上传都行,看你哪个快。
1 2 3 |
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 |
配置hbase-site.xml
1 2 |
cd /usr/local/hbase/conf vi hbase-site.xml |
1 2 3 4 5 6 7 8 9 10 11 12 |
<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
1 2 3 4 |
cd /usr/local/hbase/conf echo $java_home #若没有安装jdk可百度(偷懒) vi hbase-env.sh #添加要一致 export java_home=/usr/local/java |
运行测试
1 2 |
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添加依赖:
1 2 3 4 5 6 7 8 9 10 |
<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配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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测试数据.codefutures=debug |
hbase-site.xml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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内味了。
原创不易,请勿转载 ( 本不富裕的访问量雪上加霜 )
博主首页:https://wzlodq.blog.csdn.net/
到此这篇关于idea远程连接hbase及其java api实战的文章就介绍到这了,更多相关idea连接hbase内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/qq_45034708/article/details/115678645
查看更多关于IDEA远程连接HBase及其Java API实战详解的详细内容...