win10 idea远程连接hbase
关闭hadoop和hbase
如果已经关闭不需要走这一步
cd /usr/local/hbase bin/stop-hbase.sh cd /usr/local/hadoop ./sbin/stop-dfs.sh
获取虚拟机的ip
虚拟机终端输入
ip a
关闭虚拟机防火墙
sudo ufw disable
修改hadoop的core-site.xml文件
将ip修改成自己的ip
# 位置可能不一样,和hadoop安装位置有关 cd /usr/local/hadoop/etc/hadoop vim core-site.xml
<configuration>
<property>
<name>hadoop.tmp.dir</name>
<value>file:/usr/local/hadoop/tmp</value>
<description>abase for other temporary directories.</description>
</property>
<property>
<name>fs.defaultfs</name>
<value>hdfs://192.168.111.135:9000</value>
</property>
<property>
<name>hadoop.proxyuser.hadoop.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.hadoop.groups</name>
<value>*</value>
</property>
</configuration>
修改hbase的hbase-site.xml
将ip修改成自己的ip
# 位置可能不一样,和hbase安装位置有关 cd /usr/local/hbase vim /usr/local/hbase/conf/hbase-site.xml
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://192.168.111.135:9000/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.unsafe.stream.capability.enforce</name>
<value>false</value>
</property>
<property>
<name>hbase.wal.provider</name>
<value>filesystem</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>192.168.111.135:2181</value>
</property>
<property>
<name>hbase.master.ipc.address</name>
<value>0.0.0.0</value>
</property>
<property>
<name>hbase.regionserver.ipc.address</name>
<value>0.0.0.0</value>
</property>
</configuration>
打开hadoop和hbase
cd /usr/local/hadoop ./sbin/start-dfs.sh cd /usr/local/hbase bin/start-hbase.sh jps

idea 连接
创建maven项目
idea自带maven,如果需要自己安装maven可以参考安装maven
创建项目,选择maven,模板选择第一个maven-archetype-archetype
添加依赖(pom.xml)
记得修改自己hbase的版本,我的是2.5.4
设置好后reload一下
<dependencies>
<dependency>
<groupid>org.apache.hbase</groupid>
<artifactid>hbase-client</artifactid>
<version>2.5.4</version>
</dependency>
</dependencies>创建java文件并运行
import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.ioexception;
public class test01 {
public static configuration configuration;
public static connection connection;
public static admin admin;
public static void init(){
system.setproperty("hadoop_user_name","hadoop");
configuration = hbaseconfiguration.create();
// ip 需要修改
configuration.set("hbase.zookeeper.quorum", "192.168.111.135");
configuration.set("hbase.zookeeper.property.clientport", "2181");
// ip 需要修改
configuration.set("hbase.rootdir","hdfs://192.168.111.135:9000/hbase");
try{
connection = connectionfactory.createconnection(configuration);
admin = connection.getadmin();
}catch (ioexception e){
e.printstacktrace();
}
}
/*
* 打印所有表名称
* */
public static void tablelistprint() throws ioexception {
tablename[] tablenames = admin.listtablenames();
for(tablename tablename : tablenames){
system.out.println(tablename.getnameasstring());
}
}
public static void close(){
try{
if(admin != null){admin.close();}
if(null != connection){connection.close();}
}catch (ioexception e){
e.printstacktrace();
}
}
public static void main(string[] args) throws ioexception {
init();
tablelistprint();
close();
}
}

