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

    官方文档:http://hbase.apache.org/book.html

    java简单操作hbase的表

      1 import org.apache.hadoop.conf.Configuration;
      2 import org.apache.hadoop.hbase.*;
      3 import org.apache.hadoop.hbase.client.*;
      4 import org.apache.hadoop.hbase.filter.CompareFilter;
      5 import org.apache.hadoop.hbase.filter.FilterList;
      6 import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
      7 import org.junit.Before;
      8 import org.junit.Test;
      9 
     10 import java.io.IOException;
     11 import java.util.ArrayList;
     12 import java.util.Iterator;
     13 import java.util.Random;
     14 
     15 /**
     16  * Created by Edward on 2016/6/19.
     17  */
     18 public class TestHbase {
     19 
     20     public static Configuration conf = null;
     21     public static TableName table = TableName.valueOf("phone");
     22     public static Random random = new Random();
     23 
     24     @Before
     25     public void setup()
     26     {
     27         //通过zookeeper集群,访问hbase
     28         conf = HBaseConfiguration.create();
     29         conf.set("hbase.zookeeper.quorum", "node1,node2,node3");
     30         System.out.println("setup");
     31     }
     32 
     33     @Test
     34     public void createTable() throws IOException {
     35         HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
     36        if(hBaseAdmin.tableExists(table)) {
     37             hBaseAdmin.disableTable(table);
     38             hBaseAdmin.deleteTable(table);
     39         }
     40         //
     41         HTableDescriptor hTableDescriptor = new HTableDescriptor(table);
     42         //列族 cf1
     43         HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("cf1");
     44         hColumnDescriptor.setBlockCacheEnabled(true);
     45         hColumnDescriptor.setBlocksize(128000);
     46         hColumnDescriptor.setMaxVersions(10);
     47         //表增加列族
     48         hTableDescriptor.addFamily(hColumnDescriptor);
     49         hBaseAdmin.createTable(hTableDescriptor);
     50     }
     51 
     52 
     53     @Test
     54     /**
     55      * 插入数据
     56      */
     57     public void insert() throws IOException {
     58 
     59         //创建htable对象
     60         HTable hTable = new HTable(conf, this.table);
     61         ArrayList<Put> list = new ArrayList<Put>();
     62 
     63         for(int i = 0; i<1000; i++)
     64         {
     65             //row_key
     66             Put put = new Put(String.valueOf(i).getBytes());
     67             //column,value
     68             put.add("cf1".getBytes(),"name".getBytes(),"ls".getBytes());
     69             put.add("cf1".getBytes(),"age".getBytes(), String.valueOf(i%100).getBytes());
     70             //把每个put对象放到列表中
     71             list.add(put);
     72         }
     73         //把信息放到表中
     74         hTable.put(list);
     75     }
     76 
     77 
     78     @Test
     79     /**
     80      * 查询
     81      */
     82     public void search() throws IOException
     83     {
     84 
     85         HTable hTable = new HTable(conf, this.table);
     86         // 创建row_key对应的get对象
     87         Get get = new Get("123".getBytes());
     88         // 获取get结果
     89         Result result = hTable.get(get);
     90         //获取 column latest cell
     91         Cell columnLatestCell = result.getColumnLatestCell("cf1".getBytes(), "name".getBytes());
     92         //使用 CellUtil 获取值
     93         byte[] bytes = CellUtil.cloneValue(columnLatestCell);
     94         System.out.println(new String(bytes));
     95     }
     96 
     97 
     98     @Test
     99     /**
    100      *  通过scan方法获取数据
    101      * **/
    102     public void search1() throws IOException
    103     {
    104         HTable hTable = new HTable(conf, this.table);
    105         //设置scan范围
    106         Scan scan = new Scan("400".getBytes(),"450".getBytes());
    107         //通过scan得到result scanner
    108         ResultScanner scanner = hTable.getScanner(scan);
    109         //使用迭代器
    110         Iterator<Result> iterator = scanner.iterator();
    111 
    112         while(iterator.hasNext())
    113         {
    114             Result result= iterator.next();
    115             Cell columnLatestCell = result.getColumnLatestCell("cf1".getBytes(), "age".getBytes());
    116             //获取列族中列对应的值
    117             byte[] bytes = CellUtil.cloneValue(columnLatestCell);
    118             //获取row_key
    119             byte[] bytes1 = CellUtil.cloneRow(columnLatestCell);
    120             System.out.println(new String(bytes)+" "+new String(bytes1));
    121         }
    122     }
    123 }
  • 相关阅读:
    更改Linux时区的两种方法
    Azure上部署FTP服务
    log4j2简单使用
    elasticsearch5.5.2环境搭建
    springTask和Schedule学习
    防止SpringMVC拦截器拦截js等静态资源文件
    Mysql中使用FIND_IN_SET解决IN条件为字符串时只有第一个数据可用的问题
    ServletContext总结
    window.location.href和window.location.replace的区别
    javadoc文档
  • 原文地址:https://www.cnblogs.com/one--way/p/5628541.html
Copyright © 2011-2022 走看看