TDSQL Boundless 数据操作

这篇说说腾讯云HBase的Scan、Get、Mutate及批量操作API,涵盖setCaching、setMaxResultSize、setLimit等Scan参数,以及Get的多种查询方式、Put/Delete/Append/Increment等修改操作与批量处理示例,旨在帮助用户高效控制数据查询与修改。 Scan参数:setCaching、setMaxResultSize、setLimit等用于控制RPC返回行数。

相关产品
TDSQL Boundless 产品页,含版本规格、价格与购买入口。

二、setCaching

·功能:限制单次 RPC 服务端返回的行数。默认无限制。较大的值可加快查询速度,较小的值可减少客户端内存消耗。请根据业务需要调整,以平衡查询速度和内存消耗。

·方法签名:

public Scan setCaching(int caching);

·参数:

caching:单次 RPC 返回的行数。

·示例:

Scan scan = new Scan();scan.setCaching(500); // 每次 RPC 返回最多 500 行

三、setMaxResultSize

·功能:限制单次 RPC 服务端返回数据的最大字节数。默认无限制。使用时请注意,避免一次性返回过多数据导致内存溢出。

·方法签名:

public Scan setMaxResultSize(long maxResultSize);

·参数:

maxResultSize:单次 RPC 返回数据的最大字节数。

·示例:

Scan scan = new Scan();scan.setMaxResultSize(1024 * 1024); // 每次 RPC 返回最多 1MB 数据

四、setAllowPartialResults

·功能:设置是否允许返回部分行数据。默认 false,即每次至少返回一整行数据。使用时请注意,避免一次性返回过多数据导致内存溢出。

·方法签名:

public Scan setAllowPartialResults(final boolean allowPartialResults);

·参数:

allowPartialResults:是否允许返回部分行数据。

·示例:

Scan scan = new Scan();scan.setAllowPartialResults(true); // 允许返回部分行数据

五、withStartRow 和 withStopRow

·功能:指定扫描的起始行和结束行,包括是否包含这些行键。

·方法签名:

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);

六、setLimit

·功能:设置扫描的最大结果数,防止一次性返回大量数据导致内存溢出。

·方法签名:

public Scan setLimit(int limit);

·参数:

limit:最大结果数。

·示例:

Scan scan = new Scan(); scan.setLimit(100); // 最多返回 100 行数据

七、renewLease

·功能:延长 ResultScanner 的有效期限。默认有效期为 60 秒,超时后扫描器自动关闭。

·方法签名:

ResultScanner.renewLease();

·示例:

ResultScanner scanner = hTable.getScanner(scan);Thread.sleep(5 * 1000);scanner.next();Thread.sleep(20 * 1000);scanner.renewLease(); // 延长扫描器有效期

九、checkExistenceOnly

·功能: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);

十六、mutate

Mutate 操作用于对数据进行修改,主要包括以下四种操作:

1. Put:插入或更新数据。

2. Delete:删除数据。

3. Append:追加数据。

4. Increment:递增数据。

十七、Put操作

·功能:插入或更新数据。

·方法签名:

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);

十八、Delete 操作

·功能:删除数据。

·方法签名:

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);

十九、Append 操作

·功能:在现有数据的基础上追加数据。

·方法签名:

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);

20、Increment 操作

·功能:对数值类型的列进行递增操作。

·方法签名:

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);

21、批量操作

批量操作用于批量处理数据,支持对多个行进行 Put、Get、Delete 等操作,或者混合多种操作。批量操作可以减少网络开销,提高数据处理的效率。

22、批量 Put 操作

·功能:批量插入或更新数据。

·方法签名:

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);

23、批量 Get 操作

·功能:批量获取数据。

·方法签名:

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);

24、批量 Delete 操作

·功能:批量删除数据。

·方法签名:

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);

25、混合批量操作

·功能:支持在一个批量操作中混合 Put、Get、Delete、Increment 等多种操作。

·方法签名:

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);

赞 (0)
上一篇 4天前
下一篇 4天前