{"id":6739,"date":"2021-09-10T10:25:06","date_gmt":"2021-09-10T02:25:06","guid":{"rendered":"https:\/\/nj.transwarp.cn:8180\/?p=6739"},"modified":"2022-04-06T16:51:15","modified_gmt":"2022-04-06T08:51:15","slug":"%e6%9c%aa%e5%bc%80%e5%90%af%e5%bc%ba%e8%ae%a4%e8%af%81%e4%b8%8b%ef%bc%8cjava%e5%af%b9%e6%8e%a5%e5%bc%80%e5%90%afkerberos%e7%9a%84hyperbase","status":"publish","type":"post","link":"https:\/\/kbwp.transwarp.cn\/?p=6739","title":{"rendered":"\u672a\u5f00\u542f\u5f3a\u8ba4\u8bc1\u4e0b\uff0cjava\u5bf9\u63a5\u5f00\u542fkerberos\u7684hyperbase"},"content":{"rendered":"<h3>\u6982\u8981\u63cf\u8ff0<\/h3>\n<hr \/>\n<p>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\uff0c\u5982\u4f55\u5728\u8001\u7248\u672c\uff08\u4ee5TDH6.0.2\u4e3a\u4f8b\uff09\u672a\u5f00\u542f\u5f3a\u8ba4\u8bc1\u7684\u73af\u5883\u5982\u4f55\u901a\u8fc7java\u8bbf\u95ee\u5f00\u542fkerberos\u7684hyperbase\u670d\u52a1\u3002<\/p>\n<p>\u4ee5\u4e0b\u5f00\u53d1\u73af\u5883\u4ee5IDEA + Windows10 + jdk1.8 + Maven3.6.1\u4e3a\u4f8b<br \/>\n\u8bf7\u5b89\u88c5\u597d\u4ee5\u4e0a\u5f00\u53d1\u5de5\u5177<\/p>\n<h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<hr \/>\n<h4>1\u3001\u83b7\u53d6Hbase api\u8fd0\u884c\u4f9d\u8d56\u7684jar\u5305<\/h4>\n<p>\u4ecehyperbase\u5bb9\u5668\u6216\u8005\u6700\u65b0\u7684TDH-Client\/hyperbase\/lib\u76ee\u5f55\u4e0b\uff0c\u83b7\u53d6\u4f9d\u8d56\u7684jar\u5305<\/p>\n<pre><code class=\"language-ruby\">\u8fdb\u5165\u5bb9\u5668\u5185\u90e8\u5c06jar\u5305\u4f7f\u7528\u547d\u4ee4scp\u83b7\u53d6\na) \u83b7\u53d6hyperbase\u670d\u52a1\u6240\u5728pod\nkubectl get pod|grep hyperbase\nb) \u8fdb\u5165\u4efb\u610f\u4e00\u4e2a\u8be5\u670d\u52a1pod\u4e2d\uff0c\u8fd9\u91cc\u4ee5regionserver\u4e3a\u4f8b\nkubectl exec -it hyperbase-regionserver-hyperbase1-2285948569-0b7cl bash\nc) \u53d6\u51fajar\u5305\nscp \/usr\/lib\/hbase\/lib\/*.jar 172.22.23.1:\/mnt\/disk1<\/code><\/pre>\n<h4>2\u3001\u914d\u7f6ekrb5\u6587\u4ef6<\/h4>\n<p>windows\u73af\u5883\u4e0b\uff0c\u5c06\u96c6\u7fa4\u4e0a\u83b7\u53d6\u7684<code>\/etc\/krb5.conf<\/code>\u6587\u4ef6\uff0c\u653e\u7f6e\u5230<code>C:\/\/Windows\/krb5.ini<\/code> (\u6ce8\u610f\u4fee\u6539\u540e\u7f00\u4e3a <code>ini<\/code>)<\/p>\n<h4>3\u3001demo\u53c2\u8003\u5982\u4e0b<\/h4>\n<pre><code class=\"language-ruby\">import org.apache.hadoop.conf.Configuration;\nimport org.apache.hadoop.hbase.*;\nimport org.apache.hadoop.hbase.client.*;\nimport org.apache.hadoop.hbase.util.Bytes;\nimport org.apache.hadoop.security.UserGroupInformation;\n\nimport java.io.IOException;\nimport java.util.ArrayList;\nimport java.util.List;\n\n\/**\n * Created by gordon on 2015\/6\/16.\n *\/\npublic class HyperbaseSecureTest {\n\n    private static Configuration conf = null;\n\n    static {\n        Configuration HBASE_CONFIG = new Configuration();\n        HBASE_CONFIG.set(\"hbase.zookeeper.quorum\", \"tdh60201,tdh60202,tdh60203\");\n        HBASE_CONFIG.set(\"hbase.master.kerberos.principal\", \"hbase\/_HOST@TDH\");\n        HBASE_CONFIG.set(\"hbase.regionserver.kerberos.principal\", \"hbase\/_HOST@TDH\");\n        HBASE_CONFIG.set(\"hbase.security.authentication\", \"kerberos\");\n        HBASE_CONFIG.set(\"zookeeper.znode.parent\", \"\/hyperbase1\");\n        HBASE_CONFIG.set(\"hadoop.security.authentication\", \"kerberos\");\n\n        conf = HBaseConfiguration.create(HBASE_CONFIG);\n    }\n\n    \/**\n     * \u521b\u5efa\u4e00\u5f20\u8868\n     *\/\n    public static void creatTable(String tableName, String[] familys) throws Exception {\n        HBaseAdmin admin = new HBaseAdmin(conf);\n        if (admin.tableExists(tableName)) {\n            System.out.println(\"table already exists!\");\n        } else {\n            HTableDescriptor tableDesc = new HTableDescriptor(tableName);\n            for(int i=0; i < familys.length; i++){\n                tableDesc.addFamily(new HColumnDescriptor(familys[i]));\n            }\n            admin.createTable(tableDesc);\n            System.out.println(\"create table \" + tableName + \" ok.\");\n        }\n    }\n\n    \/**\n     * \u5220\u9664\u8868\n     *\/\n    public static void deleteTable(String tableName) throws Exception {\n        try {\n            HBaseAdmin admin = new HBaseAdmin(conf);\n            admin.disableTable(tableName);\n            admin.deleteTable(tableName);\n            System.out.println(\"delete table \" + tableName + \" ok.\");\n        } catch (MasterNotRunningException e) {\n            e.printStackTrace();\n        } catch (ZooKeeperConnectionException e) {\n            e.printStackTrace();\n        }\n    }\n\n    \/**\n     * \u63d2\u5165\u4e00\u884c\u8bb0\u5f55\n     *\/\n    public static void addRecord (String tableName, String rowKey, String family, String qualifier, String value)\n            throws Exception{\n        try {\n            HTable table = new HTable(conf, tableName);\n            Put put = new Put(Bytes.toBytes(rowKey));\n            put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));\n            table.put(put);\n            System.out.println(\"insert recored \" + rowKey + \" to table \" + tableName +\" ok.\");\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n\n    \/**\n     * \u5220\u9664\u4e00\u884c\u8bb0\u5f55\n     *\/\n    public static void delRecord (String tableName, String rowKey) throws IOException{\n        HTable table = new HTable(conf, tableName);\n        List list = new ArrayList();\n        Delete del = new Delete(rowKey.getBytes());\n        list.add(del);\n        table.delete(list);\n        System.out.println(\"del recored \" + rowKey + \" ok.\");\n    }\n\n    \/**\n     * \u67e5\u627e\u4e00\u884c\u8bb0\u5f55\n     *\/\n    public static void getOneRecord (String tableName, String rowKey) throws IOException{\n        HTable table = new HTable(conf, tableName);\n        Get get = new Get(rowKey.getBytes());\n        Result rs = table.get(get);\n        for(KeyValue kv : rs.raw()){\n            System.out.print(new String(kv.getRow()) + \" \" );\n            System.out.print(new String(kv.getFamily()) + \":\" );\n            System.out.print(new String(kv.getQualifier()) + \" \" );\n            System.out.print(kv.getTimestamp() + \" \" );\n            System.out.println(new String(kv.getValue()));\n        }\n    }\n\n    \/**\n     * \u663e\u793a\u6240\u6709\u6570\u636e\n     *\/\n    public static void getAllRecord (String tableName) {\n        try{\n            HTable table = new HTable(conf, tableName);\n            Scan s = new Scan();\n            ResultScanner ss = table.getScanner(s);\n            for(Result r:ss){\n                for(KeyValue kv : r.raw()){\n                    System.out.print(new String(kv.getRow()) + \" \");\n                    System.out.print(new String(kv.getFamily()) + \":\");\n                    System.out.print(new String(kv.getQualifier()) + \" \");\n                    System.out.print(kv.getTimestamp() + \" \");\n                    System.out.println(new String(kv.getValue()));\n                }\n            }\n        } catch (IOException e){\n            e.printStackTrace();\n        }\n    }\n\n    public static void main(String[] args) throws IOException {\n        try {\n            UserGroupInformation.setConfiguration(conf);\n            UserGroupInformation.loginUserFromKeytab(\"hbase\/tdh60201@TDH\", \"C:\\\\Users\\\\17171\\\\Desktop\\\\hyperbase.keytab\");\n\/\/        HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);\n\/\/        hBaseAdmin.createTable(new HTableDescriptor(\"gordon2\"));\n\n            String tablename = \"scores\";\n            String[] familys = {\"grade\", \"course\"};\n            HyperbaseSecureTest.creatTable(tablename, familys);\n\n            \/\/add record zkb\n            HyperbaseSecureTest.addRecord(tablename,\"zkb\",\"grade\",\"\",\"5\");\n            HyperbaseSecureTest.addRecord(tablename,\"zkb\",\"course\",\"\",\"90\");\n            HyperbaseSecureTest.addRecord(tablename,\"zkb\",\"course\",\"math\",\"97\");\n            HyperbaseSecureTest.addRecord(tablename,\"zkb\",\"course\",\"art\",\"87\");\n            \/\/add record  baoniu\n            HyperbaseSecureTest.addRecord(tablename,\"baoniu\",\"grade\",\"\",\"4\");\n            HyperbaseSecureTest.addRecord(tablename,\"baoniu\",\"course\",\"math\",\"89\");\n\n            System.out.println(\"===========get one record========\");\n            HyperbaseSecureTest.getOneRecord(tablename, \"zkb\");\n\n            System.out.println(\"===========show all record========\");\n            HyperbaseSecureTest.getAllRecord(tablename);\n\n            System.out.println(\"===========del one record========\");\n            HyperbaseSecureTest.delRecord(tablename, \"baoniu\");\n            HyperbaseSecureTest.getAllRecord(tablename);\n\n            System.out.println(\"===========show all record========\");\n            HyperbaseSecureTest.getAllRecord(tablename);\n\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<br \/>\n1\u3001hbase.zookeeper.quorum \u53c2\u6570\u662fzookeeper\u7684\u4e09\u4e2a\u8282\u70b9\uff0c\u672c\u673a\u914d\u7f6e\u4e86HOSTS\u6587\u4ef6\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528hostname\uff1b<br \/>\n<code>HBASE_CONFIG.set(&quot;hbase.zookeeper.quorum&quot;, &quot;tdh60201,tdh60202,tdh60203&quot;);<\/code><br \/>\n2\u3001\u4ece\u96c6\u7fa4\u83b7\u53d6\u7684hyperbase.keytab\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7klist -ket hyperbase.keytab \u547d\u4ee4\u83b7\u53d6\u5bf9\u5e94\u7684SPN<br \/>\n<code>UserGroupInformation.loginUserFromKeytab(&quot;hbase\/tdh60201@TDH&quot;, &quot;C:\\\\Users\\\\17171\\\\Desktop\\\\hyperbase.keytab&quot;);<\/code><\/p>\n<p><strong>\u6267\u884c\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre><code class=\"language-ruby\">create table scores ok.\ninsert recored zkb to table scores ok.\ninsert recored zkb to table scores ok.\ninsert recored zkb to table scores ok.\ninsert recored zkb to table scores ok.\ninsert recored baoniu to table scores ok.\ninsert recored baoniu to table scores ok.\n===========get one record========\nzkb course: 1631239340867 90\nzkb course:art 1631239340898 87\nzkb course:math 1631239340884 97\nzkb grade: 1631239340763 5\n===========show all record========\nbaoniu course:math 1631239340924 89\nbaoniu grade: 1631239340915 4\nzkb course: 1631239340867 90\nzkb course:art 1631239340898 87\nzkb course:math 1631239340884 97\nzkb grade: 1631239340763 5\n===========del one record========\ndel recored baoniu ok.\nzkb course: 1631239340867 90\nzkb course:art 1631239340898 87\nzkb course:math 1631239340884 97\nzkb grade: 1631239340763 5\n===========show all record========\nzkb course: 1631239340867 90\nzkb course:art 1631239340898 87\nzkb course:math 1631239340884 97\nzkb grade: 1631239340763 5\n\nProcess finished with exit code 0<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/09\/image-1631240638596.png\" alt=\"file\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981\u63cf\u8ff0 \u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\uff0c\u5982\u4f55\u5728\u8001\u7248\u672c\uff08\u4ee5TDH6.0.2\u4e3a\u4f8b\uff09\u672a\u5f00\u542f\u5f3a\u8ba4\u8bc1\u7684\u73af\u5883\u5982\u4f55\u901a\u8fc7java\u8bbf\u95ee\u5f00\u542fker ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/kbwp.transwarp.cn\/?p=6739\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-6739","post","type-post","status-publish","format-standard","hentry","category-api"],"acf":[],"_links":{"self":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/6739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6739"}],"version-history":[{"count":1,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/6739\/revisions"}],"predecessor-version":[{"id":6741,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/6739\/revisions\/6741"}],"wp:attachment":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}