4
A
回答
0
Yes, this can be done easily by setting time range to scanner and then deleting the returned result set.
public class BulkDeleteDriver {
//Added colum family and column to lessen the scan I/O
private static final byte[] COL_FAM = Bytes.toBytes("<column family>");
private static final byte[] COL = Bytes.toBytes("column");
final byte[] TEST_TABLE = Bytes.toBytes("<TableName>");
public static void main(final String[] args) throws IOException,
InterruptedException {
//Create connection to Hbase
Configuration conf = null;
Connection conn = null;
try {
conf = HBaseConfiguration.create();
//Path to HBase-site.xml
conf.addResource(new Path(hbasepath));
//Get the connection
conn = ConnectionFactory.createConnection(conf);
logger.info("Connection created successfully");
}
catch (Exception e) {
logger.error(e + "Connection Unsuccessful");
}
//Get the table instance
Table table = conn.getTable(TableName.valueOf(TEST_TABLE));
List<Delete> listOfBatchDeletes = new ArrayList<Delete>();
long recordCount = 0;
// Set scanCache if required
logger.info("Got The Table : " + table.getName());
//Get calendar instance and get proper start and end timestamps
Calendar calStart = Calendar.getInstance();
calStart.add(Calendar.DAY_OF_MONTH, day);
Calendar calEnd = Calendar.getInstance();
calEnd.add(Calendar.HOUR, hour);
//Get timestamps
long starTS = calStart.getTimeInMillis();
long endTS = calEnd.getTimeInMillis();
//Set all scan related properties
Scan scan = new Scan();
//Most important part of code set it properly!
//here my purpose it to delete everthing Present Time - 6 hours
scan.setTimeRange(starTS, endTS);
scan.setCaching(scanCache);
scan.addColumn(COL_FAM, COL);
//Scan the table and get the row keys
ResultScanner resultScanner = table.getScanner(scan);
for (Result scanResult : resultScanner) {
Delete delete = new Delete(scanResult.getRow());
//Create batches of Bult Delete
listOfBatchDeletes.add(delete);
recordCount++;
if (listOfBatchDeletes.size() == //give any suitable batch size here) {
System.out.println("Firing Batch Delete Now......");
table.delete(listOfBatchDeletes);
//don't forget to clear the array list
listOfBatchDeletes.clear();
}}
System.out.println("Firing Final Batch of Deletes.....");
table.delete(listOfBatchDeletes);
System.out.println("Total Records Deleted are.... " + recordCount);
try {
table.close();
} catch (Exception e) {
e.printStackTrace();
logger.error("ERROR", e);
}}}
4
HBase沒有範圍刪除標記的概念。這意味着如果您需要刪除多個單元格,則需要爲每個單元格放置刪除標記,這意味着您必須掃描客戶端或服務器端的每一行。這意味着您有兩種選擇:
- BulkDeleteProtocol:這使用協處理器端點,這意味着完整的操作將在服務器端運行。該鏈接有一個如何使用它的例子。如果您進行網絡搜索,您可以輕鬆找到如何在HBase中啓用協處理器端點。
掃描和刪除:這是一個乾淨且最簡單的選項。由於您說您需要刪除比特定時間戳更早的所有列族,因此可以通過使用服務器端篩選僅讀取每行的第一個鍵來大大優化掃描和刪除操作。
Scan scan = new Scan(); scan.setTimeRange(0, STOP_TS); // STOP_TS: The timestamp in question // Crucial optimization: Make sure you process multiple rows together scan.setCaching(1000); // Crucial optimization: Retrieve only row keys FilterList filters = new FilterList(FilterList.Operator.MUST_PASS_ALL, new FirstKeyOnlyFilter(), new KeyOnlyFilter()); scan.setFilter(filters); ResultScanner scanner = table.getScanner(scan); List<Delete> deletes = new ArrayList<>(1000); Result [] rr; do { // We set caching to 1000 above // make full use of it and get next 1000 rows in one go rr = scanner.next(1000); if (rr.length > 0) { for (Result r: rr) { Delete delete = new Delete(r.getRow(), STOP_TS); deletes.add(delete); } table.delete(deletes); deletes.clear(); } } while(rr.length > 0);
相關問題
- 1. Marklogic:刪除數據庫上的所有元素範圍索引?
- 2. 如何刪除solr和hbase中的所有數據
- 3. 手動刪除HBase數據
- 4. 根據分組刪除除列表中的所有對象
- 5. 根據列中的值刪除範圍內的行
- 6. 根據上次在線時間刪除所有行嗎?
- 7. 所有數據有刪除
- 8. 如何根據時間間隔刪除數據
- 9. 從Sugar ORM中刪除所有表中的所有數據
- 10. 根據創建時間刪除文件
- 11. 從數據庫的所有表中刪除所有記錄
- 12. 從數據庫的所有表中刪除所有行
- 13. Apache spark根據範圍SQL組數據
- 14. 如何根據行範圍內滿足的條件刪除列?
- 15. 從SQL Server數據庫中的所有表數據刪除,除了一些表
- 16. 從數據表中刪除所有數據(ASP.NET MVC)
- 17. 刪除datagridview中的所有數據
- 18. 當Hbase表被刪除時,數據會發生什麼變化?
- 19. 在範圍時間插入數據庫
- 20. 將數據傳送到時間範圍
- 21. 根據值範圍搜索並刪除Excel行
- 22. 刪除所有Couchbase數據/文檔ios(或刪除所有ios數據?)
- 23. 從gridview中刪除所有數據
- 24. 從一個範圍中刪除數據,它被反映在另一範圍
- 25. 刪除集[DB]中的所有表數據[審計]。[表]
- 26. 如何根據HQL中的時間戳範圍從數據庫中獲取數據
- 27. t-SQL更新表以刪除重疊的時間範圍
- 28. ggplot:根據日期設置刻面繪圖的時間範圍
- 29. 在另一個時間範圍內返回數據子集時間範圍?
- 30. 刪除CockroachDB中的數據庫中的所有表
表有大約20萬行。獲取每一行將影響性能。如果我們只傳遞表名和時間戳,是否有可能通過這種方式刪除時間戳之前的所有數據,而不必實際傳遞該行? –
時間戳是你的行鍵還是行鍵的一部分? 如果不是那麼你將如何找出刪除哪一行。您必須檢查時間戳是否正確,行鍵的時間戳或任何其他列時間戳。 如果是: 然後只需傳遞rowKey(Timestamp)作爲範圍掃描參數或使用模糊過濾器找出這些鍵。 無論你做什麼,掃描都會發生。 爲了使掃描速度更快,您可以查看CoProcessors,但如果您不是HBase的專家,請不要嘗試它們 –