*I have modified the spring class HBaseConfigurationFactoryBean in a way
that will allow developers to set any property of the HBaseConfiguration for
the client.*

The updated configuration will look like:

<bean id="hbaseConfiguration"
class="com.gumgum.hbase.HBaseConfigurationFactoryBean">
        <property name="hbaseProperties">
        <props>
           <!-- hbase.zookeeper.quorum is required. Other properties are
optional-->
            <prop
key="hbase.zookeeper.quorum">${hbase.zookeeper.quorum}</prop>
        </props>
        </property>
    </bean>

I have updated the original spring issue with the new class. Here is the url
of the issue:
http://jira.springframework.org/browse/SPR-5950

I am also attaching the three classes with this email: HbaseCallback.java,
HBaseConfigurationFactoryBean.java and HBaseTemplate.java

Here is the remaining configuration you will need to do:
<bean id="hbaseTemplate" class="com.gumgum.hbase.HBaseTemplate">
        <constructor-arg ref="hTablePool"/>
</bean>

<bean id="hTablePool" class="org.apache.hadoop.hbase.client.HTablePool">
        <constructor-arg ref="hbaseConfiguration"/>
        <constructor-arg value="${hbase.htable.pool.size}"/>
</bean>


Regards,
Vaibhav Puranik
Gumgum
package org.springframework.hbase;

import org.apache.hadoop.hbase.client.HTable;

/**
 * Defines methods to get HTable and close HTable. Modeled after DataSource interface in JDBC.
 *
 * @author Vaibhav Puranik
 * @version $Id: HBaseCallback.java 3429 2009-07-21 02:10:28Z vaibhav $
 */
public interface HBaseCallback {

    /**
     * Gets called by HbaseTemplate.execute with an active HTable for the given tablename.
     * @param htable htable instance for the given tablename.
     * @return a result object, or null if none
     * @throws Exception
     */
    Object doInHbase(HTable htable) throws Exception;
}
package com.gumgum.hbase;

import java.util.Properties;
import java.util.Set;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.beans.factory.FactoryBean;

/**
 * Creates HBase Configuration for the given HBase master.
 * @author Vaibhav Puranik
 * @version $Id: HBaseConfigurationFactoryBean.java 3458 2009-07-24 21:29:59Z vaibhav $
 */
public class HBaseConfigurationFactoryBean implements FactoryBean {

    /**
     * You can set various hbase client properties.
     * hbase.zookeeper.quorum must be set.
     */
    private Properties hbaseProperties;

    @Override
    public Object getObject() throws Exception {
        if (hbaseProperties != null) {
            HBaseConfiguration config = new HBaseConfiguration();
            Set<String> propertyNames = hbaseProperties.stringPropertyNames();
            for (String propertyName : propertyNames) {
                config.set(propertyName, hbaseProperties.getProperty(propertyName));
            }
            return config;
        } else {
            throw new RuntimeException("hbase properties cannot be null");
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public Class getObjectType() {
        return HBaseConfiguration.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public Properties getHbaseProperties() {
        return hbaseProperties;
    }

    public void setHbaseProperties(Properties hbaseProperties) {
        this.hbaseProperties = hbaseProperties;
    }
}

package org.springframework.hbase;

import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Helper class that simplifies Hbase data access code. This class will call HTablePool's getTable and
 * closeHtable methods before and after your action.
 *
 * @author Vaibhav Puranik
 * @version $Id: HBaseTemplate.java 3429 2009-07-21 02:10:28Z vaibhav $
 */
public class HBaseTemplate {

    private static final Logger LOGGER = LoggerFactory.getLogger(HBaseTemplate.class);
    private HTablePool hTablePool;

    public HBaseTemplate(HTablePool hTablePool) {
        this.hTablePool = hTablePool;
    }

    public Object execute(String tablename, HBaseCallback action) {
        Object result = null;
        try {
            HTable hTable = hTablePool.getTable(tablename);
            result = action.doInHbase(hTable);
            hTablePool.putTable(hTable);
        } catch (Exception e) {
            LOGGER.error("Exception occured in execute method: ", e);
        }
        return result;
    }

    public HTablePool getHTablePool() {
        return hTablePool;
    }

    public void setHTablePool(HTablePool tablePool) {
        hTablePool = tablePool;
    }


}

Reply via email to