其他
hbase shell命令
# 进入shell bin/hbase shell # 列出hbase中所有的表 list # 创建一个新表,表名为studentinfo,包含两个列族personal和grades。 create 'studentinfo', 'personal', 'grades' # 向studentinfo表中插入一条记录,rowkey为2023001,personal:name列的值为张三,grades:math列的值为90。 put 'studentinfo','2023001', 'personal:name','张三' put 'studentinfo','2023001', 'grades:math', 90 # 查询rowkey为2023001的所有信息。 get 'studentinfo','2023001' # 修改2023001的grades:math列的值为95。 put 'studentinfo', '2023001', 'grades:math', '95' # 删除2023001的personal:name列。 delete 'studentinfo', '2023001', 'personal:name' # 扫描studentinfo表,查看所有记录。 scan 'studentinfo'
java api
import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.bytes;
import java.io.ioexception;
public class work01 {
public static configuration configuration;
public static connection connection;
public static admin admin;
public static void main(string[] args) throws ioexception {
init();
// 删除表 第一次运行请注释
// deletetable("employeerecords");
tablelistprint();
createtable("employeerecords",new string[]{"info","salary"});
tablelistprint();
insertdata("employeerecords","606","info","name","cy");
insertdata("employeerecords","606","info","department","现代信息产业学院");
insertdata("employeerecords","606","info","monthly","50000");
getdata("employeerecords","606");
updatedata("employeerecords","606","60000");
getdata("employeerecords","606");
deletedata("employeerecords","606");
close();
}
public static void init(){
system.setproperty("hadoop_user_name","hadoop");
configuration = hbaseconfiguration.create();
configuration.set("hbase.zookeeper.quorum", "192.168.111.135");
configuration.set("hbase.zookeeper.property.clientport", "2181");
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
// 避免乱码问题
configuration.set("hbase.client.encoding.fallback", "utf-8");
try{
connection = connectionfactory.createconnection(configuration);
admin = connection.getadmin();
}catch (ioexception e){
e.printstacktrace();
}
}
/*
* 打印所有表名称
* */
public static void tablelistprint() throws ioexception {
tablename[] tablenames = admin.listtablenames();
system.out.print("所有表:");
for(tablename tablename : tablenames){
system.out.print(tablename.getnameasstring() + "\t");
}
system.out.println();
}
public static void createtable(string mytablename,string[] colfamily) throws ioexception {
tablename tablename = tablename.valueof(mytablename);
if(admin.tableexists(tablename)){
system.out.println("talbe is exists!");
}else {
tabledescriptorbuilder tabledescriptor = tabledescriptorbuilder.newbuilder(tablename);
for(string str:colfamily){
columnfamilydescriptor family =
columnfamilydescriptorbuilder.newbuilder(bytes.tobytes(str)).build();
tabledescriptor.setcolumnfamily(family);
}
admin.createtable(tabledescriptor.build());
}
}
public static void deletetable(string mytablename) throws ioexception {
tablename tablename = tablename.valueof(mytablename);
admin.disabletable(tablename);
admin.deletetable(tablename);
}
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 updatedata(string tablename,string rowkey,string val) throws ioexception {
table table = connection.gettable(tablename.valueof(tablename));
put put = new put(rowkey.getbytes());
put.addcolumn("info".getbytes(),"monthly".getbytes(), val.getbytes());
table.put(put);
table.close();
}
public static void getdata(string tablename,string rowkey)throws ioexception{
table table = connection.gettable(tablename.valueof(tablename));
get get = new get(rowkey.getbytes());
result result = table.get(get);
cell[] cells = result.rawcells();
system.out.print("行键:"+rowkey);
for (cell cell : cells) {
//获取列名
string colname = bytes.tostring(cell.getqualifierarray(),cell.getqualifieroffset(),cell.getqualifierlength());
string value = bytes.tostring(cell.getvaluearray(), cell.getvalueoffset(), cell.getvaluelength());
system.out.print("\t"+colname+":"+value);
}
system.out.println();
table.close();
}
public static void deletedata(string tablename,string rowkey)throws ioexception{
table table = connection.gettable(tablename.valueof(tablename));
delete delete = new delete(rowkey.getbytes());
table.delete(delete);
system.out.println("删除成功");
}
public static void close(){
try{
if(admin != null){admin.close();}
if(null != connection){connection.close();}
}catch (ioexception e){
e.printstacktrace();
}
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论