Question

I am trying to query a .CSV file using Java and MetaModel API and not succeeding. This API is easy with a .XLS file but with a .CSV I am stumped because there is no table name for the SQL query. Can anyone provide any hint or insight on how to do this?

public static Object[][] getCsvData( File csvFile ) 
{   
    CsvConfiguration conf = new CsvConfiguration( 1, true, false );
    DataContext ctx = DataContextFactory.createCsvDataContext( csvFile, conf );

// CsvDataSet dataSet = csvContext.query().
//      .selectAll()
//      .where("run").eq("Y")
//      .execute();
// List<Row> rows = dataSet.toRows();

If you want to try my code, see my GitHub project here, which is a proof of concept in progress (with a working Excel example).

Was it helpful?

Solution

you can do like this For CSV

public static Object[][] getCsvData(File csvFile) {
        CsvConfiguration conf = new CsvConfiguration(1);
        DataContext csvContext = DataContextFactory.createCsvDataContext(
                csvFile, conf);
        Schema schema = csvContext.getDefaultSchema();
        Table[] tables = schema.getTables();
        Table table = tables[0];
        DataSet dataSet = csvContext.query().from(table).selectAll().where("run").eq("Y").execute();
        List<Row> rows = dataSet.toRows();
        Object[][] myArray = new Object[rows.size()][2];
        int i = 0;
        SelectItem[] cols = rows.get(0).getSelectItems();
        for (Row r : rows) {
            Object[] data = r.getValues();
            for (int j = 0; j < cols.length; j++) {
                if (data[j] == null)
                    data[j] = ""; // force empty string where there are NULL
                                    // values
            }
            myArray[i][0] = cols;
            myArray[i][1] = data;
            i++;
        }
        logger.info("Row count: " + rows.size());
        logger.info("Column names: " + Arrays.toString(cols));
        return myArray;
    }

I figured it out For XmlDataProvider you can do like this

@Test
    public void testXML() {

        XmlSaxTableDef employeeTableDef = new XmlSaxTableDef(
                "/root/organization/employees/employee", new String[] {
                        "/root/organization/employees/employee/name",
                        "/root/organization/employees/employee/gender",
                        "index(/root/organization)"});

        XmlSaxTableDef organizationTableDef = new XmlSaxTableDef(
                "/root/organization", new String[] { 
                        "/root/organization/name",
                        "/root/organization@type" });

        DataContext dc = new XmlSaxDataContext(xmlFile, employeeTableDef,
                organizationTableDef);

        Table employeeTable = dc.getTableByQualifiedLabel("/employee");
        Column fk = employeeTable.getColumnByName("index(/root/organization)");
        Column empName = employeeTable.getColumnByName("/name");

        Table organizationTable = dc.getTableByQualifiedLabel("/organization");
        Column orgId = organizationTable.getColumnByName("row_id");
        Column orgName = organizationTable.getColumnByName("/name");
        Query q = dc.query().from(employeeTable)
                .innerJoin(organizationTable).on( fk, orgId )
                .select(empName).as("employeename")
                .select(orgName).as("companyname").toQuery();
        DataSet ds = dc.executeQuery(q);

        List<Row> rows = ds.toRows();
        for (Row r : rows) {
            System.out.println(Arrays.deepToString(r.getValues()));
        }

    }

Let me know if you face any issues :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top