• docker 部署hbase 并且java Api连接


    1. 首先先运行容器
    
    docker run -d --name hbase -p 2181:2181 -p 16010:16010 -p16000:16000 -p 16020:16020 -p 16030:16030 harisekhon/hbase
    
    • 1
    • 2

    2.在本机的hosts中注册docker的id
    因为docker内部集成了其他环境而其他环境 中的ip是docker id 所以需要在hosts中转换

    192.168.80.120	dockerhbase sa445is094a
    
    • 1

    3.客户端连接依赖

    
    >
                <!--        hbaseClient依赖包,需要排除日志log4j,不然和springboot的冲突了-->
    
                    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
                    
                        org.apache.hbase
                        hbase-client
                        2.5.4
                        
                            
                                org.slf4j
                                slf4j-log4j12
                            
                        
                    
                    
                    
                        org.apache.hbase
                        hbase
                        2.5.4
                        pom
                    
                >
                    >log4j>
                    >log4j>
                    >1.2.17>
                >
    
            >
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    俩个依赖出现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
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    4.java api 调用

    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();
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
  • 相关阅读:
    CSP-J/S 2023第一轮认证晋级分数线有些爆冷,超出想象
    《HelloGitHub》第 73 期
    PS工作记录——图片元素复制,智能抠图
    linux-守护进程daemon
    JVM学习总结(一)
    nmp ERR! code ERR SOCKET TIMEOUT nmp ERR!network npmSocket timeout(已解决)
    【毕业设计】电商产品评论数据分析可视化(情感分析) - python 大数据
    实现网站都变成灰色-filter
    Java ~ Reference ~ SoftReference
    Spring、Spring MVC、Spring boot、Spring Cloud面试题(史上最全面试题,精心整理100家互联网企业,面试必过)
  • 原文地址:https://blog.csdn.net/qq_55272229/article/details/134539153