javahadoopspring-datahbasespring-data-hadoop

How to "findAllBy{Column}" with HbaseTemplate?


I have been developing my first RESTful server utilizing such technologies as Spring and HBase.

Message below is the core model of my server;

@AllArgsConstructor
@Getter
public class Message {

    private final String from;

    private final String to;

    private final long when;

}

My server should save and find messages into HBase.

I have already made research and read this documentation: https://docs.spring.io/spring-hadoop/docs/2.5.0.RELEASE/reference/html/springandhadoop-hbase.html#data-access-object-dao-support

However, it's not enough. The tutorial doesn't expose how to find by a specific column.

Could you provide a code snippet that retrieves a message from HBase based on the following query?

SELECT from, to, when FROM Message WHERE to = {userId} OR from = {userId}

Solution

  • There is an example to make a column based query as given below:

    Scan scan = new Scan();
    scan.addColumn(HbaseJsonEventSerializer.COLUMFAMILY_FILTERS_BYTES,
                    Bytes.toBytes(columnName));
            Filter filter = new SingleColumnValueFilter(HbaseJsonEventSerializer.COLUMFAMILY_FILTERS_BYTES,
                    Bytes.toBytes(columnName), CompareOp.EQUAL, Bytes.toBytes(columnValue));
            scan.setFilter(filter);
    
            List<String> rows  =
            hbaseTemplate.find("searchclicks", scan, new RowMapper<String>() {
                @Override
                public String mapRow(Result result, int rowNum) throws Exception {
                    byte[] value = result.getValue(
                            HbaseJsonEventSerializer.COLUMFAMILY_FILTERS_BYTES,
                            Bytes.toBytes(columnName));
                    if (value != null) {
                        String facetValue = new String(value);
                        LOG.debug("Facet field: {} and Facet Value: {}",
                                new Object[] { columnName, facetValue });
                    }
                    return facetValue;
                }
            });
    

    You can refer this for more examples:
    https://www.programcreek.com/java-api-examples/?code=jaibeermalik/searchanalytics-bigdata/searchanalytics-bigdata-master/src/main/java/org/jai/hbase/HbaseServiceImpl.java