Java_Habse_shell

  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.util.Bytes;
  5 
  6 import java.io.IOException;
  7 public class HBaseOperation{
  8     public static Configuration configuration;
  9     public static Connection connection;
 10     public static Admin admin;
 11     public static long ts;
 12     public static void main(String[] args)throws IOException{
 13         //createTable("Student",new String[]{"Score"});
 14         //createTable("SC",new String[]{"Score"});
 15         //createTable("Course",new String[]{"Score"});
 16         /*
 17          String[] fields = {"Score:Math", "Score:Computer Science", "Score:English"};
 18          String[] values = {"99", "80", "100"};
 19          try {
 20              addRecord("Student", "Score", fields, values);
 21         }
 22          catch (IOException e) {
 23              e.printStackTrace();
 24          }
 25          */
 26         
 27          //scanColumn("Student", "Score");
 28         
 29         
 30 //         try {
 31 //             modifyData("Student", "Score", "Math", "100");
 32 //        } catch (IOException e) {
 33 //            e.printStackTrace();
 34 //        }
 35     
 36         
 37          try {
 38              deleteRow("Student", "Score");
 39          } catch (IOException e) {
 40              e.printStackTrace();
 41          }
 42     }
 43     //建立连接
 44     public static void init(){
 45         configuration  = HBaseConfiguration.create();
 46         configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
 47         try{
 48             connection = ConnectionFactory.createConnection(configuration);
 49             admin = connection.getAdmin();
 50         }catch (IOException e){
 51             e.printStackTrace();
 52         }
 53     }
 54     //关闭连接
 55     public static void close(){
 56         try{
 57             if(admin != null){
 58                 admin.close();
 59             }
 60             if(null != connection){
 61                 connection.close();
 62             }
 63         }catch (IOException e){
 64             e.printStackTrace();
 65         }
 66     }
 67     //建表
 68     public static void createTable(String myTableName,String[] colFamily) throws IOException {
 69         init();
 70         TableName tableName = TableName.valueOf(myTableName);
 71         if(admin.tableExists(tableName)){
 72             System.out.println("talbe is exists!");
 73         }else {
 74             HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
 75             for(String str:colFamily){
 76                 HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
 77                 hTableDescriptor.addFamily(hColumnDescriptor);
 78             }
 79             admin.createTable(hTableDescriptor);
 80             System.out.println(myTableName+"表建立成功--李运辰");
 81         }
 82         close();
 83     }
 84     //删表
 85     public static void deleteTable(String tableName) throws IOException {
 86         init();
 87         TableName tn = TableName.valueOf(tableName);
 88         if (admin.tableExists(tn)) {
 89             admin.disableTable(tn);
 90             admin.deleteTable(tn);
 91         }
 92         close();
 93     }
 94     //添加
 95     public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
 96         init();
 97         Table table = connection.getTable(TableName.valueOf(tableName));
 98         for (int i = 0; i != fields.length; i++) {
 99             Put put = new Put(row.getBytes());
100             String[] cols = fields[i].split(":");
101             put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes()); table.put(put);
102         }
103         table.close(); close();
104         System.out.println("添加成功--李运辰");
105     }
106 
107     //删除数据
108     public static void deleteRow(String tableName, String row) throws IOException {
109         init();
110         Table table = connection.getTable(TableName.valueOf(tableName));
111         Delete delete=new Delete(row.getBytes());
112         table.delete(delete);
113         table.close(); close();
114         System.out.println("删除成功--李运辰");
115         
116     }
117  
118 
119     public static void modifyData(String tableName, String row, String column, String val) throws IOException {
120         init();
121         Table table = connection.getTable(TableName.valueOf(tableName));
122         Put put = new Put(row.getBytes());
123         Scan scan = new Scan();
124         ResultScanner resultScanner = table.getScanner(scan);
125         for (Result r : resultScanner) {
126             for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) {
127                 ts = cell.getTimestamp();
128             }
129         }
130         put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());
131         table.put(put);
132         table.close();
133         close();
134         System.out.println("更新成功--李运辰");
135     }
136     
137     public static void scanColumn(String tableName, String column) throws IOException {
138         init();
139         Table table = connection.getTable(TableName.valueOf(tableName));
140         Scan scan = new Scan();
141         scan.addFamily(Bytes.toBytes(column));
142         ResultScanner scanner = table.getScanner(scan);
143         for (Result result = scanner.next();result != null;result = scanner.next()) {
144                    showCell(result);
145         }
146         table.close();
147         close();
148     }
149     //格式化输出
150     public static void showCell(Result result){
151         Cell[] cells = result.rawCells();
152         for(Cell cell:cells){
153             System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
154             System.out.println("Timetamp:"+cell.getTimestamp()+" ");
155             System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
156             System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
157             System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
158         }
159     }
160 }

 

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