Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing CSV headers only [feature request?] #92

Closed
nakamorichi opened this issue Jun 21, 2018 · 5 comments
Closed

Writing CSV headers only [feature request?] #92

nakamorichi opened this issue Jun 21, 2018 · 5 comments
Labels

Comments

@nakamorichi
Copy link

nakamorichi commented Jun 21, 2018

I have a case when there might be no actual input, but I still want to output an empty CSV file containing the headers. Due to the streaming nature, I don't know beforehand whether there will be input objects or not, so I want to write the headers first. Is there a way to get the headers as a String, byte[], etc.?

EDIT: found a way to get the header as a string, but surely there's a better way?

List<String> columns = new ArrayList<>();
CsvSchema schema = mapper.typedSchemaFor(klass);
schema.forEach(column -> columns.add(column.getName()));
String headerStr = String.join(",", columns) + "\n";
@cowtowncoder
Copy link
Member

@Kitanotori I agree in that it seems like a reasonable thing to be able to write "empty" files, i.e. just header line, no data lines.

Does this not work by using SequenceWriter (creating ObjectWriter from mapper, then SequenceWriter with writeValues() method), and by simply creating, closing it without writing any entries?

@amr
Copy link

amr commented Jun 25, 2020

@cowtowncoder I was able to confirm that doing what you suggested does indeed write the header. Thanks for the tip!

Would be nice of course if this was provided explicitly through the API somehow.

@dabaus
Copy link

dabaus commented Feb 1, 2024

Was this ever added? I have similar case where i want to write csv but in a streaming way.
So i need to get the header first.

@cowtowncoder
Copy link
Member

See my response: it was already possible to do, as confirmed by @amr.

Will close.

@cowtowncoder cowtowncoder closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2024
@cowtowncoder
Copy link
Member

@amr That is via API, fwtw. But I guess you mean something like a convenience method or so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants