Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate java class from csv file

Tags:

java

csv

I have multiple csv files containing about 200 to 300 columns and I need to create pojos out of them with a mapping of one to one (column too java class field). Never mind the fact that is not recommended. If you know a tool or how to do this automatically please pitch in.

so you have a csv file, containing thousands of rows with hundreds of columns, the first row contains the header of the columns. SO what I need, based on the first row (header of column) to create a java class that contains those headers as class fields. Never mind the actual data. I just need a java class with those fields

There was a question regarding somewhat this post but this was asked about 3 years ago, so I guess is obsolete.

like image 910
aurelius Avatar asked Oct 17 '25 03:10

aurelius


2 Answers

You can use Javassist to generate classes at runtime:

Code:

public static void main(String[] args) throws Exception {
    String[] fieldNames = null;
    Class<?> rowObjectClass = null;
    try(BufferedReader stream = new BufferedReader(new InputStreamReader(Program.class.getResourceAsStream("file.csv")))) {
        while(true) {
            String line = stream.readLine();
            if(line == null) {
                break;
            }
            if(line.isEmpty() || line.startsWith("#")) {
                continue;
            }
            if(rowObjectClass == null) {
                fieldNames = line.split(",");
                rowObjectClass = buildCSVClass(fieldNames);
            } else {
                String[] values = line.split(",");
                Object rowObject = rowObjectClass.newInstance();
                for (int i = 0; i < fieldNames.length; i++) {
                    Field f = rowObjectClass.getDeclaredField(fieldNames[i]);
                    f.setAccessible(true);
                    f.set(rowObject, values[i]);

                }
                System.out.println(reflectToString(rowObject));
            }
        }
    }
}

private static int counter = 0;
public static Class<?> buildCSVClass(String[] fieldNames) throws CannotCompileException, NotFoundException {
    ClassPool pool = ClassPool.getDefault();
    CtClass result = pool.makeClass("CSV_CLASS$" + (counter++));
    ClassFile classFile = result.getClassFile();
    ConstPool constPool = classFile.getConstPool();
    classFile.setSuperclass(Object.class.getName());
    for (String fieldName : fieldNames) {
        CtField field = new CtField(ClassPool.getDefault().get(String.class.getName()), fieldName, result);
        result.addField(field);
    }
    classFile.setVersionToJava5();
    return result.toClass();
}

public static String reflectToString(Object value) throws IllegalAccessException {
    StringBuilder result = new StringBuilder(value.getClass().getName());
    result.append("@").append(System.identityHashCode(value)).append(" {");
    for (Field f : value.getClass().getDeclaredFields()) {
        f.setAccessible(true);
        result.append("\n\t").append(f.getName()).append(" = ").append(f.get(value)).append(", ");
    }
    result.delete(result.length()-2, result.length());
    return result.append("\n}").toString();
}


Resources:

file.csv (classpath):

############
foo,bar
############
hello,world
cafe,babe


Output:

CSV_CLASS$0@1324706137 {
    foo = hello, 
    bar = world
}
CSV_CLASS$0@1373076110 {
    foo = cafe, 
    bar = babe
}
like image 153
Binkan Salaryman Avatar answered Oct 18 '25 15:10

Binkan Salaryman


As per my undersatnding, you are trying to read a csv file with large range of columns, being treated it as a table for database

My solution is as follows

  • Use csvjdbc to query the data columns

This can be done by using the file as data source and csvjdbc driver, and using the Metadata, you can retrieve all the columns

  • Create a runtime POJO Class using tool Provider

This can be done and a refernce is at here

like image 24
Harshavardhan Konakanchi Avatar answered Oct 18 '25 16:10

Harshavardhan Konakanchi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!