zoukankan      html  css  js  c++  java
  • java 连接 kerberos 认证的 HBase 和 HDFS

    这是两个功能,都很简单就写一块了。。

    简单到什么程度呢,简单到只贴代码就可以了。。。

    HBase

    package com.miras.data;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.Connection;
    import org.apache.hadoop.hbase.client.ConnectionFactory;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.client.Table;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.apache.hadoop.security.UserGroupInformation;
    
    import java.io.IOException;
    
    /**
     * @author sunzq
     * @since 2017/8/30
     */
    public class HbaseMain {
        public static void main(String[] args) throws IOException {
            System.setProperty("java.security.krb5.conf", "/app/conf/krb5.conf");
            Configuration conf = HBaseConfiguration.create();
            conf.set("hadoop.security.authentication", "Kerberos");
            UserGroupInformation.setConfiguration(conf);
            UserGroupInformation.loginUserFromKeytab("storm-miras", "/app/conf/storm.headless.keytab");
            Connection connection = ConnectionFactory.createConnection(conf);
            Table table = connection.getTable(TableName.valueOf("hbase_krb"));
            Put put = new Put(Bytes.toBytes(2));
            put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("world"));
            table.put(put);
            table.close();
            connection.close();
        }
    }

    HDFS

    package com.miras.data;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileStatus;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.security.UserGroupInformation;
    
    import java.io.IOException;
    
    /**
     * @author sunzq
     * @since 2017/8/30
     */
    public class HdfsMain {
        public static void main(String[] args) throws IOException {
            System.setProperty("java.security.krb5.conf", "/app/conf/krb5.conf");
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://node1:8020");
            conf.set("hadoop.security.authentication", "kerberos");
            UserGroupInformation.setConfiguration(conf);
            UserGroupInformation.loginUserFromKeytab("storm-miras", "/app/conf/storm.headless.keytab");
            FileSystem fs = FileSystem.get(conf);
            FileStatus[] fsStatus = fs.listStatus(new Path("/"));
            for (int i = 0; i < fsStatus.length; i++) {
                System.out.println(fsStatus[i].getPath().toString());
            }
        }
    }

    水了一篇

  • 相关阅读:
    修改Firebug字体
    [CodeWars][JS]如何判断给定的数字是否整数
    [CodeWars][JS]实现链式加法
    【ACM成长之路】刷题记录
    【C++】用于ACM/OI等算法竞赛的读入优化
    C# 读取写入excel单元格(包括对excel的一些基本操作)
    Git上传本地项目到GitHub等云托管仓库
    贝塞尔曲线(B-spline)的原理与应用
    【已解决】Ubuntu U盘启动出现“Failed to load ldlinux.c32”问题
    【算法】Tarjan算法求强连通分量
  • 原文地址:https://www.cnblogs.com/kischn/p/7454550.html
Copyright © 2011-2022 走看看