use of org.apache.commons.csv.CSVParser in project phoenix by apache.
the class CSVCommonsLoaderIT method testCSVUpsertWithCustomDelimiters.
@Test
public void testCSVUpsertWithCustomDelimiters() throws Exception {
CSVParser parser = null;
PhoenixConnection conn = null;
try {
String stockTableName = generateUniqueName();
// Create table
String statements = "CREATE TABLE IF NOT EXISTS " + stockTableName + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
// Upsert CSV file
CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, stockTableName, Arrays.<String>asList(STOCK_COLUMNS), true, '1', '2', '3', CSVCommonsLoader.DEFAULT_ARRAY_ELEMENT_SEPARATOR);
csvUtil.upsert(new StringReader(STOCK_CSV_VALUES_WITH_DELIMITER));
// Compare Phoenix ResultSet with CSV file content
PreparedStatement statement = conn.prepareStatement("SELECT SYMBOL, COMPANY FROM " + stockTableName);
ResultSet phoenixResultSet = statement.executeQuery();
parser = new CSVParser(new StringReader(STOCK_CSV_VALUES_WITH_DELIMITER), csvUtil.getFormat());
for (CSVRecord record : parser) {
assertTrue(phoenixResultSet.next());
int i = 0;
for (String value : record) {
assertEquals(value, phoenixResultSet.getString(i + 1));
i++;
}
}
assertFalse(phoenixResultSet.next());
} finally {
if (parser != null)
parser.close();
if (conn != null)
conn.close();
}
}
use of org.apache.commons.csv.CSVParser in project phoenix by apache.
the class CSVCommonsLoader method upsert.
/**
* Upserts data from CSV file.
*
* Data is batched up based on connection batch size.
* Column PDataType is read from metadata and is used to convert
* column value to correct type before upsert.
*
* The constructor determines the format for the CSV files.
*
* @param fileName
* @throws Exception
*/
public void upsert(String fileName) throws Exception {
CSVParser parser = CSVParser.parse(new File(fileName), Charsets.UTF_8, format);
upsert(parser);
}
use of org.apache.commons.csv.CSVParser in project phoenix by apache.
the class CSVFileResultHandler method read.
public synchronized List<Result> read() throws IOException {
CSVParser parser = null;
util.ensureBaseResultDirExists();
try {
File file = new File(resultFileName);
parser = CSVParser.parse(file, Charset.defaultCharset(), CSVFormat.DEFAULT);
List<CSVRecord> records = parser.getRecords();
List<Result> results = new ArrayList<>();
String header = null;
for (CSVRecord record : records) {
// First record is the CSV Header
if (record.getRecordNumber() == 1) {
header = record.toString();
continue;
}
List<ResultValue> resultValues = new ArrayList<>();
for (String val : record.toString().split(PherfConstants.RESULT_FILE_DELIMETER)) {
resultValues.add(new ResultValue(val));
}
Result result = new Result(resultFileDetails, header, resultValues);
results.add(result);
}
return results;
} finally {
parser.close();
}
}
use of org.apache.commons.csv.CSVParser in project tika by apache.
the class ISATabUtils method parseStudy.
public static void parseStudy(InputStream stream, XHTMLContentHandler xhtml, Metadata metadata, ParseContext context) throws IOException, TikaException, SAXException {
TikaInputStream tis = TikaInputStream.get(stream);
// Automatically detect the character encoding
TikaConfig tikaConfig = context.get(TikaConfig.class);
if (tikaConfig == null) {
tikaConfig = TikaConfig.getDefaultConfig();
}
try (AutoDetectReader reader = new AutoDetectReader(new CloseShieldInputStream(tis), metadata, tikaConfig.getEncodingDetector());
CSVParser csvParser = new CSVParser(reader, CSVFormat.TDF)) {
Iterator<CSVRecord> iterator = csvParser.iterator();
xhtml.startElement("table");
xhtml.startElement("thead");
if (iterator.hasNext()) {
CSVRecord record = iterator.next();
for (int i = 0; i < record.size(); i++) {
xhtml.startElement("th");
xhtml.characters(record.get(i));
xhtml.endElement("th");
}
}
xhtml.endElement("thead");
xhtml.startElement("tbody");
while (iterator.hasNext()) {
CSVRecord record = iterator.next();
xhtml.startElement("tr");
for (int j = 0; j < record.size(); j++) {
xhtml.startElement("td");
xhtml.characters(record.get(j));
xhtml.endElement("td");
}
xhtml.endElement("tr");
}
xhtml.endElement("tbody");
xhtml.endElement("table");
}
}
use of org.apache.commons.csv.CSVParser in project tika by apache.
the class ISATabUtils method extractMetadata.
private static void extractMetadata(Reader reader, Metadata metadata, String studyFileName) throws IOException {
boolean investigationSection = false;
boolean studySection = false;
boolean studyTarget = false;
Map<String, String> map = new HashMap<String, String>();
try (CSVParser csvParser = new CSVParser(reader, CSVFormat.TDF)) {
Iterator<CSVRecord> iterator = csvParser.iterator();
while (iterator.hasNext()) {
CSVRecord record = iterator.next();
String field = record.get(0);
if ((field.toUpperCase(Locale.ENGLISH).equals(field)) && (record.size() == 1)) {
investigationSection = Arrays.asList(sections).contains(field);
studySection = (studyFileName != null) && (field.equals(studySectionField));
} else {
if (investigationSection) {
addMetadata(field, record, metadata);
} else if (studySection) {
if (studyTarget) {
break;
}
String value = record.get(1);
map.put(field, value);
studyTarget = (field.equals(studyFileNameField)) && (value.equals(studyFileName));
if (studyTarget) {
mapStudyToMetadata(map, metadata);
studySection = false;
}
} else if (studyTarget) {
addMetadata(field, record, metadata);
}
}
}
} catch (IOException ioe) {
throw ioe;
}
}
Aggregations