mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-16 17:34:52 -06:00
unify name of CsvOutptPlugin impl
This commit is contained in:
parent
668f9f35ea
commit
9d96131bbf
@ -1,4 +0,0 @@
|
||||
package io.nosqlbench.engine.extensions.csvoutput;
|
||||
|
||||
public class CsvOutput {
|
||||
}
|
@ -2,7 +2,7 @@ package io.nosqlbench.engine.extensions.csvoutput;
|
||||
|
||||
public class CsvOutputPluginInstance {
|
||||
|
||||
public CsvOutput open(String filename, String... headers) {
|
||||
return new CsvOutputWriter(filename, headers);
|
||||
public CsvOutputPluginWriter open(String filename, String... headers) {
|
||||
return new CsvOutputPluginWriter(filename, headers);
|
||||
}
|
||||
}
|
||||
|
@ -8,23 +8,28 @@ import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.PosixFilePermissions;
|
||||
import java.util.*;
|
||||
|
||||
public class CsvOutputWriter extends CsvOutput {
|
||||
public class CsvOutputPluginWriter extends CsvOutput {
|
||||
|
||||
private final CSVPrinter printer;
|
||||
private final FileWriter filewriter;
|
||||
private final LinkedHashSet<String> headerKeys;
|
||||
private final String filename;
|
||||
|
||||
public CsvOutputWriter(String filename, String... headers) {
|
||||
this.filename = filename;
|
||||
CSVFormat fmt = CSVFormat.DEFAULT;
|
||||
this.headerKeys = new LinkedHashSet<>(Arrays.asList(headers));
|
||||
public CsvOutputPluginWriter(String filename, String... headers) {
|
||||
try {
|
||||
this.filewriter = new FileWriter(filename);
|
||||
this.printer = new CSVPrinter(filewriter,fmt);
|
||||
if (Files.size(Path.of(filename))==0) {
|
||||
this.filename = filename;
|
||||
Path filepath = Path.of(filename);
|
||||
Files.createDirectories(filepath.getParent(), PosixFilePermissions.asFileAttribute(
|
||||
PosixFilePermissions.fromString("rwxr-x---")
|
||||
));
|
||||
CSVFormat fmt = CSVFormat.DEFAULT;
|
||||
this.headerKeys = new LinkedHashSet<>(Arrays.asList(headers));
|
||||
this.filewriter = new FileWriter(filepath.toString());
|
||||
this.printer = new CSVPrinter(filewriter, fmt);
|
||||
if (Files.size(Path.of(filename)) == 0) {
|
||||
printer.printRecord(headerKeys);
|
||||
printer.flush();
|
||||
}
|
||||
@ -33,21 +38,21 @@ public class CsvOutputWriter extends CsvOutput {
|
||||
}
|
||||
}
|
||||
|
||||
public CsvOutputWriter write(Value value) {
|
||||
public CsvOutputPluginWriter write(Value value) {
|
||||
List<String> lineout = new ArrayList<>();
|
||||
Map<String,String> provided = new HashMap<>();
|
||||
Map<String, String> provided = new HashMap<>();
|
||||
if (value.isHostObject()) {
|
||||
Object o = value.asHostObject();
|
||||
if (o instanceof Map) {
|
||||
((Map<?, ?>) o).forEach((k,v) -> {
|
||||
provided.put(k.toString(),v.toString());
|
||||
((Map<?, ?>) o).forEach((k, v) -> {
|
||||
provided.put(k.toString(), v.toString());
|
||||
});
|
||||
} else {
|
||||
throw new RuntimeException("host object provided as '" + o.getClass().getCanonicalName()+ ", but only Maps are supported.");
|
||||
throw new RuntimeException("host object provided as '" + o.getClass().getCanonicalName() + ", but only Maps are supported.");
|
||||
}
|
||||
} else if (value.hasMembers()) {
|
||||
for (String vkey : value.getMemberKeys()) {
|
||||
provided.put(vkey,value.getMember(vkey).toString());
|
||||
provided.put(vkey, value.getMember(vkey).toString());
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("Value was not a Map host object nor a type with members.");
|
||||
@ -60,7 +65,7 @@ public class CsvOutputWriter extends CsvOutput {
|
||||
lineout.add("");
|
||||
}
|
||||
}
|
||||
if (provided.size()>0) {
|
||||
if (provided.size() > 0) {
|
||||
throw new RuntimeException("Unqualified column was emitted for file '" + filename);
|
||||
}
|
||||
|
@ -7,14 +7,14 @@ import org.junit.jupiter.api.Test;
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
public class CsvOutputWriterTest {
|
||||
public class CsvOutputPluginWriterTest {
|
||||
|
||||
@Test
|
||||
public void testCsvOutputWriter() {
|
||||
File tmpfile = Files.newTemporaryFile();
|
||||
tmpfile.deleteOnExit();
|
||||
System.out.println("tmpfile="+ tmpfile.getPath());
|
||||
CsvOutputWriter out = new CsvOutputWriter(tmpfile.getPath(), "one", "two");
|
||||
CsvOutputPluginWriter out = new CsvOutputPluginWriter(tmpfile.getPath(), "one", "two");
|
||||
out.write(Value.asValue(Map.of("one","one_","two","two_")));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user