产品概述
应用场景
产品架构
实例类型
兼容性说明
使用规范建议
public Scan setCaching(int caching);
caching:单次 RPC 返回的行数。Scan scan = new Scan();scan.setCaching(500); // 每次 RPC 返回最多 500 行
public Scan setMaxResultSize(long maxResultSize);
maxResultSize:单次 RPC 返回数据的最大字节数。Scan scan = new Scan();scan.setMaxResultSize(1024 * 1024); // 每次 RPC 返回最多 1MB 数据
public Scan setAllowPartialResults(final boolean allowPartialResults);
allowPartialResults:是否允许返回部分行数据。Scan scan = new Scan();scan.setAllowPartialResults(true); // 允许返回部分行数据
public Scan withStartRow(byte[] startRow);public Scan withStopRow(byte[] stopRow);
startRow:起始行键。stopRow:结束行键。Scan scan = new Scan();byte[] startRow = Bytes.toBytes("row1");byte[] stopRow = Bytes.toBytes("row5");scan.withStartRow(startRow);scan.withStopRow(stopRow);
public Scan setLimit(int limit);
limit:最大结果数。Scan scan = new Scan();scan.setLimit(100); // 最多返回 100 行数据
ResultScanner.renewLease();
ResultScanner scanner = hTable.getScanner(scan);Thread.sleep(5 * 1000);scanner.next();Thread.sleep(20 * 1000);scanner.renewLease(); // 延长扫描器有效期
checkExistenceOnly 用于设置 Get 操作是否只返回数据是否存在的结果(通过 Result 的 getExists() 方法获取),而不是返回完整的结果集。这可以用于快速检查某行或某个单元格是否存在,减少网络传输和内存消耗。public Get setCheckExistenceOnly(boolean checkExistenceOnly);
checkExistenceOnly:如果为 true,则 Get 操作只返回是否存在的结果;如果为 false,则返回完整的结果集。// 创建 Get 对象Get get = new Get(Bytes.toBytes("row1"));// 设置只检查是否存在get.setCheckExistenceOnly(true);// 执行 Get 操作Result result = table.get(get);// 检查数据是否存在boolean exists = result.getExists();System.out.println("数据是否存在: " + exists);
public Result get(Get get);
get:Get 对象,指定要获取的行键。// 获取整行数据Get get = new Get(Bytes.toBytes("row1"));Result result = table.get(get);
public Get addFamily(byte[] family);
family:列族名称。Get getFamily = new Get(Bytes.toBytes("row1"));getFamily.addFamily(Bytes.toBytes("cf1"));Result familyResult = table.get(getFamily);
public Get addColumn(byte[] family, byte[] qualifier);
family:列族名称。qualifier:列名。Get getColumn = new Get(Bytes.toBytes("row1"));getColumn.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col1"));Result columnResult = table.get(getColumn);
public Get addColumn(byte[] family, byte[] qualifier);
family:列族名称。qualifier:列名。Get getMultiple = new Get(Bytes.toBytes("row1"));getMultiple.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col1"));getMultiple.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col2"));getMultiple.addColumn(Bytes.toBytes("cf2"), Bytes.toBytes("col3"));Result multipleResult = table.get(getMultiple);
public Get setMaxVersions(int maxVersions);
maxVersions:要获取的最大版本数。Get getVersion = new Get(Bytes.toBytes("row1"));getVersion.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col1"));getVersion.setMaxVersions(3); // 获取最多3个版本Result versionResult = table.get(getVersion);
public Get setTimeRange(long minStamp, long maxStamp);
minStamp:时间范围的最小时间戳。maxStamp:时间范围的最大时间戳。Get getTimeRange = new Get(Bytes.toBytes("row1"));getTimeRange.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col1"));getTimeRange.setTimeRange(System.currentTimeMillis() - 3600000, System.currentTimeMillis());Result timeRangeResult = table.get(getTimeRange);
public Put addColumn(byte[] family, byte[] qualifier, byte[] value);public Put addColumn(byte[] family, byte[] qualifier, long timestamp, byte[] value);
family:列族名称。qualifier:列名。value:列值。timestamp(可选):时间戳。// 1. 基础 Put 操作Put put = new Put(Bytes.toBytes("row1"));put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col1"), Bytes.toBytes("value1"));put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col2"), Bytes.toBytes("value2"));table.put(put);// 2. 带时间戳的 PutPut putWithTimestamp = new Put(Bytes.toBytes("row1"));long timestamp = System.currentTimeMillis();putWithTimestamp.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col1"), timestamp, Bytes.toBytes("value1"));table.put(putWithTimestamp);
public Delete addColumn(byte[] family, byte[] qualifier);public Delete addColumn(byte[] family, byte[] qualifier, long timestamp);public Delete addColumns(byte[] family, byte[] qualifier);public Delete addFamily(byte[] family);
family:列族名称。qualifier:列名。timestamp(可选):时间戳。// 1. 删除整行Delete deleteRow = new Delete(Bytes.toBytes("row1"));table.delete(deleteRow);// 2. 删除指定列族Delete deleteFamily = new Delete(Bytes.toBytes("row1"));deleteFamily.addFamily(Bytes.toBytes("cf1"));table.delete(deleteFamily);// 3. 删除指定列Delete deleteColumn = new Delete(Bytes.toBytes("row1"));deleteColumn.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col1"));table.delete(deleteColumn);// 4. 删除指定版本Delete deleteVersion = new Delete(Bytes.toBytes("row1"));deleteVersion.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col1"), timestamp);table.delete(deleteVersion);// 5. 删除列的所有版本Delete deleteAllVersions = new Delete(Bytes.toBytes("row1"));deleteAllVersions.addColumns(Bytes.toBytes("cf1"), Bytes.toBytes("col1"));table.delete(deleteAllVersions);
public Append add(byte[] family, byte[] qualifier, byte[] value);
family:列族名称。qualifier:列名。value:要追加的值。// 1. 基础 Append 操作Append append = new Append(Bytes.toBytes("row1"));append.add(Bytes.toBytes("cf1"), Bytes.toBytes("col1"), Bytes.toBytes("_appended"));Result appendResult = table.append(append);// 2. 多个列的 AppendAppend appendMultiple = new Append(Bytes.toBytes("row1"));appendMultiple.add(Bytes.toBytes("cf1"), Bytes.toBytes("col1"), Bytes.toBytes("_suffix1"));appendMultiple.add(Bytes.toBytes("cf1"), Bytes.toBytes("col2"), Bytes.toBytes("_suffix2"));Result appendMultipleResult = table.append(appendMultiple);
public Increment addColumn(byte[] family, byte[] qualifier, long amount);
family:列族名称。qualifier:列名。amount:递增的数值。// 1. 基础 Increment 操作Increment increment = new Increment(Bytes.toBytes("row1"));increment.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("counter"), 1L);Result incrementResult = table.increment(increment);
public void put(List<Put> puts);
puts:包含多个 Put 对象的列表。List<Put> puts = new ArrayList<>();for (int i = 0; i < 100; i++) {Put put = new Put(Bytes.toBytes("row" + i));put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col1"), Bytes.toBytes("value" + i));puts.add(put);}table.put(puts);
public Result[] get(List<Get> gets);
gets:包含多个 Get 对象的列表。List<Get> gets = new ArrayList<>();for (int i = 0; i < 100; i++) {Get get = new Get(Bytes.toBytes("row" + i));gets.add(get);}Result[] results = table.get(gets);
public void delete(List<Delete> deletes);
deletes:包含多个 Delete 对象的列表。List<Delete> deletes = new ArrayList<>();for (int i = 0; i < 100; i++) {Delete delete = new Delete(Bytes.toBytes("row" + i));deletes.add(delete);}table.delete(deletes);
public Object[] batch(List<? extends Row> actions, Object[] results);
actions:包含多个 Row 对象(如 Put、Get、Delete、Increment)的列表。results:用于存储操作结果的数组。List<Row> actions = new ArrayList<>();actions.add(new Put(Bytes.toBytes("row1")).addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("col1"), Bytes.toBytes("value1")));actions.add(new Get(Bytes.toBytes("row2")));actions.add(new Delete(Bytes.toBytes("row3")));actions.add(new Increment(Bytes.toBytes("row4")).addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("counter"), 1L));Object[] results = new Object[actions.size()];table.batch(actions, results);
文档反馈