Skip to content

Instantly share code, notes, and snippets.

@jgrgt
Created November 4, 2015 08:13
Show Gist options
  • Save jgrgt/746c07a50b1ae897fe44 to your computer and use it in GitHub Desktop.
Save jgrgt/746c07a50b1ae897fe44 to your computer and use it in GitHub Desktop.
Read bson files to documents in Java/Groovy
/**
* Publishing this publicly because it took me so long to get to this stage:
* - Using a memory-mapped byte buffer because I will handle *large* files.
* - Not too happy with the end of my loop, but I could not find how to check for the end of the buffer otherwise.
*/
import org.bson.BsonBinaryReader
import org.bson.BsonReader
import org.bson.Document
import org.bson.codecs.DecoderContext
import org.bson.codecs.DocumentCodec
import java.nio.BufferUnderflowException
import java.nio.MappedByteBuffer
import java.nio.channels.FileChannel
RandomAccessFile aFile = new RandomAccessFile("./assets.bson", "r");
FileChannel inChannel = aFile.getChannel();
MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
buffer.load();
while (true) {
BsonReader reader = new BsonBinaryReader(buffer)
DecoderContext decoderContext = DecoderContext.builder().build()
DocumentCodec codec = new DocumentCodec()
try {
Document document = codec.decode(reader, decoderContext)
println document.get('_id')
} catch (BufferUnderflowException e) {
println "DONE"
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment