HBase表基本操作,读取和写入

  1 package com.beifeng.hbase;
  2 /**
  3  * Hbase基本操作测试,往表写入数据,并读取数据
  4  */
  5 import java.io.IOException;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 
  9 import org.apache.hadoop.conf.Configuration;
 10 import org.apache.hadoop.hbase.HBaseConfiguration;
 11 import org.apache.hadoop.hbase.TableName;
 12 import org.apache.hadoop.hbase.client.Connection;
 13 import org.apache.hadoop.hbase.client.ConnectionFactory;
 14 import org.apache.hadoop.hbase.client.Get;
 15 import org.apache.hadoop.hbase.client.Put;
 16 import org.apache.hadoop.hbase.client.Result;
 17 import org.apache.hadoop.hbase.client.Table;
 18 import org.apache.hadoop.hbase.util.Bytes;
 19 
 20 public class TestHbase {
 21 
 22     public static void main(String[] args) throws IOException {
 23         //insert data
 24         //insertData("user");//success
 25 
 26         //read data
 27         readDate("user","1006","info","name");//success
 28     }
 29 
 30     public static void readDate(String tbName,String rowKey,String columnFamily,String qlif) throws IOException{
 31         Configuration configuration = HBaseConfiguration.create();
 32         configuration.set("hbase.zookeeper.quorum", "hadoopMaster,hdp102,hdp103");
 33         Connection connection = ConnectionFactory.createConnection(configuration);
 34         //tablename
 35         String tableName = tbName;
 36         //rowkey
 37         String rowkey  = rowKey;
 38         
 39         //columnfamily
 40         String columnName = columnFamily;
 41 
 42         //qulifier
 43         String qualifier = qlif;
 44         
 45         //connection table
 46         Table table  = connection.getTable(TableName.valueOf(tableName));
 47         Get get = new Get(rowkey.getBytes());
 48         get.addColumn(columnName.getBytes(), qualifier.getBytes());
 49         Result result = table.get(get);
 50         //get the result
 51         String valueStr = Bytes.toString(result.getValue(columnName.getBytes(), qualifier.getBytes()));
 52         
 53         System.out.println(valueStr);
 54         
 55         table.close();
 56         connection.close();
 57     }
 58     
 59     public static void insertData(String tableName){
 60         try {
 61             Configuration configuration = HBaseConfiguration.create();
 62             configuration.set("hbase.zookeeper.quorum", "hadoopMaster,hdp102,hdp103");
 63             Connection connection = ConnectionFactory.createConnection(configuration);
 64             Table table = connection.getTable(TableName.valueOf(tableName));
 65             String columnFamily = "info";
 66             
 67             Put put = new Put("1007".getBytes());
 68             
 69             put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("id"), Bytes.toBytes("1"));
 70             put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
 71             put.addImmutable(Bytes.toBytes(columnFamily), Bytes.toBytes("age"), Bytes.toBytes(27));
 72             put.addImmutable(Bytes.toBytes(columnFamily), Bytes.toBytes("phone"), Bytes.toBytes("021-58865997"));
 73             put.addImmutable(Bytes.toBytes(columnFamily), Bytes.toBytes("email"), Bytes.toBytes("zhangsan@qq.com"));
 74             table.put(put);
 75             
 76             //同时put多个
 77             Put put1003 = new Put("1008".getBytes());
 78             put1003.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("id"), Bytes.toBytes("2"));
 79             put1003.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("name"), Bytes.toBytes("user1"));
 80             
 81             Put put1004 = new Put("1009".getBytes());
 82             put1004.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("id"), Bytes.toBytes("3"));
 83             put1004.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("name"), Bytes.toBytes("user2"));
 84             
 85             Put put1005 = new Put("1010".getBytes());
 86             put1005.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("id"), Bytes.toBytes("4"));
 87             put1005.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes("name"), Bytes.toBytes("user3"));
 88             
 89             List<Put> puts = new ArrayList<Put>();
 90             puts.add(put1003);
 91             puts.add(put1004);
 92             puts.add(put1005);
 93             
 94             table.put(puts);
 95             
 96             table.close();
 97             connection.close();
 98         } catch (Exception e) {
 99             e.printStackTrace();
100         }
101     }
102 
103 }

 

posted @ 2017-07-12 16:04  翱翔的孤鹰  阅读(491)  评论(0编辑  收藏  举报