IDEA远程连接HBase及其JavaAPI实战详解
开放端⼝
安全组没开放端⼝是原罪导致好多BUG费时费⼒。Hbase悄悄咪咪的⽤了好多端⼝,⽐如被我抓到的42239,直接搜索报错药不对症。
Hbase安装
直接下载或者下载到本地再上传都⾏,看你哪个快。
wget /dist/hbase/1.3.2/hbase-1.3.
tar -zxvf hbase-1.3. #解压
mv hbase-1.3.2-bin /urs/local/hbase
配置l
cd /usr/local/hbase/conf
l
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name&dir</name>
<value>/hbase-data</value>
</property>
<property>
<name&keeper.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
公⽹ip master
⽬录结构:
创建⼀个maven项⽬并在l添加依赖:
<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时查看。
log4j.properties配置
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
defutures=DEBUG
<?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&dir</name>
<value>/hbase-data</value>
</property>
<property>
<name&keeper.quorum</name>
<value>master:2181</value>
</property>
</configuration>
HBaseCRUD.java
package ex3;
import org.f.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(){
ate();
configuration.set("keeper.quorum","121.36.240.205"); // 换成你⾃⼰的IP configuration.set("keeper.property.clientPort","2181");
try{
ateConnection(configuration);
Admin();
}catch (IOException e){
e.printStackTrace();
}
}
/**
* 关闭连接
*/
public static void close(){
try{
if(admin!=null)
admin.close();
}catch (IOException e){
idea debuge.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);
}
}
}
/**
* 添加数据
* @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 = Table(TableName.valueOf(tablename));//获取表名
Put put = new Bytes());
put.Bytes(),Bytes(),Bytes());
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 = Table(TableName.valueOf(tablename));
Get get = new Bytes());
get.Bytes(),Bytes());
Result result = (get);
String val = new Bytes(),col==null?Bytes()));
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.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.close();
System.out.println("记得⼀键三连~");
}
}
点击运⾏后需要点耐⼼等待,灰⾊INFO、DEBUG信息都可不⽤管,报错的话会是红⾊字体(评论区交流),有tomcat内味了。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论