Nutch2.3(二)
你提供的代码示例展示了如何使用Java API与HBase进行交互,包括创建表、添加数据、查询数据以及删除表等操作。以下是对代码的一些详细解释和改进建议:
1. 创建表
public static void creatTable(String tableName, String[] family) throws IOException { HBaseAdmin admin = new HBaseAdmin(conf); if (!admin.tableExists(tableName)) { HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); for (String columnFamily : family) { tableDescriptor.addFamily(new HColumnDescriptor(columnFamily)); } admin.createTable(tableDescriptor); } } 2. 添加数据
public static void addData(String rowKey, String tableName, String[] column1, String[] value1, String[] column2, String[] value2) throws IOException { HTable table = new HTable(conf, tableName); Put put1 = new Put(Bytes.toBytes(rowKey)); for (int i = 0; i < column1.length; i++) { put1.add(Bytes.toBytes("article"), Bytes.toBytes(column1[i]), Bytes.toBytes(value1[i])); } Put put2 = new Put(Bytes.toBytes(rowKey)); for (int i = 0; i < column2.length; i++) { put2.add(Bytes.toBytes("author"), Bytes.toBytes(column2[i]), Bytes.toBytes(value2[i])); } table.put(put1); table.put(put2); table.close(); } 3. 查询数据
public static void getResult(String tableName, String rowKey) throws IOException { HTable table = new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(rowKey)); Result result = table.get(get); for (Cell cell : result.listCells()) { System.out.println("Family: " + Bytes.toString(CellUtil.cloneFamily(cell)) + ", Column Qualifier: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + ", Value: " + Bytes.toString(CellUtil.cloneValue(cell))); } table.close(); } 4. 遍历查询
public static void getResultScann(String tableName, String startRow, String stopRow) throws IOException { HTable table = new HTable(conf, tableName); Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow)); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { System.out.println("Key: " + Bytes.toString(result.getRow())); for (Cell cell : result.listCells()) { System.out.println("Family: " + Bytes.toString(CellUtil.cloneFamily(cell)) + ", Column Qualifier: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + ", Value: " + Bytes.toString(CellUtil.cloneValue(cell))); } } scanner.close(); table.close(); } 5. 查询某一列的值
public static String getResultByColumn(String tableName, String rowKey, String columnFamily, String columnQualifier) throws IOException { HTable table = new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier)); Result result = table.get(get); byte[] value = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier)); if (value != null) { return Bytes.toString(value); } else { return null; } } 6. 更新列
public static void updateTable(String tableName, String rowKey, String columnFamily, String columnQualifier, String newValue) throws IOException { HTable table = new HTable(conf, tableName); Put put = new Put(Bytes.toBytes(rowKey)); put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier), Bytes.toBytes(newValue)); table.put(put); table.close(); } 7. 查询某列的多版本
public static void getResultByVersion(String tableName, String rowKey, String columnFamily, String columnQualifier) throws IOException { HTable table = new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier)); Result result = table.get(get); List<Cell> cells = result.getColumnCells(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier)); for (Cell cell : cells) { System.out.println("Version: " + cell.getTimestamp() + ", Value: " + Bytes.toString(CellUtil.cloneValue(cell))); } table.close(); } 8. 删除一列
public static void deleteColumn(String tableName, String rowKey, String columnFamily, String columnQualifier) throws IOException { HTable table = new HTable(conf, tableName); Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumns(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier)); table.delete(delete); table.close(); } 9. 删除表
public static void deleteTable(String tableName) throws IOException { HBaseAdmin admin = new HBaseAdmin(conf); if (admin.tableExists(tableName)) { admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); } } 改进建议
- 资源管理:确保在操作完成后关闭表和连接,以避免资源泄漏。
- 异常处理:捕获并处理可能的异常,以便更好地调试和日志记录。
- 配置管理:使用外部配置文件(如
hbase-site.xml)来管理HBase配置,而不是硬编码在代码中。
通过这些改进,你的代码将更加健壮和易于维护。