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();
}
}
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内味了。
原创不易,请勿转载(
本不富裕的访问量雪上加霜)
博主首页:https://wzlodq.blog.csdn.net/
到此这篇关于IDEA远程连接HBase及其Java API实战的文章就介绍到这了,更多相关idea连接HBase内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341