-
Notifications
You must be signed in to change notification settings - Fork 9
Tutorial Casper IO
Casper datasets IO provides a set of classes for performing import and export to/from casper datasets. It includes functions/classes for:
-
Import from a collection of POJO beans (CBuildFromCollection)
-
Export to a collection of POJO beans (CExportBeans, CExportBeansCached)
-
Import from CSV, XLS and XLSX files (CBuildFromFile)
-
Export to CSV files (CExportCSV)
-
Casper dataset file definitions, or in other words, specification of a casper dataset, its name, columns, column types, and PKs that is loaded from a file(CDataFileDef)
-
Loading of casper dataset file definitions from files (CDataFileDefLoader)
-
Create a casper dataset from a collection of beans
// create a casper dataset called "people" from the // peopleColl collection using "id" for the primary key builderPK = new CBuildFromCollection("people", peopleColl, Object.class, "id"); // create a casper dataset called "people" from the // peopleColl collection without a primary key builderNoPK = new CBuildFromCollection("people", peopleColl, Object.class, null); CDataCacheContainer container = new CDataCacheContainer(builderPK);
Export a casper dataset to a collection of beans
// Exports a casper dataset to a collection of java beans using Apache commons
// beanutils reflection code. Faster than CExportBeansCached for smaller
// collections.
CExportBeans<Person> cexportBeans = new CExportBeans<Person>(Person.class);
container.export(cexportBeans);
Collection<Person> beans = cexportBeans.getBeans();
// Exports a casper dataset to a collection of java beans using a MethodCache
// for reflection. Faster than CExportBeans for larger
// collections.
CExportBeansCached<Person> cexportBeans = new CExportBeansCached<Person>(Person.class);
container.export(cexportBeans);
Collection<Person> beans = cexportBeans.getBeans();
Load a casper dataset from a file
// Load from an XLS file, reading the column header from the file
// and reading columns as whatever type is returned by the XLS reader
CBuilder builder = new CBuildFromFile(new File("people.xls"));
CDataCacheContainer container = new CDataCacheContainer(builder);
// Load from a CSV file, reading the column header from the file
// and reading columns as String
CBuilder builder = new CBuildFromFile(new File("people.csv"));
CDataCacheContainer container = new CDataCacheContainer(builder);
// Load CSV file, using the headers found in the file,
// and converting each column to the narrowest possible
// data type. Missing integers and doubles will be
// returned as CellReaders.MISSING_VALUE_INTEGER
// or CellReaders.MISSING_VALUE_DOUBLE
CBuilder builder = new CBuildNarrowedFile(new File("people.csv"))
.setConvertMissing(true);
CDataCacheContainer container = new CDataCacheContainer(builder);
// Load the specified columns, as the specified types (ie: CellReader)
// from an CSV file
String[] columnNames = new String[] {"crefnum",
"sex", "refnum", "question", "age",
"weight"};
String[] PKs = new String[] {"refnum", "crefnum"};
CellReader<?>[] columnOptionalReaders = new CellReader<?>[] {
CellReaders.OPTIONAL_INTEGER,
CellReaders.CHARACTER,
CellReaders.OPTIONAL_INTEGER,
CellReaders.OPTIONAL_INTEGER,
CellReaders.OPTIONAL_INTEGER,
CellReaders.OPTIONAL_DOUBLE};
CBuilder builder = new CBuildFromFile(new File("people.csv"), "people dataset",
columnNames, columnOptionalReaders,PKs);
CDataCacheContainer container = new CDataCacheContainer(builder);
Load a casper dataset from a file definition
// Load the same file as the last example but using a CDataFileDef
CDataFile cdf = new CDataFileDef("cdf people xls", columnNames,
columnOptionalReaders, PKs);
CDataCacheContainer container = cdf.loadDataset(new File("people.xls"));
// Load the same file as the last example but from its definition supplied in people.json
CDataFileDef builtCDF = loader.fromJsonFile(new File("people.json"));
CDataCacheContainer container = builtCDF.loadDataset(new File("people.xls"));
// people.json
{
"name":"cdf patients xls",
"column_names":[
"crefnum",
"sex",
"refnum",
"question",
"age",
"weight"
],
"column_types":[
"optional integer",
"character",
"optional integer",
"optional integer",
"optional integer",
"optional double"
],
"primary_key":[
"refnum",
"crefnum"
]
}
Export a casper dataset to a CSV file
// Export complete dataset to a csv file
CExportCSV csv = new CExportCSV(new File("export.csv"));
container.export(csv);
// Export only specified columns of dataset to a csv file
CExportCSV csv = new CExportCSV(new File("export.csv"),"refnum,sex,crefnum,age");
container.export(csv);