Le but du jeu de cet exemple est de créer un projet RESTfull qui insère des données dans une base HBase. Ces services web seront exposés sur un serveur JEEJava Entreprise Edition JBoss EAP 6.1.
Premièrement, il faut installer une base Hbase sur un serveur linux. Il est possible de l’installer sur un serveur windows, mais ceci est plus compliqué à réaliser. Nous ne traiterons pas de l’installation de HBase dans ce post car il existe de nombreux tutoriels très bien faits sur le net.
La première question concernant le projet est de savoir comment architecturer le projet JEE. Sans prétendre être la meilleure solution, voici celle que nous avons mis en place dans le projet:
Ce code permet de bien gérer la connexion à la base de données HBase depuis votre application et en particulier depuis vos managers.
package com.axopen.hbase.connector;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
public class HBaseConnector {
private static HBaseConnector instance;
private static HTablePool hTablePool;
private static Configuration mConfiguration;
/**
* Private Constructor
*/
private HBaseConnector() {
try {
// CONFIGURATION
Configuration lConfiguration = new Configuration();
try {
HBaseAdmin.checkHBaseAvailable(lConfiguration);
} catch (MasterNotRunningException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ZooKeeperConnectionException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// PERMET DE VERIFIER LA CONFIGURATION ET L’ACCES AU SERVEUR
mConfiguration = HBaseConfiguration.create(lConfiguration);
// CREATION D’UN POOL DE TABLE
hTablePool = new HTablePool(lConfiguration,
HBaseConstants.HTABLE_POOL_SIZE);
// CREE LA DATABASE SI NON EXISTANTE
initDatabase();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Creates the tables and table columns in the database.
*
* @throws IOException
*/
public static void initDatabase() throws IOException {
HBaseAdmin admin = new HBaseAdmin(mConfiguration);
HTableDescriptor[] blogs = admin.listTables(HBaseConstants.STATSTABLE);
if (blogs.length == 0) {
HTableDescriptor blogstable = new HTableDescriptor(
HBaseConstants.STATSTABLE);
admin.createTable(blogstable);
// Cannot edit a stucture on an active table.
admin.disableTable(HBaseConstants.STATSTABLE);
// META DATA
HColumnDescriptor metadesc = new HColumnDescriptor(
HBaseConstants.META);
admin.addColumn(HBaseConstants.STATSTABLE, metadesc);
// DATA
HColumnDescriptor datadesc = new HColumnDescriptor(
HBaseConstants.DATA);
admin.addColumn(HBaseConstants.STATSTABLE, datadesc);
// For readin, it needs to be re-enabled.
admin.enableTable(HBaseConstants.STATSTABLE);
}
admin.close();
}
/**
* @return The HbaseTableManager instance
*/
public static HBaseConnector getInstance() {
if (instance == null) {
instance = new HBaseConnector();
}
return instance;
}
/**
* Method used to retrieve a HTable instance.
*
* @param tableName
* The table name
* @return The HTableInterface instance
* @throws IOException
*/
public synchronized HTableInterface getHTable(String tableName)
throws IOException {
return hTablePool.getTable(tableName);
}
}
Enfin pour utiliser votre base de données, il suffit juste d’utiliser votre Singleton nouvellement créé.
HTableInterface table = HBaseConnector.getInstance().getHTable(
HBaseConstants.STATSTABLE);
Échanger des DTO adaptés à chaque besoin afin de retourner le moins de données possible
En tant qu’utilisateurs de Gitlab, vous avez peut-être déjà rêver de fonctionnalités nouvelles permettant d’enrichir votre expérience utilisateur comme la possibilité de faire des statistiques, des exports Excel, etc…
Tuto - Les nativeQuery en Hibernate 4, ou comment lancer une requête écrite « en dur »