Over a million developers have joined DZone.

Spring Boot/Batch Tutorial: Integration With HBASE REST API and Data Ingestion

DZone 's Guide to

Spring Boot/Batch Tutorial: Integration With HBASE REST API and Data Ingestion

In this post, we look at how to integrate these two popular frameworks into your Java app in order to perform data ingestion.

· Big Data Zone ·
Free Resource

Before reading this article you need some basic knowledge on REST, Spring Boot, and Spring Batch.

This article is focused on how to ingest data using Spring Boot/Batch and the HBase REST API. As Spring for Apache Hadoop project will reach End-Of-Life status on April 5th, 2019, using the REST API with Spring Batch helps us to interact with HBase directly from a Windows environment, so you don't need to deploy your jar to a Unix/Linux environment where your HBase is running.

For data ingestion using HBase's REST API, we need to use Base64 encoding for ingesting and fetching as well. First, we need to start HBase's REST API using the following commands:

The screenshot below shows what happens when these commands are run:

Now we are ready and have HBase's REST API up and running. Let's create a sprinboot-batch project and create our classes for file ingestion.

This use case covers the three concepts:

  1. Reading and writing multiple files with Spring Batch.

  2. Integrating REST APIs in an item writer.

  3. Invoking HBase's REST API for ingestion.

Here we are going to ingest each file as a single column in an HBase table and file name as row-key of that HBase table. First, create a Java bean to define the file details that we'll use across the project.

Java bean

Java code

Now let's build the job config class with a reader, processor, and writer:

package com.damu.hbase.test.ingest;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.security.KeyManagementException;

import java.security.KeyStoreException;

import java.security.NoSuchAlgorithmException;

import java.security.cert.X509Certificate;

import java.time.Instant;

import java.util.Base64;

import java.util.List;

import java.util.stream.Collectors;

import javax.net.ssl.SSLContext;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;

import org.apache.http.conn.ssl.TrustStrategy;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.springframework.batch.core.Job;

import org.springframework.batch.core.Step;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;

import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;

import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;

import org.springframework.batch.core.launch.support.RunIdIncrementer;

import org.springframework.batch.item.ItemWriter;

import org.springframework.batch.item.support.PassThroughItemProcessor;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.http.HttpEntity;

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpMethod;

import org.springframework.http.ResponseEntity;

import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.web.client.RestTemplate;




public class ReadMultiFileJob {


 private JobBuilderFactory jobBuilderFactory;


 private StepBuilderFactory stepBuilderFactory;


 public Job readFiles() throws IOException {

  return jobBuilderFactory.get(“readFiles”).incrementer(new RunIdIncrementer()).




 public Step step1() throws IOException {

  return stepBuilderFactory.get(“step1”). < JSONFileBatchBean, JSONFileBatchBean > chunk(10)




 public PassThroughItemProcessor < JSONFileBatchBean > processor() {

  return new PassThroughItemProcessor < JSONFileBatchBean > ();



 public FileReader multiResourceItemReader() throws IOException {

  List < File > jsonFiles = Files.walk(Paths.get(“C: \\DAMU\\ XML\\ multi”))




  System.out.println(“inside file reader === === === === === = ”);

  return new FileReader(jsonFiles);



 public RestTemplate restTemplate() {

  TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;

  SSLContext sslContext = null;

  try {

   sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy)


  } catch (KeyManagementException e) {


  } catch (NoSuchAlgorithmException e) {


  } catch (KeyStoreException e) {



  SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);

  CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();

  HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();


  RestTemplate restTemplate = new RestTemplate(requestFactory);

  return restTemplate;



 public ItemWriter < JSONFileBatchBean > writer() {

  return items -> {

   for (JSONFileBatchBean bean: items)



    //System.err.println(“bean.toString() — -”+bean.getJsonFile().createNewFile());

    FileInputStream in = new FileInputStream(bean.getJsonFile());

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] buf = new byte[1024];

    for (int readNum;
     (readNum = in .read(buf)) != -1;) {

     bos.write(buf, 0, readNum);


    byte[] byteArray = bos.toByteArray();

    Instant instant = Instant.now();

    long timeStampMillis = instant.toEpochMilli();

    String rowKey = bean.getFileName();

    HttpHeaders headers = new HttpHeaders();

    headers.set(“Accept”, “application / json”);

    headers.set(“content - type”, “application / json”);

    String fileName = bean.getFileName();

    String rowKeyHash = Base64.getEncoder().encodeToString(bean.getFileName().getBytes(“utf - 8”));

    String URL = ”http: //localhost:8157/FILE_DATA/"+rowKeyHash;

     String jsonPaylod = ” {
      Row\”: [{
       key\”: “+“\””


       \”Cell\”: [{
        column\”: \””

         +“RGF0YUNGOnRlc3R4bWw = ” //XML test


        +“timestamp\”: ”

         +“\”” +timeStampMillis + ”\””

        \”$\”: \””+Base64.getEncoder().encodeToString(byteArray)


    System.out.println(“byteArray === ”+byteArray);

    System.out.println(“jsonPaylod === ”+jsonPaylod);

    HttpEntity < String > entity = new HttpEntity < > (jsonPaylod, headers);

    ResponseEntity < String > response = restTemplate().exchange(URL, HttpMethod.POST, entity, String.class);

    System.out.println(“Response: “+response);

    in .close();





ow let's see how we are calling HBase API

Here are the table structure and URL details.

After running the  Spring Boot application console logger, we get the following:



jsonPaylod==={“Row”: [{“key”: “VEVTVF9KU09OLmpzb24=”,”Cell”: [{“column”: “RGF0YUNGOnRlc3R4bWw=”, “timestamp”:”1554230585299",”$”: “ew0KICAibmFtZSI6ICJKdW5nbGUgR3ltIiwNCiAgImFnZSI6IDI1LA0KICAiZmF2b3JpdGVfY29sb3IiOiAiI2ZmYTUwMCIsDQogICJnZW5kZXIiOiAibWFsZSIsDQogICJsb2NhdGlvbiI6IHsNCiAgICAiY2l0eSI6ICJTZWF0dGxlIiwNCiAgICAic3RhdGUiOiAiV0EiLA0KICAgICJjaXR5c3RhdGUiOiAiU2VhdHRsZSwgV0EiDQogIH0sDQogICJwZXRzIjogWw0KICAgIHsNCiAgICAgICJ0eXBlIjogImRvZyIsDQogICAgICAibmFtZSI6ICJGb28iDQogICAgfSwNCiAgICB7DQogICAgICAidHlwZSI6ICJjYXQiLA0KICAgICAgIm5hbWUiOiAiQmFyIg0KICAgIH0NCiAgXQ0KfQ==”}]}]}

Response: <200,{}>



jsonPaylod==={“Row”: [{“key”: “VEVTVF9URVhULnR4dA==”,”Cell”: [{“column”: “RGF0YUNGOnRlc3R4bWw=”, “timestamp”:”1554230585637",”$”: “SGVsbG8gdGhpcyBzaW1wbGUgaGFic2UgaW50ZWdyYXRpb24=”}]}]}

Response: <200,{}>



2019-Apr-02 11:43:06.292 INFO [restartedMain] o.s.b.c.l.s.SimpleJobLauncher — Job: [FlowJob: [name=readFiles]] completed with the following parameters: [{run.id=14}] and the following status: [COMPLETED]

We can use the HBase GET API to see the ingested data:

Data ingestion

spring batch ,spring boot tutorial ,big data ,data ingestion ,restful apis

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}