paolo@bimodesign.com | +34 608 61 64 10

NoSQL

        

HBase - scan options

Scan is the second way to get data from HBase tables and in this post I'll show a simple example using java, showing some options as Start and Stop row and Batching.
You will find:

- SetMaxVersions(1): To limit the number, in this case "1", of versions of each column to be returned.
- setStartRow: Set the start row of the scan.
- setStopRow: Set the stop row of the scan.
- setBatch: Set the maximum number of values to return for each call to next()
- delete: If "raw" is enabled the scan will return all delete marker and deleted rows that have not been collected, yet

Note: It's so important to close the scan.

public void SomeFunctions(byte[] someRowKey) throws Exception
{
	Scan scan = new Scan();
	scan.setMaxVersions(1);
	scan.setStartRow(Bytes.toBytes(someRowKey));	
	
	if(recursive)
		scan.setStopRow(Bytes.toBytes(CalculateStopRowKey(someRowKey)));
	else
		scan.setStopRow(Bytes.toBytes(node.nodeKey));

	scan.addFamily(HBaseDefs.SomeHBaseTable.ColumnFamilyFromEnum(family));
	scan.setBatch(100);
  
	ResultScanner scanner = SomeHBaseTable.getScanner(scan);
	try
	{
		for (Result res : scanner)
		{
			for(KeyValue kv : res.raw())
			{
				SomeObject vc = new someObject(family, kv.getQualifier(), kv.getValue());
     
				if(!vc.deleted)
				{
					// in case the data is marked how deleted
					return;
				}
			}
		}
	}
	finally
	{
		scanner.close();
	}
}

For more details about scanning in HBase, visit this link