zoukankan      html  css  js  c++  java
  • 用Java访问带有Kerberos认证的HBase

    程序代码实例如下:
    
      
    package com.hbasedemo;
    
    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.KeyValue;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Result;
    import org.apache.hadoop.hbase.client.ResultScanner;
    import org.apache.hadoop.hbase.client.Scan;
    import org.apache.hadoop.security.UserGroupInformation;
    
    public class Test {
           private static Configuration conf = null;
    
           static {
    
                 // 这个配置文件主要是记录 kerberos的相关配置信息,例如KDC是哪个IP?默认的realm是哪个?
                 // 如果没有这个配置文件这边认证的时候肯定不知道KDC的路径喽
                 // 这个文件也是从远程服务器上copy下来的
                System. setProperty("java.security.krb5.conf""C:/Users/dongzeguang/Downloads/krb5.conf" );
                
                 conf = HBaseConfiguration.create();
                 conf.set("hadoop.security.authentication" , "Kerberos" );
                 // 这个hbase.keytab也是从远程服务器上copy下来的, 里面存储的是密码相关信息
                 // 这样我们就不需要交互式输入密码了
                 conf.set("keytab.file" , "C:/Users/Downloads/hbase.keytab" );
                 // 这个可以理解成用户名信息,也就是Principal
                 conf.set("kerberos.principal" , "hbase/1722.myip.domain@HADOOP.COM" );           
                UserGroupInformation. setConfiguration(conf);
                 try {
                      UserGroupInformation. loginUserFromKeytab("hbase/1722.myip.domain@HADOOP.COM""C:/Users/Downloads/hbase.keytab" );
                } catch (IOException e) {
                       // TODO Auto-generated catch block
                      e.printStackTrace();
                }
          }
    
           public static void scanSpan(final String tableName) throws Exception {
                HTable table =  new HTable(conf, tableName);
                System. out.println("tablename:" +new String(table.getTableName()));
                Scan s new Scan();
                ResultScanner rs = table.getScanner(s);
                
                 for (Result r : rs) {
                      System. out.println(r.toString());
                      KeyValue[] kv = r. raw();
                       for (int i = 0; i < kv.length; i++) {
                            System. out.print(new String(kv[i].getRow()) + "");
                            System. out.print(new String(kv[i].getFamily()) + ":");
                            System. out.print(new String(kv[i].getQualifier() ) + "" );
                            System. out.print(kv[i].getTimestamp() + "" );
                            System. out.println(new String(kv[i].getValue() ));
                      }
                }
    
          }
    
           /**
           * @param args
           */
           public static void main(String[] args) {
                 // TODO Auto-generated method stub
                 try {
                      Test. scanSpan("h_span");
                } catch (Exception e) {
                       // TODO Auto-generated catch block
                      e.printStackTrace();
                }
          }
    
    }
  • 相关阅读:
    [ios] 响应上下左右滑动手势
    [ios]字符串转化成NSDate类型 计算与当前时间的相差 月数 天数 【转】
    [ios] NSSet,NSMutableSet[zhuan]
    [ios] 如何让xcode自动检查内存泄露 【转】
    iOS 使用Quartz 2D画虚线 【转】
    [ios]设置表格单元格交替背景 【转】
    [ios] IOS文件操作的两种方式:NSFileManager操作和流操作【转】
    [ios] 自定义UIAlertView样式,实现可替换背景和按钮 【转】
    [ios]上传应用程序到app store 【转】
    [ios] iOS中arc的设置与使用
  • 原文地址:https://www.cnblogs.com/felixzh/p/11586865.html
Copyright © 2011-2022 走看看