Java_Habse_add

  1 import java.io.IOException;
  2 import org.apache.hadoop.conf.Configuration;
  3 import org.apache.hadoop.hbase.HBaseConfiguration;
  4 import org.apache.hadoop.hbase.TableName;
  5 import org.apache.hadoop.hbase.client.Admin;
  6 import org.apache.hadoop.hbase.client.Connection;
  7 import org.apache.hadoop.hbase.client.ConnectionFactory;
  8 import org.apache.hadoop.hbase.client.Put;
  9 import org.apache.hadoop.hbase.Cell;
 10 import org.apache.hadoop.hbase.CellUtil;
 11 import org.apache.hadoop.hbase.client.Get;
 12 import org.apache.hadoop.hbase.client.Result;
 13 import org.apache.hadoop.hbase.client.Table;
 14 
 15 
 16 public class HBaseTest2 {
 17     public static Configuration configuration;
 18     public static Connection connection;
 19     public static Admin admin;
 20 
 21     public static void main(String[] args) {
 22         configuration=HBaseConfiguration.create();
 23         configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
 24         
 25         /*
 26         try {
 27             connection=ConnectionFactory.createConnection(configuration);
 28             admin=connection.getAdmin();
 29             insertRow("Student","scofield","score","English","45");
 30             insertRow("Student","scofield","score","Math","89");
 31             insertRow("Student","scofield","score","Computer","100");
 32             System.out.println("插入成功!---李运辰");
 33         } catch (IOException e) {
 34             e.printStackTrace();
 35         }
 36       */
 37         configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
 38         try {
 39             connection=ConnectionFactory.createConnection(configuration);
 40             admin=connection.getAdmin();
 41             getData("Student","scofield","score","English");
 42             System.out.println("输出完成!---李运辰");
 43         } catch (IOException e) {
 44             e.printStackTrace();
 45         }
 46         
 47         
 48         
 49         
 50         close();
 51     }
 52 
 53     public static void insertRow(String tableName, String rowKey, String colFamily, String col, String val) {
 54         try {
 55             Table table = connection.getTable(TableName.valueOf(tableName));
 56             Put put = new Put(rowKey.getBytes());
 57             put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
 58             table.put(put);
 59             table.close();
 60         } catch (IOException e) {
 61             // TODO Auto-generated catch block
 62             e.printStackTrace();
 63         }
 64 
 65     }
 66 
 67 
 68     public static void getData(String tableName, String rowKey, String colFamily, String col) {
 69         try {
 70             Table table = connection.getTable(TableName.valueOf(tableName));
 71             Get get=new Get(rowKey.getBytes());
 72             get.addColumn(colFamily.getBytes(), col.getBytes());
 73             Result result=table.get(get);
 74             showCell(result);
 75             table.close();
 76         } catch (IOException e) {
 77             // TODO Auto-generated catch block
 78             e.printStackTrace();
 79         }
 80 
 81     }
 82 
 83     private static void showCell(Result result) {
 84         Cell[] cells=result.rawCells();
 85         for(Cell cell:cells) {
 86             System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+"     ");
 87             System.out.println("Timetamp:"+cell.getTimestamp()+"     ");
 88             System.out.println("column Family"+new String(CellUtil.cloneFamily(cell))+"     ");
 89             System.out.println("row Name:"+new String(CellUtil.cloneValue(cell))+"     ");
 90             System.out.println("value"+new String(CellUtil.cloneValue(cell))+"     ");
 91             
 92         }
 93         
 94     }
 95     public static void close() {
 96 
 97         try {
 98             if (admin != null) {
 99                 admin.close();
100             }
101             if(null!=connection) {
102                 connection.close();
103             }    
104         } catch (IOException e) {
105             // TODO Auto-generated catch block
106             e.printStackTrace();
107         }
108 
109     }
110 
111 }

 

posted @ 2019-06-18 11:19  Python研究者  阅读(155)  评论(0编辑  收藏  举报