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

Java

        

Java/Hbase - Composite Key

When the HBase table has a composite rowkey the method get() of Java is slightly different respect to the single key.
First of all, this is an example of the Hbase table:
The rowkey is composed by a Byte element plus a Byte array (byte[]). For example:

ItemType Byte[]
Item Byte[]

Divide the code into:
- Method to generate the concatenated values for the key
- Mainly method to get the values from HBase
- Code calling the mainly method and get the return value HashMap

This is the complete code.
You will find the comments inside it.

public byte[] firstValue;
public byte[] lastValue;

// Create a method to generate the concatenated values for the key
public static byte[] getRowKeyFrom(byte[] ItemType, byte[] Item)
{
	return Bytes.add(ItemType, Item);
}

// Mainly function to get the values from HBase
public HashMap GetCounters(byte[] firstValue, byte[] lastValue) throws IOException
{
	// HashMap to store the key/value (String/Counters)
	HashMap listCounters = new HashMap();
	
	try
	{
		// Call the get with the two values of the key
		Get nameGet = new Get(Tables.NameTableHBase.getRowKeyFrom(firstValue, lastValue));

		// Add all the columns of the Default family 
		nameGet.addColumn(Tables.NameTableHBase.Families.Default.FamilyName, Tables.NameTableHBase.Families.Default.COUNTER_ONE);
		nameGet.addColumn(Tables.NameTableHBase.Families.Default.FamilyName, Tables.NameTableHBase.Families.Default.COUNTER_TWO);			
		nameGet.addColumn(Tables.NameTableHBase.Families.Default.FamilyName, Tables.NameTableHBase.Families.Default.COUNTER_THREE);
		nameGet.addColumn(Tables.NameTableHBase.Families.Default.FamilyName, Tables.NameTableHBase.Families.Default.COUNTER_FOUR);
		
		Result result = NameTableHBase.get(vectorGet);
		
		if (!result.isEmpty()){
			// For every columns we check if the value exist and then put into the HashMap
			
			byte[] aux;
					
			aux = result.getValue(Tables.NameTableHBase.Families.Default.FamilyName,Tables.NameTableHBase.Families.Default.COUNTER_ONE);
				if(aux != null)			
					listCounters.put("COUNTER_ONE", Bytes.toInt(aux));

			aux = result.getValue(Tables.NameTableHBase.Families.Default.FamilyName,Tables.NameTableHBase.Families.Default.COUNTER_TWO);
				if(aux != null)			
					listCounters.put("COUNTER_TWO", Bytes.toInt(aux));

			aux = result.getValue(Tables.NameTableHBase.Families.Default.FamilyName,Tables.NameTableHBase.Families.Default.COUNTER_THREE);
				if(aux != null)			
					listCounters.put("COUNTER_THREE", Bytes.toInt(aux));

			aux = result.getValue(Tables.NameTableHBase.Families.Default.FamilyName,Tables.NameTableHBase.Families.Default.COUNTER_FOUR);
				if(aux != null)			
					listCounters.put("COUNTER_FOUR", Bytes.toInt(aux));			
		}
	}catch (Exception e){
		logger.error(String.format("Generic Error %s.", value),e);
	}

	return listCounters;		
	}
}

// Call the mainly method
...
HashMap resultCounters = null;
resultCounters = hbaseConfigData.GetCounters(firstValue, lastValue);

// Instead of to read the HashMap using a loop
if (resultCounters != null){
	if(resultCounters.containsKey("COUNTER_ONE")){
		this.counterOne = this.counterOne+ resultCounters.get("COUNTER_ONE");
	}
	
	if(resultCounters.containsKey("COUNTER_TWO")){
		this.counterTwo = this.counterTwo+ resultCounters.get("COUNTER_TWO");
	}

	if(resultCounters.containsKey("COUNTER_THREE")){
		this.counterThree = this.counterThree+ resultCounters.get("COUNTER_THREE");
	}

	if(resultCounters.containsKey("COUNTER_FOUR")){
		this.counterFour = this.counterFour+ resultCounters.get("COUNTER_FOUR");
	}	
}

Note: The code of loop into the HashMap should be:

for (Entry entry : resultCounters.entrySet()) {

	switch (entry.getKey()){
		case "COUNTER_ONE":
			this.counterOne = entry.getValue();
		break;
		
		case "COUNTER_TWO":
			this.counterTwo = entry.getValue();
		break;
		
		case "COUNTER_THREE":
			this.counterThree = entry.getValue();
		break;
		
		case "COUNTER_FOUR":
			this.counterFour = entry.getValue();
		break;		
	}
}