use of au.com.bytecode.opencsv_voltpatches.CSVWriter in project voltdb by VoltDB.
the class SQLCommandOutputFormatterCSV method printTable.
@Override
public void printTable(PrintStream stream, VoltTable t, boolean includeColumnNames) throws IOException {
final int columnCount = t.getColumnCount();
List<VoltType> columnTypes = new ArrayList<VoltType>(columnCount);
for (int i = 0; i < columnCount; i++) {
columnTypes.add(t.getColumnType(i));
}
CSVWriter csvWriter = new CSVWriter(new OutputStreamWriter(stream));
if (includeColumnNames) {
String[] columnNames = new String[columnCount];
for (int i = 0; i < columnCount; i++) {
columnNames[i] = t.getColumnName(i);
}
csvWriter.writeNext(columnNames);
}
VoltTableUtil.toCSVWriter(csvWriter, t, columnTypes);
}
use of au.com.bytecode.opencsv_voltpatches.CSVWriter in project voltdb by VoltDB.
the class VoltTableUtil method toCSV.
/*
* Returns the number of characters generated and the csv data
* in UTF-8 encoding.
*/
public static Pair<Integer, byte[]> toCSV(VoltTable vt, ArrayList<VoltType> columns, char delimiter, char[] fullDelimiters, int lastNumCharacters) throws IOException {
StringWriter sw = new StringWriter((int) (lastNumCharacters * 1.2));
CSVWriter writer;
if (fullDelimiters != null) {
writer = new CSVWriter(sw, fullDelimiters[0], fullDelimiters[1], fullDelimiters[2], String.valueOf(fullDelimiters[3]));
} else if (delimiter == ',')
// CSV
writer = new CSVWriter(sw, delimiter);
else {
// TSV
writer = CSVWriter.getStrictTSVWriter(sw);
}
toCSVWriter(writer, vt, columns);
String csvString = sw.toString();
return Pair.of(csvString.length(), csvString.getBytes(com.google_voltpatches.common.base.Charsets.UTF_8));
}
use of au.com.bytecode.opencsv_voltpatches.CSVWriter in project voltdb by VoltDB.
the class JDBCStatementReader method susceptibleRun.
@Override
public void susceptibleRun() throws SQLException {
PreparedStatement stmt = null;
Connection conn = null;
ResultSet rslt = null;
RowWithMetaData lineData = null;
int columnCount = 0;
ImporterType.Acceptor[] acceptors = null;
Object[] columnValues = null;
String[] stringValues = null;
try {
conn = DriverManager.getConnection(m_config.jdbcurl, m_config.jdbcuser, m_config.jdbcpassword);
DatabaseMetaData dbmd = conn.getMetaData();
int resultSetType = ResultSet.TYPE_FORWARD_ONLY;
if (!dbmd.supportsResultSetType(resultSetType)) {
resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
}
stmt = conn.prepareStatement("select * from " + m_config.jdbctable, resultSetType, ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(m_config.fetchsize);
rslt = stmt.executeQuery();
ResultSetMetaData mdata = rslt.getMetaData();
columnCount = mdata.getColumnCount();
/*
* Each column from jdbc source must be an importable type. First
* we determine if there is a corresponding importer type for the
* given column. If there is one then determine the column acceptor
* that converts the database type to the appropriate volt type, and
* add it to the acceptors array
*/
acceptors = new ImporterType.Acceptor[columnCount];
for (int i = 1; i <= columnCount; ++i) {
ImporterType type = ImporterType.forClassName(mdata.getColumnClassName(i));
if (type == null) {
throw new SQLException(String.format("Unsupported data type %s for column %s", mdata.getColumnTypeName(i), mdata.getColumnName(i)));
}
acceptors[i - 1] = type.getAcceptorFor(rslt, i);
}
} catch (Exception ex) {
m_log.error("database query initialization failed", ex);
forceClose(conn, stmt, rslt);
Throwables.propagate(ex);
}
StringWriter sw = new StringWriter(16384);
PrintWriter pw = new PrintWriter(sw, true);
CSVWriter csw = new CSVWriter(pw);
StringBuffer sb = sw.getBuffer();
stringValues = new String[columnCount];
try {
while (rslt.next()) {
long rownum = m_totalRowCount.incrementAndGet();
Arrays.fill(stringValues, "NULL");
columnValues = new Object[columnCount];
lineData = new RowWithMetaData(new String[1], rownum);
try {
for (int i = 0; i < columnCount; ++i) {
columnValues[i] = acceptors[i].convert();
stringValues[i] = acceptors[i].format(columnValues[i]);
}
csw.writeNext(stringValues);
((String[]) lineData.rawLine)[0] = sb.toString();
sb.setLength(0);
m_loader.insertRow(lineData, columnValues);
} catch (SQLException ex) {
m_errHandler.handleError(lineData, null, getExceptionAndCauseMessages(ex));
}
}
} catch (InterruptedException ignoreIt) {
} finally {
forceClose(conn, stmt, rslt);
}
m_log.debug("JSBCLoader Done.");
}
use of au.com.bytecode.opencsv_voltpatches.CSVWriter in project voltdb by VoltDB.
the class TestVoltTableUtil method testCSVTimestamp.
/**
* Round-trip the time, see if it's still the same.
* @throws IOException
*/
@Test
public void testCSVTimestamp() throws IOException {
CSVWriter writer = mock(CSVWriter.class);
ColumnInfo[] columns = new ColumnInfo[] { new ColumnInfo("", VoltType.TIMESTAMP) };
ArrayList<VoltType> columnTypes = new ArrayList<VoltType>();
// To make sure we have microseconds we get millisecond from current timestamp
// and add remainder to ensure we have micros in timestamp.
TimestampType ts = new TimestampType((System.currentTimeMillis() * 1000) + System.currentTimeMillis() % 1000);
VoltTable vt = new VoltTable(columns);
vt.addRow(ts);
for (ColumnInfo ci : columns) {
columnTypes.add(ci.type);
}
VoltTableUtil.toCSVWriter(writer, vt, columnTypes);
ArgumentCaptor<String[]> captor = ArgumentCaptor.forClass(String[].class);
verify(writer).writeNext(captor.capture());
String[] values = captor.getValue();
assertEquals(1, values.length);
TimestampType newTs = new TimestampType(values[0]);
assertEquals(ts, newTs);
}
use of au.com.bytecode.opencsv_voltpatches.CSVWriter in project voltdb by VoltDB.
the class TestVoltTableUtil method testCSVNullConversion.
@Test
public void testCSVNullConversion() throws IOException {
CSVWriter writer = mock(CSVWriter.class);
ColumnInfo[] columns = new ColumnInfo[] { new ColumnInfo("", VoltType.BIGINT), new ColumnInfo("", VoltType.FLOAT), new ColumnInfo("", VoltType.DECIMAL), new ColumnInfo("", VoltType.STRING), new ColumnInfo("", VoltType.TIMESTAMP), new ColumnInfo("", VoltType.VARBINARY) };
ArrayList<VoltType> columnTypes = new ArrayList<VoltType>();
VoltTable vt = new VoltTable(columns);
vt.addRow(VoltType.NULL_BIGINT, VoltType.NULL_FLOAT, VoltType.NULL_DECIMAL, VoltType.NULL_STRING_OR_VARBINARY, VoltType.NULL_TIMESTAMP, VoltType.NULL_STRING_OR_VARBINARY);
for (ColumnInfo ci : columns) {
columnTypes.add(ci.type);
}
VoltTableUtil.toCSVWriter(writer, vt, columnTypes);
ArgumentCaptor<String[]> captor = ArgumentCaptor.forClass(String[].class);
verify(writer).writeNext(captor.capture());
String[] values = captor.getValue();
assertEquals(6, values.length);
for (String v : values) {
assertEquals(Constants.CSV_NULL, v);
}
}
Aggregations