/** * Encodes a DBObject as a BSON byte array. * * @param doc the document to encode * @return the document encoded as BSON */ public static byte[] encode(final BSONObject doc) { return new BasicBSONEncoder().encode(doc); }
@Override public Put createPut(byte[] row, BSONObject object) { byte[] raw = bsonEncoder.encode(object); Put put = new Put(row); put.add(COL_FAMILY, COL_QUALIFIER, raw); return put; } }
import org.bson.BasicBSONEncoder; BSONObject bson = (BSONObject)com.mongodb.util.JSON.parse(string_json); BasicBSONEncoder encoder = new BasicBSONEncoder(); byte[] bson_byte = encoder.encode(bson);
@Override public byte[] createRowKey(BSONObject object) { Object id = object.get("_id"); byte[] raw; if (id instanceof ObjectId) { raw = ((ObjectId) id).toByteArray(); } else if (id instanceof String) { raw = ((String) id).getBytes(); } else if (id instanceof BSONObject) { raw = bsonEncoder.encode(((BSONObject) id)); } else { throw new RuntimeException("Don't know how to serialize _id: " + id.toString()); } return DigestUtils.md5(raw); }
File file = new File("a.bson"); String path = "b.bson"; BasicBSONEncoder encoder = new BasicBSONEncoder(); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); BSONDecoder decoder = new BasicBSONDecoder(); try { while (inputStream.available() > 0) { BSONObject bsonSingleRow = decoder.readObject(inputStream); c = bsonSingleRow.get("yourKey").toString(); if (Math.random()> .98)) Files.write(Paths.get(path), encoder.encode(bsonSingleRow),StandardOpenOption.CREATE, StandardOpenOption.APPEND); } } } catch (IOException e) { ... }
.add("s", inputSplit.getStart()) .add("l", inputSplit.getLength()).get(); byte[] encodedObj = bsonEnc.encode(splitObj); fsDataOut.write(encodedObj, 0, encodedObj.length);
.add("s", inputSplit.getStart()) .add("l", inputSplit.getLength()).get(); byte[] encodedObj = bsonEnc.encode(splitObj); fsDataOut.write(encodedObj, 0, encodedObj.length);
@Override public void writeStructEnd() throws TException { super.writeStructEnd(); if (!inFlight()) { // We've finished writing the root struct, so dump out to the real transport. realTransport.write(new BasicBSONEncoder().encode(super.getOutput())); } }
public byte[] DBObjectToBSON(DBObject dbObject) { BasicBSONEncoder encoder = new BasicBSONEncoder(); byte bson[] = encoder.encode(dbObject); return bson; } public DBObject BSONToDBObject(byte[] bson) { BasicBSONDecoder decoder = new BasicBSONDecoder(); JSONCallback callback = new JSONCallback(); decoder.decode(bson, callback); Object obj = callback.get(); DBObject dbObject = (DBObject) obj; return dbObject; }
byte[] mongoBytes = new BasicBSONEncoder().encode(mongoData); long mongoWriteTime = System.nanoTime() - start; assertSame(bytes, mongoBytes, "BSON ", "Mongo ");