- 首先先运行容器
docker run -d--name hbase -p2181:2181 -p16010:16010 -p16000:16000-p16020:16020 -p16030:16030 harisekhon/hbase
2.在本机的hosts中注册docker的id
因为docker内部集成了其他环境而其他环境 中的ip是docker id 所以需要在hosts中转换
192.168.80.120 dockerhbase sa445is094a
3.客户端连接依赖
<dependencies>
<!-- hbaseClient依赖包,需要排除日志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>
俩个依赖出现log4j的依赖申明错误,所以这里需要排除,并且新建一个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 调用
publicclassPractice4{publicstaticConfiguration configuration;publicstaticConnection connection;publicstaticAdmin admin;publicstaticvoidmain(String[] args)throwsIOException{init();createTable("student",newString[]{"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();}publicstaticvoidinit(){
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();}}publicstaticvoidclose(){try{if(admin !=null){
admin.close();}if(null!= connection){
connection.close();}}catch(IOException e){
e.printStackTrace();}}publicstaticvoidcreateTable(String myTableName,String[] colFamily)throwsIOException{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());}}publicstaticvoidinsertData(String tableName,String rowKey,String colFamily,String col,String val)throwsIOException{Table table = connection.getTable(TableName.valueOf(tableName));Put put =newPut(rowKey.getBytes());
put.addColumn(colFamily.getBytes(),col.getBytes(), val.getBytes());
table.put(put);
table.close();}publicstaticvoidgetData(String tableName,String rowKey,String colFamily,String col)throwsIOException{Table table = connection.getTable(TableName.valueOf(tableName));Get get =newGet(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());Result result = table.get(get);System.out.println(newString(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
table.close();}}
版权归原作者 蓝胖子不是胖子 所有, 如有侵权,请联系我们删除。