{"id":1430,"date":"2022-06-14T11:59:38","date_gmt":"2022-06-14T03:59:38","guid":{"rendered":"http:\/\/www.eait.co\/?p=1430"},"modified":"2022-06-14T11:59:38","modified_gmt":"2022-06-14T03:59:38","slug":"hbaseapi%e7%bc%96%e5%86%99","status":"publish","type":"post","link":"https:\/\/notes.coremix.net\/?p=1430","title":{"rendered":"HbaseAPI\u7f16\u5199"},"content":{"rendered":"<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage hbaseapi;\r\n\r\nimport com.google.inject.internal.util.$AsynchronousComputationException;\r\nimport com.sun.tools.internal.xjc.reader.xmlschema.BindYellow;\r\nimport org.apache.hadoop.conf.Configuration;\r\nimport org.apache.hadoop.hbase.*;\r\nimport org.apache.hadoop.hbase.client.*;\r\nimport org.apache.hadoop.hbase.util.Bytes;\r\nimport sun.rmi.transport.tcp.TCPConnection;\r\n\r\nimport java.io.IOException;\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\npublic class HbaseTest {\r\n    static Configuration conf;\r\n    static Connection connection = null;\r\n    static HBaseAdmin admin = null;\r\n    static {\r\n        try {\r\n            conf = HBaseConfiguration.create();\r\n            conf.set(&quot;hbase.zookeeper.quorum&quot;,&quot;bigdata166&quot;);\r\n            conf.set(&quot;hbase.zookeeper.property.clientport&quot;,&quot;2181&quot;);\r\n            connection = ConnectionFactory.createConnection(conf);\r\n            admin = (HBaseAdmin) connection.getAdmin();\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * \u5224\u65ad\u8868\u662f\u5426\u5b58\u5728\r\n      * @param tableName\r\n     * @return\r\n     * @throws IOException\r\n     *\/\r\n\r\n    public static boolean isTableExist(String tableName) throws IOException {\r\n        return admin.tableExists(tableName);\r\n    }\r\n\r\n    \/**\r\n     * \u5217\u65cf\u5efa\u8bae\u5c0f\u4e8e3\u4e2a\r\n     * @param tableName\r\n     * @param columnFamily\r\n     * @throws Exception\r\n     *\/\r\n    public static void createTable(String tableName,String... columnFamily)throws Exception{\r\n        if (isTableExist(tableName)) {\r\n            System.out.println(&quot;\u8868\u5df2\u7ecf\u5b58\u5728&quot;);\r\n           }else{\r\n                \/\/\u8868\u63cf\u8ff0\u5668\r\n                HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));\r\n                for (String cf : columnFamily) {\r\n                    HColumnDescriptor hcd = new HColumnDescriptor(cf);\r\n                    desc.addFamily(hcd);\r\n                }\r\n                admin.createTable(desc);\r\n                System.out.println(&quot;\u8868\u521b\u5efa\u6210\u529f&quot;+tableName);\r\n            }\r\n    }\r\n    public static void droptable(String tableName) throws Exception{\r\n        if (isTableExist(tableName)){\r\n            admin.disableTable(tableName);\r\n            admin.deleteTable(tableName);\r\n            System.out.println(&quot;\u8868\u5df2\u5220\u9664&quot;);\r\n        }else{\r\n            System.out.println(&quot;\u8868\u4e0d\u5b58\u5728&quot;);\r\n        }\r\n    }\r\n\r\n    public static void addRowData(String tableName,String rowKey,String columnFamily,String column,String value) throws IOException {\r\n        if (isTableExist(tableName)){\r\n\/\/        HTable hTable = new HTable(conf, tableName);\r\n            Table hTable = connection.getTable(TableName.valueOf(tableName)); \/\/  \u65b0API\uff0c\u8001API\u4ee3\u7801\u91cf\u591a\u7684\u65f6\u5019\u53ef\u80fd\u51fa\u73b0\u7ebf\u7a0b\u4e0d\u5b89\u5168\r\n            \/\/ \u5f80put\u4e2d\u653e\u5165\u6570\u636e\r\n        Put put = new Put(Bytes.toBytes(rowKey));\r\n        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column),Bytes.toBytes(value));\/\/\u7b2c\u4e00\u79cd\u65b9\u6cd5\r\n\/\/        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),Bytes.toBytes(value)); \/\/\u7b2c\u4e8c\u79cd\u65b9\u6cd5\r\n        hTable.put(put);\r\n        hTable.close();\r\n            System.out.println(&quot;\u6570\u636e\u63d2\u5165\u6210\u529f&quot;);}else{\r\n            System.out.println(&quot;\u8868\u4e0d\u5b58\u5728&quot;);\r\n        }\r\n    }\r\n\r\n    public static void deleteMultiRow(String tableName,String... rows) throws IOException {\r\n\/\/        HTable hTable = new HTable(conf, tableName);\r\n        Table hTable = connection.getTable(TableName.valueOf(tableName)); \/\/  \u65b0API\uff0c\u8001API\u4ee3\u7801\u91cf\u591a\u7684\u65f6\u5019\u53ef\u80fd\u51fa\u73b0\u7ebf\u7a0b\u4e0d\u5b89\u5168\r\n        List&lt;Delete&gt; deleteList = new ArrayList&lt;&gt;(); \/\/ \u7528\u4e8e\u6253\u5305\u5220\u9664\r\n        for (String row : rows) {\r\n            Delete delete = new Delete(Bytes.toBytes(row));\r\n            deleteList.add(delete);\r\n            System.out.println(&quot;\u5220\u9664\u884c\u6210\u529f&quot;);\r\n        }\r\n        hTable.delete(deleteList);\r\n        hTable.close();\r\n\r\n\r\n    }\r\n\r\n    public static void getAllRows(String tableName) throws IOException {\r\n\/\/        HTable hTable = new HTable(conf, tableName);\r\n        Table hTable = connection.getTable(TableName.valueOf(tableName)); \/\/  \u65b0API\uff0c\u8001API\u4ee3\u7801\u91cf\u591a\u7684\u65f6\u5019\u53ef\u80fd\u51fa\u73b0\u7ebf\u7a0b\u4e0d\u5b89\u5168\r\n        Scan scan = new Scan();\r\n        ResultScanner resultScanner= hTable.getScanner(scan);\r\n        for (Result result : resultScanner) {\r\n            Cell&#x5B;] cells = result.rawCells();\r\n            for (Cell cell : cells) {\r\n                System.out.println(&quot;RK:&quot;+Bytes.toString(CellUtil.cloneRow(cell)));\r\n                System.out.println(&quot;\u5217\u65cf:&quot;+Bytes.toString(CellUtil.cloneFamily(cell)));\r\n                System.out.println(&quot;\u5217:&quot;+Bytes.toString(CellUtil.cloneQualifier(cell)));\r\n                System.out.println(&quot;\u503c:&quot;+Bytes.toString(CellUtil.cloneValue(cell)));\r\n            }\r\n        }\r\n    }\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    \/**\r\n     * \u83b7\u5f97\u67d0\u4e00\u884c\u6570\u636e\r\n     *\r\n     * @param tableName\r\n     * @param rowKey\r\n     * @throws IOException\r\n     *\/\r\n    public static void getRow(String tableName, String rowKey) throws IOException {\r\n\r\n        Table table = connection.getTable(TableName.valueOf(tableName));\r\n        Get get = new Get(Bytes.toBytes(rowKey));\r\n\r\n        Result result = table.get(get);\r\n        for (Cell cell : result.rawCells()) {\r\n            System.out.println(&quot;\u884c\u952e:&quot; + Bytes.toString(result.getRow()));\r\n            System.out.println(&quot;\u5217\u65cf&quot; + Bytes.toString(CellUtil.cloneFamily(cell)));\r\n            System.out.println(&quot;\u5217:&quot; + Bytes.toString(CellUtil.cloneQualifier(cell)));\r\n            System.out.println(&quot;\u503c:&quot; + Bytes.toString(CellUtil.cloneValue(cell)));\r\n            System.out.println(&quot;\u65f6\u95f4\u6233:&quot; + cell.getTimestamp());\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * \u83b7\u5f97\u67d0\u4e00\u4e2avalue\u7684\u503c\r\n     *\r\n     * @param tableName\r\n     * @param rowKey\r\n     * @throws IOException\r\n     *\/\r\n    public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException {\r\n\r\n        Table table = connection.getTable(TableName.valueOf(tableName));\r\n        Get get = new Get(Bytes.toBytes(rowKey));\r\n        get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));\r\n\r\n        Result result = table.get(get);\r\n        for (Cell cell : result.rawCells()) {\r\n            System.out.println(&quot;\u884c\u952e:&quot; + Bytes.toString(result.getRow()));\r\n            System.out.println(&quot;\u5217\u65cf&quot; + Bytes.toString(CellUtil.cloneFamily(cell)));\r\n            System.out.println(&quot;\u5217:&quot; + Bytes.toString(CellUtil.cloneQualifier(cell)));\r\n            System.out.println(&quot;\u503c:&quot; + Bytes.toString(CellUtil.cloneValue(cell)));\r\n            System.out.println(&quot;\u65f6\u95f4\u6233:&quot; + cell.getTimestamp());\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * \u521d\u59cb\u5316\u547d\u540d\u7a7a\u95f4\r\n     *\r\n     * @param namespace \u547d\u540d\u7a7a\u95f4\u7684\u540d\u5b57\r\n     * @throws Exception\r\n     *\/\r\n    public static void initNameSpace(String namespace) throws Exception {\r\n\r\n        \/\/\u547d\u540d\u7a7a\u95f4\u63cf\u8ff0\u5668\r\n        NamespaceDescriptor nd = NamespaceDescriptor\r\n                .create(namespace)\r\n                .addConfiguration(&quot;AUTHOR&quot;, &quot;andy&quot;)\r\n                .build();\r\n        \/\/\u901a\u8fc7admin\u5bf9\u8c61\u6765\u521b\u5efa\u547d\u540d\u7a7a\u95f4\r\n        admin.createNamespace(nd);\r\n        System.out.println(&quot;\u5df2\u521d\u59cb\u5316\u547d\u540d\u7a7a\u95f4&quot;);\r\n        \/\/\u5173\u95ed\u4e24\u4e2a\u5bf9\u8c61\r\n        close(admin, connection);\r\n    }\r\n\r\n    \/**\r\n     * \u5173\u95edadmin\u5bf9\u8c61\u548cconnection\u5bf9\u8c61\r\n     *\r\n     * @param admin      \u5173\u95edadmin\u5bf9\u8c61\r\n     * @param connection \u5173\u95edconnection\u5bf9\u8c61\r\n     * @throws IOException IO\u5f02\u5e38\r\n     *\/\r\n    private static void close(Admin admin, Connection connection) throws IOException {\r\n        if (admin != null) {\r\n            admin.close();\r\n        }\r\n        if (connection != null) {\r\n            connection.close();\r\n        }\r\n    }\r\n\r\n\r\n\r\n\r\n    public static void main(String&#x5B;] args) throws Exception {\r\n\/\/        boolean flage = isTableExist(&quot;student&quot;);\r\n\/\/        System.out.println(flage);\r\n\r\n\/\/        createTable(&quot;ShallTable2&quot;,&quot;cf1&quot;,&quot;cf2&quot;,&quot;cf3&quot;);\r\n\/\/        droptable(&quot;Shalltable2&quot;);\r\n        addRowData(&quot;student&quot;,&quot;1007&quot;,&quot;cf1&quot;,&quot;xinAPIideacolumn2&quot;,&quot;\u8fd9\u662f\u6765\u81eaidea\u7684\u6570\u636e&quot;);\r\n        \/\/\u63d2\u5165\u4e0d\u5b58\u5728\u7684\u5217\u65cf\u540d\uff0c\u4f1a\u62a5\u9519\r\n\/\/        addRowData(&quot;student&quot;,&quot;1006&quot;,&quot;cf2&quot;,&quot;ideacolumn&quot;,&quot;\u8fd9\u662f\u6765\u81eaidea\u7684\u6570\u636e&quot;);\r\n\/\/        deleteMultiRow(&quot;student&quot;,&quot;1006&quot;);\r\n        getAllRows(&quot;student&quot;);\r\n    }\r\n}\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>package hbaseapi; import com.google.inject.internal.uti [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1430","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1430"}],"version-history":[{"count":1,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1430\/revisions"}],"predecessor-version":[{"id":1431,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/1430\/revisions\/1431"}],"wp:attachment":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}