azure011328

导航

 

(1)createTable(String tableName, String[] fields)

       创建表,参数tableName为表的名称,字符串数组fields为存储记录各个字段名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。

 

终端查看创建的表,已经存在,虽然没有插入数据

 

  1. public static void createTable(String tableName, String[] fields) throws IOException {  
  2.       init();  
  3.       TableName tablename = TableName.valueOf(tableName);  
  4.       if (admin.tableExists(tablename)) {  
  5.           System.out.println("table is exists!");  
  6.           admin.disableTable(tablename);  
  7.           admin.deleteTable(tablename);  
  8.       }  
  9.       HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);  
  10.       for (String str : fields) {  
  11.           HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);  
  12.           hTableDescriptor.addFamily(hColumnDescriptor);  
  13.       }  
  14.       admin.createTable(hTableDescriptor);  
  15.       close();  
  16.   }  

 

       (2)addRecord(String tableName, String row, String[] fields, String[] values)

       向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对应的数据values。其中,fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,字符串数组fields为{“Score:Math”, ”Score:Computer Science”, ”Score:English”},数组values存储这三门课的成绩。

 

终端查看已经添加的数据

 

  1. public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {  
  2.        init();  
  3.        Table table = connection.getTable(TableName.valueOf(tableName));  
  4.        for (int i = 0; i != fields.length; i++) {  
  5.            Put put = new Put(row.getBytes());  
  6.            String[] cols = fields[i].split(":");  
  7.            put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());  
  8.            table.put(put);  
  9.        }  
  10.        table.close();  
  11.        close();  
  12.    }  

 

       (3)scanColumn(String tableName, String column)

       浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。

 

源代码

  1. public static void scanColumn(String tableName, String column) throws IOException {  
  2.         init();  
  3.         Table table = connection.getTable(TableName.valueOf(tableName));  
  4.         Scan scan = new Scan();  
  5.         scan.addFamily(Bytes.toBytes(column));  
  6.         ResultScanner scanner = table.getScanner(scan);  
  7.         for (Result result = scanner.next(); result != null; result = scanner.next()) {  
  8.             showCell(result);  
  9.         }  
  10.         table.close();  
  11.         close();  
  12.     }  
  13.   
  14.     public static void showCell(Result result) {  
  15.         Cell[] cells = result.rawCells();  
  16.         for (Cell cell : cells) {  
  17.             System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");  
  18.             System.out.println("Timetamp:" + cell.getTimestamp() + " ");  
  19.             System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");  
  20.             System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");  
  21.             System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");  
  22.         }  
  23.     } 

 

       (4)modifyData(String tableName, String row, String column)

       修改表tableName,行row(可以用学生姓名S_Name表示),列column指定的单元格的数据。

 

终端查看

 

  1. public static void modifyData(String tableName, String row, String column, String val) throws IOException {  
  2.       init();  
  3.       Table table = connection.getTable(TableName.valueOf(tableName));  
  4.       Put put = new Put(row.getBytes());  
  5.       Scan scan = new Scan();  
  6.       ResultScanner resultScanner = table.getScanner(scan);  
  7.       for (Result r : resultScanner) {  
  8.           for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) {  
  9.               ts = cell.getTimestamp();  
  10.           }  
  11.       }  
  12.       put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());  
  13.       table.put(put);  
  14.       table.close();  
  15.       close();  
  16.   }  

 

(5)deleteRow(String tableName, String row)

       删除表tableName中row指定的行的记录。

 

可见记录被删除

 

 

源代码

  1.  public static void modifyData(String tableName, String row, String column, String val) throws IOException {  
  2.         init();  
  3.         Table table = connection.getTable(TableName.valueOf(tableName));  
  4.         Put put = new Put(row.getBytes());  
  5.         Scan scan = new Scan();  
  6.         ResultScanner resultScanner = table.getScanner(scan);  
  7.         for (Result r : resultScanner) {  
  8.             for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) {  
  9.                 ts = cell.getTimestamp();  
  10.             }  
  11.         }  
  12.         put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());  
  13.         table.put(put);  
  14.         table.close();  
  15.         close();  
  16.     }  
posted on 2024-12-13 10:55  淮竹i  阅读(9)  评论(0)    收藏  举报