IDEA远程连接HBase及其Java API实战详解
开放端口
安全组没开放端口是原罪!!!导致好多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.gz tar -zxvf hbase-1.3.2-bin.tar.gz #解压 mv hbase-1.3.2-bin /urs/local/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>
配置hbase-env.sh
cd /usr/local/hbase/conf echo $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, 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及其java api实战的文章就介绍到这了,更多相关idea连接hbase内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: Linux 添加中文字体库
下一篇: 选择合适的推荐系统模型