DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Can You Use HBase as Your Persistent Store?

Can You Use HBase as Your Persistent Store?

Dmitriy Setrakyan user avatar by
Dmitriy Setrakyan
·
Jun. 28, 12 · Interview
Like (0)
Save
Tweet
Share
9.21K Views

Join the DZone community and get the full member experience.

Join For Free

We have added many cool features in GridGain 4.1. One of them is tight integration with Hadoop ecosystem. There are two ways you can integrate with Hadoop. One way is upstream integration in which you efficiently load data from HDFS into In-Memory Cache (aka Data Grid) where it gets indexed for low-latency query access. Another way is downstream integration where HDFS is used as a persistent store and data gets flushed into it periodically from In-Memory Cache using write-behind cache feature.

With downstream integration users are able to hold the latest data in memory and use HDFS as historical data warehouse without any extra ETL process and without lags in data. Business applications can run queries and get instant analytical feedback on the whole data set which includes most recent data held in-memory and HDFS-based warehoused data, hence not loosing any data in query results at all.

Here is an example of how HDFS-based cache store would look like in this case. I show how HBase would be used, but you can write directly to HDFS if you like. Note that all we do here is override methods load(...), put(...), and remove(...) to tell GridGain how to load and update entries in HBase.

Full source of this example can also be found on GitHub GridGain Project:

Cache Store with HBase

public class GridCacheHBasePersonStore 
    extends GridCacheStoreAdapter<Long, Person> {
    // Default config path.
    private static final String CONFIG_PATH "/my/hbase/hbase-site.xml";

    // Table name.
    private static final String TABLE_NAME = "persons";

    // Maximum allowed pool size.
    private static final int MAX_POOL_SIZE = 4;

    // HBase table pool.
    private HTablePool tblPool;

    // HBase column descriptor for first name.
    private HColumnDescriptor first = new HColumnDescriptor("firstName");

    // HBase column descriptor for last name.
    private HColumnDescriptor last = new HColumnDescriptor("lastName");

    public GridCacheHBasePersonStore() throws Exception {
        prepareDb();
    }

    // Load entry from HBase.
    @Override 
    public Person load(String cacheName, GridCacheTx tx, Long key) 
        throws GridException {
        HTableInterface t = tblPool.getTable(TABLE_NAME);

        try {
            Result r = t.get(new Get(Bytes.toBytes(key)));

            if (r == null)
                throw new GridException("Failed to load key: " + key);

            if (r.isEmpty())
                return null;

            Person p = new Person();

            p.setId(Bytes.toLong(r.getRow()));
            p.setFirst(Bytes.toString(r.getValue(first.getName(), null)));
            p.setLast(Bytes.toString(r.getValue(last.getName(), null)));
            
            return p;
        }
        catch (IOException e) {
            throw new GridException(e);
        }
        finally {
            close(t);
        }
    }

    // Store entry in HBase.
    @Override 
    public void put(String cacheName, GridCacheTx tx, Long key, Person val)
        throws GridException {
        HTableInterface t = tblPool.getTable(TABLE_NAME);

        try {
            t.put(new Put(Bytes.toBytes(key))
                .add(first.getName(), null, Bytes.toBytes(val.getFirst()))
                .add(last.getName(), null, Bytes.toBytes(val.getLast()));
        }
        catch (IOException e) {
            throw new GridException(e);
        }
        finally {
            close(t);
        }
    }

    // Remove entry from HBase.
    @Override 
    public void remove(String cacheName, GridCacheTx tx, Long key) 
        throws GridException {
        HTableInterface t = tblPool.getTable(TABLE_NAME);

        try {
            t.delete(new Delete(Bytes.toBytes(key)));
        }
        catch (IOException e) {
            throw new GridException(e);
        }
        finally {
            close(t);
        }
    }

    // Initialize HBase database.
    private void prepareDb() throws IOException {
        Configuration cfg = new Configuration();

        cfg.addResource(CONFIG_PATH);

        HBaseAdmin admin = new HBaseAdmin(cfg);

        if (!admin.tableExists(TABLE_NAME)) {
            HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);

            desc.addFamily(first);
            desc.addFamily(last);

            admin.createTable(desc);
        }

        tblPool = new HTablePool(cfg, MAX_POOL_SIZE);
    }

    // Close HBase Table.
    private void close(@Nullable HTableInterface t) {
        ...
    }
}

  To configure this store you should simply specify it in cache configuration and enable write-behind if you need data to be periodically flushed to HBase like so:

HBase Store Cache Configuration

><bean class="org.gridgain.grid.cache.GridCacheConfigurationAdapter">
    ...
    <!-- Setup HBase Cache Store. -->
    <property name="store">
        <bean class="GridCacheHBasePersonStore" scope="singleton"/>
    </property>

    <!-- Enable write-behind. -->
    <property name="writeBehindEnabled" value="true"/>
    ...
</bean>

 

 

Data (computing)

Published at DZone with permission of Dmitriy Setrakyan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Key Elements of Site Reliability Engineering (SRE)
  • ClickHouse: A Blazingly Fast DBMS With Full SQL Join Support
  • Kubernetes-Native Development With Quarkus and Eclipse JKube
  • How To Perform Local Website Testing Using Selenium And Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: