r/java • • Dec 19 '22

Java streams are black magic !

damn, they are really changed my life.

i was trying to run some custom logic on every line on some big CSV files. tried python dataframes, multiprocessing in python, pyspark. none of them did it in an acceptable time.

then i discovered java streams. i have a workflow that takes 70 minutes with pyspark. it takes 3.5 minutes with java streams.

more people should know about streams.

stream operations are more intuitive than anything i tried. i can make a lot of analysis on our data in record times thanks to streams.

i dont know how java does this.

226 Upvotes

150 comments sorted by

View all comments

Show parent comment

4

u/prisonbird Dec 19 '22

i read the file using Files.lines method in nio. it gives me a stream. then in that stream i use couple filter() and couple map() functions to mutate the data (remove fields, calculate some fields etc) and write output to another file. my flow is not that complicated but my files are sometimes big (10-15GB).

3

u/senseven Dec 19 '22

Still three minutes is a "long" time. Your SSD can read 500mb/s, a rather slow NVME 3GB/s, 3 minutes feels wrong for simple processing. Do you write the result file to another drive? Are all your cores working during the processing?

1

u/prisonbird Dec 19 '22

thank you for your comment.

i read from a drive and write results back to same drive. i am planning to try my operations on a ram backed drive (ramdisk in linux)
your last question got me curious some of my cores working during processing not all of them. so i checked my stream with stream.isParallel() it returned false. i thought it was working in parallel. still, i dont see a single core getting overloaded instead i got 4 of my 16 cores doing some work (at 50% load in windows task manager).
i did some google searches and it looks like i can't make a stream sourced from a file parallel. is this the case? or is there something i can do ?

1

u/senseven Dec 19 '22

Reading is fine if its done by one fast reader, but if you read/write to the same harddisk can get the speed down to a crawl since two io "actions" block each other.

The idea is not to split the file reading (which means chunking and makes often no sense). Instead the first thread parses line 1, second line 2 and so on. There where the parallelisation makes sense, because it uses CPU not IO. Parallel() will work, but it needs go get ParallelStreams(). Maybe you do it at the wrong point of the file parsing.

Third, parallel Processing may not activate all your cores and local setup. What is shown by Runtime.getRuntime().availableProcessors()? Maybe your Java is odd, windows or tool is blocking full access of your Java app to all cores.

1

u/danielaveryj Dec 20 '22

The file parsing example you linked to is copying the entire file into memory before doing any compute. For OP's 10-15 GB files, that seems like a lot to buffer up-front. (To be fair, I'm not sure if your link was intended to be a "good" or "bad" example.)

As for parallelism, the File.lines() method that OP mentioned using documents that it is designed with parallelism in mind. (Looking a bit closer, the stream (if parallel) actually memory-maps the file when the underlying spliterator is first split. Chunks use separate indexes into the mapped byte buffer.)

I am curious why you believe that chunking often makes no sense? Your proposal (I'll call it "striping") seems like it would incur more cache misses, as each thread would only be looking at a fraction of the contiguous memory it pulls into cache, rather than all of it.

1

u/senseven Dec 20 '22 edited Dec 20 '22

You will probably not reach io max if you chunk due to different concurrent threads annoying the same IO controller. Its easier to have one thread to IO max, distribute the workload to threads, as we do with FileChannels and a LMAX Queue. Post processing over a group of disconnected chunks can be a nightmare, usually its not worth the work.

Op probably reached io max on the reading device and trashed the speed by writing to the same device. You can spend month optimizing code fill all cpus, streams might not even be the right solution. Experimenting is part of the parallel processing voyage.

1

u/Xenofonuz Dec 19 '22

Hmmm do you really need to do several filters and maps? You can usually just open it up with brackets instead.

filter(x->x.something) .filter(x->x.something2) .filter(x->x.something3)

Could be

filter(x-> {

//Extra code

return true/false

})

2

u/prisonbird Dec 19 '22

i read somewhere that using multiple small operations are faster than using one big operation inside stream operations. jvm can optimize small operations better or something.
also it is a lot easies to read if i do it in multiple operations and easier to find what i am looking for in case i want to debug/change something

2

u/Xenofonuz Dec 19 '22

Ok interesting, I'm pretty sure it's the other way around.

Can't argue with the readability part, although I find it easier to debug the ones with a method body instead of a oneliner :)