I need to scan very large JSONL files efficiently and am considering a parallel grep-style approach over line-delimited text.

Would love to hear how you would design it.

top 22 comments

sorted by: hot top controversial new old
[+] 7 points 7 months ago* (last edited 7 months ago) (8 children)
  • [–] 3 points 7 months ago (7 children)

    Can a file really be split efficiently? And is reading from multiple files on the same disk really faster than scanning a single file from top to bottom?

  • source
  • parent
  • hideshow 7 child comments
  • [–] 6 points 7 months ago (1 child)

    You don't actually need to "split" anything, you just read from different offsets per thread. Mmap might be the most efficient way to do this (or at least the easiest)

    Whether or not that's going to run into hardware bottlenecks is a separate issue from designing a parallel algorithm. Idk what OP is trying to accomplish, but if their hardware is known (eg this is an internal tool meant to run in a data center), they'll need to read up on their hardware and virtualization architecture to squeeze the most IO performance.

    But if parsing is actually the bottleneck, there's a lot you can do to optimize it in software. Simdjson would be a good place to start.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 2 points 7 months ago (1 child)

    I think, you could open the same file multiple times and then just skip ahead by some number of bytes before you start reading.

    But yeah, no idea if this would actually be efficient. The bottleneck is likely still the hard drive and trying to fit multiple sections of the file into RAM might end up being worse than reading linearly...

  • source
  • parent
  • hideshow 1 child comment
  • load more comments (1 reply)
  • [–] 4 points 7 months ago (2 children)

    The question is, what will be your limiting factor: CPU or disk I/O? Parallel processing doesn't do much good if the workers have to wait on the disk to deliver more data. I'd start with an async architecture, where the program can do its processing while it is waiting on more data.

  • source
  • hideshow 2 child comments
  • [–] 4 points 7 months ago (1 child)

    One additinal trick is to compress your files before writing them to disk, using some kind of fast lightweight compression like parallel gzip (pigz command) or lzop. When parsing them, you will have smaller disk reads but higher CPU usage, which will give speed advantage if you have server-class CPU with lots of cache.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 4 points 7 months ago (1 child)
    1. How many grep-like ops per file?
    2. Is it interactive or run by another process?
    3. Do you know which files ahead of time?
    4. Do you have any control over that file creation?
    5. Is the JSONL append only? Is the grep running while the file is modified?
    6. How large is very large? 100s of MB? Few GB? 100s of GB? Whether or not it fits in memory could change the approach.
    7. You're using files, plural, would parallelizing at the file level (e.g. one thread per file) be enough?
    8. How many files and how often is that executed?
  • source
  • hideshow 1 child comment
  • [–] 3 points 7 months ago

    How large is very large? Would it be something that jq can’t do? Is it purely string search or JSON-tree search?

    Generally you would want to get file size, split it into ranges which can be read as valid UTF-8. Feed each range into reader thread. Can be inefficient for HDDs because each thread will try to access random location on disk forcing needle to jump back and forth. Also you’ll need reread ranges at split point with some positive and negative offset in case desired content got split. Things are getting much more complicated if you want JSON-tree grep. Branches may get split from parent nodes across multiple ranges.

  • source
  • [–] 2 points 7 months ago*
    1. chunk_size := file_size / cpu_cores. Compile regex.

    2. spawn cpu_cores workers:
      2.a. worker #n starts at n * chunk_size bytes. If n > 0, skip bytes until newline encountered.
      2.b worker starts feeding bytes from file/chunk into regex. When match is found, write to output (stdout or file, whichever has better performance). When newline encountered, restart regex state automata.
      2.c after having read chunk_size bytes, continue until encountering a newline to ensure the whole file is covered by the parallel search.

    Optionally, keep track of byte number and attach them to the found matches when outputting, to facilitate eventually de-duplicating and/or navigating to said match in the file.

    To avoid problems, have each worker output to a separate file, and only combine these output files when the workers are all finished.

    As others have said, it's going to be hard to get more speedup than this, and you will ultimately be limited by your storage's read speed and throughput if the whole file cannot fit into memory.

  • source
  • [–] 2 points 7 months ago

    If you're writing a program, definitely multiple threads or processes that each scan a chunk of the file, which basically means seek to the start of the chunk, read lines into the scan code until you hit the end of the chunk. For jsonl each chunk will need an alignment step to not break the jsonl.

    For command line trickery, maybe the file could be chunked up by running multiple dd instances with an offset parameter piped into grep. This has many synchronization issues and all the outputs should be captured separately then combined afterwards. I can't think of a good way to align this method to line edges but maybe you can put some fancy regular expression magic into the grep step to ignore malformed json at the beginning and end and overlap the chunks?

    Grep is fast already, maybe test the simple approach and see how long it takes.

  • source
  • [–] 1 point 7 months ago*

    Could u use an already parallelized solution like ripgrep? I think someone else also mentioned putting it in a database, that shouldnt be too bad either.

  • source
  • [–] 1 point 7 months ago (4 children)

    Bad idea. First, file is read sequentially, and you can't parallelize this. Second, grep is a bad solution for structured files. Better use jq or something similar.

  • source
  • hideshow 4 child comments
  • [–] 3 points 7 months ago (3 children)

    https://jsonltools.com/what-is-jsonl

    First time hearing about it, but JSONL is like CSV with json per line. So not really structured.

    I don’t see why you couldn’t just use grep on it.

  • source
  • parent
  • hideshow 3 child comments
  • [–] 0 points 7 months ago (2 children)

    Sorry, I missed that L, and I've never heard about JSONL before (although worked with JSON logs that are effectively JSONL). So, well, you may use grep, however it can be inefficient (depends on regex engine and how good you are in regexes). It is also easy to make a mistake if you are not very proficient in regexes. So I'd prefer using JSON parser (jq or another, maybe lower level if performance matters) over grep anyway.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 7 months ago (1 child)

    parsing will make it orders of magnitude slower

  • source
  • parent
  • hideshow 1 child comment
  • [–] 1 point 7 months ago

    It will not if your parser is not overcomplicated and does not populate some huge structures with data. You only need to find tokens and compare them with field names and values you are looking for. Regexes are slower and don't allow processing escaped characters correctly.

  • source
  • parent
  • Read the JSONL into a real database like Postgres.

  • source