docker?部署hbase并且java?Api連接的操作過(guò)程
1.首先先運(yùn)行容器
docker run -d --name hbase -p 2181:2181 -p 16010:16010 -p16000:16000 -p 16020:16020 -p 16030:16030 harisekhon/hbase
2.在本機(jī)的hosts中注冊(cè)docker的id
因?yàn)閐ocker內(nèi)部集成了其他環(huán)境而其他環(huán)境 中的ip是docker id 所以需要在hosts中轉(zhuǎn)換
192.168.80.120 dockerhbase sa445is094a
3.客戶(hù)端連接依賴(lài)
<dependencies>
<!-- hbaseClient依賴(lài)包,需要排除日志log4j,不然和springboot的沖突了-->
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.5.4</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>2.5.4</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>倆個(gè)依賴(lài)出現(xiàn)log4j的依賴(lài)申明錯(cuò)誤,所以這里需要排除,并且新建一個(gè)properties后綴的配置文件
log4j.rootLogger=debug, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=example.log log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=5 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
4.java api 調(diào)用
public class Practice4 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args)throws IOException{
init();
createTable("student",new String[]{"score"});
insertData("student","zhangsan","score","English","69");
insertData("student","zhangsan","score","Math","86");
insertData("student","zhangsan","score","Computer","77");
getData("student", "zhangsan", "score","English");
close();
}
public static void init(){
configuration = HBaseConfiguration.create();
// configuration.set("hbase.rootdir","hdfs://192.168.249.132:8020/");
configuration.set("hbase.zookeeper.quorum", "192.168.249.132");
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();
}
if(null != connection){
connection.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("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 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);
System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
table.close();
}
}到此這篇關(guān)于docker 部署hbase 并且java Api連接的文章就介紹到這了,更多相關(guān)docker 部署hbase內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
docker拷貝文件到主機(jī)及導(dǎo)入導(dǎo)出容器及運(yùn)行導(dǎo)出容器方式
這篇文章主要介紹了docker拷貝文件到主機(jī)及導(dǎo)入導(dǎo)出容器及運(yùn)行導(dǎo)出容器方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Docker無(wú)法stop或者rm指定容器問(wèn)題解決方案
這篇文章主要介紹了Docker無(wú)法stop或者rm指定容器問(wèn)題解決方案,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
Docker安裝Nacos單機(jī)的實(shí)現(xiàn)方式(配合寶塔)
這篇文章主要介紹了Docker安裝Nacos單機(jī)的實(shí)現(xiàn)方式(配合寶塔),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06
docker容器運(yùn)行成功但無(wú)法訪(fǎng)問(wèn)的原因分析及解決方案(以Tomcat為例親測(cè)有效)
這篇文章主要介紹了docker容器運(yùn)行成功但無(wú)法訪(fǎng)問(wèn)的原因分析及對(duì)應(yīng)解決方案(以Tomcat為例親測(cè)有效),文中通過(guò)圖文結(jié)合的方式介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03
查看Docker停止容器占用的內(nèi)存的實(shí)現(xiàn)方法小結(jié)
要查看 Docker 停止的容器占用的內(nèi)存,可以通過(guò)以下幾種方法來(lái)實(shí)現(xiàn),Docker 提供了一些命令和工具來(lái)幫助你管理和監(jiān)控容器的資源使用情況,需要的朋友可以參考下2024-11-11
Docker 容器之間的互相通信實(shí)現(xiàn)示例
本文主要介紹了Docker 容器之間的互相通信實(shí)現(xiàn)示例,通過(guò)創(chuàng)建自定義網(wǎng)絡(luò),你可以輕松地在 Docker 容器之間建立通信,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01

