[ETL] Classe UsuarioETL
Autor: _matheus/. | Publicado: 2011-09-09T04:48:00-07:00

Em seguida é explicado o funcionamento do processo de ETL da tabela Usuario do banco de dados Acordeon para a tabela Usuario do banco de dados do DW de Acordeon.



package br.com.etl.usuarios;

import br.com.acessobd.AcessoBdAcordeon;
import br.com.acessobd.AcessoBdDw;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UsuarioETL {

&nbsp; &nbsp; PreparedStatement pstm;
&nbsp; &nbsp; ResultSet rs;
&nbsp; &nbsp; String consultaUsuario = "SELECT * FROM USUARIO";
&nbsp; &nbsp; String cadastraUsuario = "INSERT INTO USUARIO (ID, CPF, DT_NASC, EMAIL, NOME_COMPLETO, SENHA, SEXO, TELEFONE) "
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "VALUES(?,?,?,?,?,?,?,?)";

&nbsp; &nbsp; public List&lt;UsuarioBean&gt; extract() {
&nbsp; &nbsp; &nbsp; &nbsp; List&lt;UsuarioBean&gt; listUsuario = new ArrayList&lt;UsuarioBean&gt;();
&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AcessoBdAcordeon mysql = new AcessoBdAcordeon();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pstm = mysql.conectar().prepareStatement(consultaUsuario);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs = pstm.executeQuery();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UsuarioBean usu;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (rs.next()) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usu = new UsuarioBean();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usu.setId(rs.getInt("id"));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usu.setCpf(rs.getString("cpf"));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usu.setDtNasc(rs.getDate("dt_nasc"));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usu.setEmail(rs.getString("email"));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usu.setNomeCompleto(rs.getString("nome_completo"));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usu.setSenha(rs.getString("senha"));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usu.setSexo(rs.getInt("sexo"));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usu.setTelefone(rs.getString("telefone"));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listUsuario.add(usu);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysql.desconectar();
&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; return listUsuario;
&nbsp; &nbsp; }

&nbsp; &nbsp; public String transform(int sexo) {
&nbsp; &nbsp; &nbsp; &nbsp; if (sexo == 0) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "M";
&nbsp; &nbsp; &nbsp; &nbsp; } else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "F";
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }

&nbsp; &nbsp; public void load(UsuarioBean usuario) {
&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AcessoBdDw mysql = new AcessoBdDw();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pstm = mysql.conectar().prepareStatement(cadastraUsuario);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pstm.setInt(1, usuario.getId());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pstm.setString(2, usuario.getCpf());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pstm.setDate(3, usuario.getDtNasc());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pstm.setString(4, usuario.getEmail());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pstm.setString(5, usuario.getNomeCompleto());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pstm.setString(6, usuario.getSenha());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pstm.setString(7, transform(usuario.getSexo()));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pstm.setString(8, usuario.getTelefone());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pstm.executeUpdate();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysql.desconectar();

&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }


public static void main(String[] args) {
&nbsp; &nbsp; &nbsp; &nbsp; UsuarioETL usuario = new UsuarioETL();
&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; List&lt;UsuarioBean&gt; listUsuario = usuario.extract();
&nbsp; &nbsp; &nbsp; &nbsp; Iterator&lt;UsuarioBean&gt; itU = listUsuario.iterator();
&nbsp; &nbsp; &nbsp; &nbsp; while(itU.hasNext()){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UsuarioBean usu = itU.next();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usuario.load(usu);
&nbsp; &nbsp; &nbsp; &nbsp; }
}

}

&nbsp; &nbsp; &nbsp; &nbsp; O método extract é onde é feita a extração dos dados do banco de dados do Acordeon. Primeiro é aberta uma conexão com o banco de dados, em seguida é executado a query "SELECT * FROM USUARIO" e inserido o resultado desta query em objetos da classe UsuárioBean, sendo que cada objeto é adicionado em um List. Por fim é retornado o List.
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;No método transform é onde é feita a transformação de dados. O método recebe como parâmetro um inteiro, que vai variar em 0 e 1, caso este valor seja 0 é retornada a String "M", e caso seja 1 é retornada a String "F".
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Já o método load é onde é feita a carga dos dados no banco de dados do DW. Ele recebe como parâmetro um objeto da classe UsuarioBean. Primeiro é aberta uma conexão com o banco de dados, em seguida é adicionado no lugar das interrogações de&nbsp;"INSERT INTO USUARIO (ID, CPF,DT_NASC, EMAIL, NOME_COMPLETO, SENHA, SEXO, TELEFONE) VALUES(?,?,?,?,?,?,?,?)" os atributos do UsuarioBean passado. Por fim é executado o update.