简体   繁体   中英

avoid repetitive code Java

I am working on ETL Java project and it does 3 things extract - read the data from a table transform the data to JSON Load the data It works fine. The issue is I am doing it for each table. The way I have right now is

   class ETLHelper
   {
     private Person read(ResultSet results){
       Person p = new Person();
       p.setPersonId(results.getString("PERSON_ID"));
       p.setPersonName(results.getString("PERSON_NAME"));
       return p;
     }
     private String transform(Person p){
       TransformPerson t = new TransformPerson();
       t.setTransformPersonId(p.getPersonId);
       t.setTransformPersonName(p.getPersonName);
       PersonEData eData = new PersonEData();
       eData.setDate1(p.date1);
       eData.setDate2(p.date2);
       t.seteData(eData);
       PersonDetails pd = new PersonDetails();
       pd.settransformdata(t);
       return writeValueAsString(pd);
     }
     public void etl(){
       Connection c = null;
       PreparedStatement p = null;
       ResultSet r = null;

       c = getConnection();
       p = c.prepareStatement(getSql());
       r = p.executeQuery();

       while(r.next()){
        messages.add(transform(read(r)));
       /*code for loading data*/
       }
     }

   }

Person.Java:

 @JsonTypeName(value = "PERSON")
 @JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
  public class Person{
   @JsonProperty(value = "PERSON_ID")
   private String personId;
   //getter and setter for personId
   @JsonProperty(value = "PERSON_NAME")
   private String personName;
   //getter and setter for personName
}

TransformPerson.java:

@JsonRootName(value = "Person")
class TransformPerson{
 private String transformPersonName;
 private String transformPersonId;  
 /*getter and setter for transformPersonName and tranformPersonId*/
 @override
 String toString(){
  return "Person [name =" + transformPersonName + ", id = " + transformPeronId "]";
  }
 }

PersonEdata:

private String date1;
private String date2;
/*getter and setter*/
@override
public String toString(){
 return "PersonEdata [date1=" + date1 +", date2=" + date2 + "]";
}

So a Person class, a class needed for transformation and etl class is written for each table. There are also some additional classes like PersonEdata that returns JSON when toString() is called. Is there anyway can I change this design to avoid writing the similar code for each table? There are some constraints. Each table is different and they transformation class is needed because there are other programs that uses the JSON generated so we need to generate JSON that needs to understood by those programs.

In your current solution, you have created :

  1. Person class - to hold the data retrieved from DB

  2. PersonTransform class - to copy the data from Person to other representation and have extended the capability to create JSON by overrinding toString()

To keep it simple what you can do is:

  1. Have single Class like Person for each entity (Table) - which is JSON Serializable.

  2. Don't override the toString method to represent the JSON representation - use JSON serializer instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM