zoukankan      html  css  js  c++  java
  • HBase的JavaAPI操作

     1 package hbase;
     2 
     3 import org.apache.hadoop.conf.Configuration;
     4 import org.apache.hadoop.hbase.HBaseConfiguration;
     5 import org.apache.hadoop.hbase.HColumnDescriptor;
     6 import org.apache.hadoop.hbase.HTableDescriptor;
     7 import org.apache.hadoop.hbase.client.Get;
     8 import org.apache.hadoop.hbase.client.HBaseAdmin;
     9 import org.apache.hadoop.hbase.client.HTable;
    10 import org.apache.hadoop.hbase.client.Put;
    11 import org.apache.hadoop.hbase.client.Result;
    12 import org.apache.hadoop.hbase.client.ResultScanner;
    13 import org.apache.hadoop.hbase.client.Scan;
    14 
    15 /**
    16  * 要先将HBase相关jar包添加进去!!!
    17  * 
    18  * 创建表、删除表 (使用HBaseAdmin)
    19  * 
    20  * 插入记录、查询一条记录、遍历所有记录 (使用HTable)
    21  * 
    22  * @author ahu_lichang
    23  * 
    24  */
    25 public class HBaseApp {
    26 
    27     private static final String TABLE_NAME = "table1";
    28     private static final String FAMILY_NAME = "family1";
    29     private static final String ROW_KEY = "rowkey1";
    30 
    31     public static void main(String[] args) throws Exception {
    32         Configuration conf = HBaseConfiguration.create();
    33         /*
    34          * hbase操作必备
    35          */
    36         conf.set("hbase.rootdir", "hdfs://hadoop0:9000/hbase");
    37         // 使用eclipse时必须添加这个,否则无法定位
    38         conf.set("hbase.zookeeper.quorum", "hadoop0");
    39         /*
    40          * 创建表
    41          */
    42         HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
    43         /*
    44          * if (!hBaseAdmin.tableExists(TABLE_NAME)) { HTableDescriptor
    45          * hTableDescriptor = new HTableDescriptor(TABLE_NAME);
    46          * HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(
    47          * FAMILY_NAME); hTableDescriptor.addFamily(hColumnDescriptor);
    48          * hBaseAdmin.createTable(hTableDescriptor); }
    49          */
    50 
    51         /*
    52          * 添加一条记录
    53          */
    54         HTable hTable = new HTable(conf, TABLE_NAME);
    55         /*
    56          * Put put = new Put(ROW_KEY.getBytes());
    57          * put.add(FAMILY_NAME.getBytes(), "age".getBytes(), "25".getBytes());
    58          * hTable.put(put);
    59          */
    60 
    61         /*
    62          * 查询一条记录
    63          */
    64 
    65         /*
    66          * Get get = new Get(ROW_KEY.getBytes()); Result result =
    67          * hTable.get(get); byte[] value = result
    68          * .getValue(FAMILY_NAME.getBytes(), "age".getBytes()); //
    69          * keyvalues={rowkey1/family1:age/1491571143625/Put/vlen=2/ts=0} 25
    70          * System.out.println(result + "	" + new String(value));
    71          */
    72 
    73         /*
    74          * 遍历所有记录
    75          */
    76         Scan scan = new Scan();
    77         ResultScanner resultScanner = hTable.getScanner(scan);
    78         for (Result result : resultScanner) {
    79             byte[] value = result.getValue(FAMILY_NAME.getBytes(),
    80                     "age".getBytes());
    81             System.out.println(result + "	" + new String(value));
    82         }
    83 
    84         hTable.close();
    85         /*
    86          * 删除表
    87          */
    88         /*
    89          * hBaseAdmin.disableTable(TABLE_NAME);
    90          * hBaseAdmin.deleteTable(TABLE_NAME);
    91          */
    92     }
    93 
    94 }
  • 相关阅读:
    unable to import maven project see logs for details
    全栈工程师:第一章:NodeJS的安装与配置
    Unable to open debugger port (127.0.0.1:63959): java.net.SocketException "socket closed",编译过来就是无法打开调试器端口,套接字已关闭
    我的分享:第七章:数据埋点
    我的分享:第六章:IDEA的优秀插件
    我的分享:第五章:java程序员一个人搭建网站(静态的,动态的都有)
    Docker:第三章:简单入门和深入理解
    我的分享:第四章:深入理解MySQL索引底层数据结构与算法
    NPM使用详解(上)
    JS实现继承的几种方式
  • 原文地址:https://www.cnblogs.com/ahu-lichang/p/6680166.html
Copyright © 2011-2022 走看看