Win10 IDEA遠程連接HBase教程
Win10 IDEA遠程連接HBase
關(guān)閉Hadoop和Hbase
如果已經(jīng)關(guān)閉不需要走這一步
cd /usr/local/hbase bin/stop-hbase.sh cd /usr/local/hadoop ./sbin/stop-dfs.sh
獲取虛擬機的ip
虛擬機終端輸入
ip a
關(guān)閉虛擬機防火墻
sudo ufw disable
修改Hadoop的core-site.xml文件
將IP修改成自己的IP
# 位置可能不一樣,和Hadoop安裝位置有關(guān) 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安裝位置有關(guān) 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 連接
創(chuàng)建Maven項目
IDEA自帶Maven,如果需要自己安裝Maven可以參考安裝Maven
創(chuàng)建項目,選擇Maven,模板選擇第一個maven-archetype-archetype
添加依賴(pom.xml)
記得修改自己hbase的版本,我的是2.5.4
設(shè)置好后Reload一下
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>創(chuàng)建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 # 創(chuàng)建一個新表,表名為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","現(xiàn)代信息產(chǎn)業(yè)學(xué)院");
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();
}
}
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java操作mongodb之多表聯(lián)查的實現(xiàn)($lookup)
這篇文章主要介紹了java操作mongodb之多表聯(lián)查的實現(xiàn)($lookup),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java使用POI-TL和JFreeChart動態(tài)生成Word報告
本文介紹了使用POI-TL和JFreeChart生成包含動態(tài)數(shù)據(jù)和圖表的Word報告的方法,并分享了實際開發(fā)中的踩坑經(jīng)驗,通過代碼示例講解的非常詳細,具有一定的參考價值,需要的朋友可以參考下2025-02-02
Apache?log4j2-RCE?漏洞復(fù)現(xiàn)及修復(fù)建議(CVE-2021-44228)
Apache?Log4j2是一款Java日志框架,大量應(yīng)用于業(yè)務(wù)系統(tǒng)開發(fā)。2021年11月24日,阿里云安全團隊向Apache官方報告了Apache?Log4j2遠程代碼執(zhí)行漏洞(CVE-2021-44228),本文給大家介紹Apache?log4j2-RCE?漏洞復(fù)現(xiàn)(CVE-2021-44228)的相關(guān)知識,感興趣的朋友一起看看吧2021-12-12
解決工具接口調(diào)用報錯:error:Unsupported Media Type問題
當(dāng)遇到"UnsupportedMediaType"錯誤時,意味著HTTP請求的Content-Type與服務(wù)器期望的不匹配,比如服務(wù)器期待接收JSON格式數(shù)據(jù),而發(fā)送了純文本格式,常見的Content-Type類型包括text/html、application/json、multipart/form-data等2024-10-10
java poi設(shè)置生成的word的圖片為上下型環(huán)繞以及其位置的實現(xiàn)
這篇文章主要介紹了java poi設(shè)置生成的word的圖片為上下型環(huán)繞以及其位置的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
java并發(fā)編程包JUC線程同步CyclicBarrier語法示例
這篇文章主要為大家介紹了java并發(fā)編程工具包JUC線程同步CyclicBarrier語法使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03

