如何使用MongoDB Java Driver将Bson序列写入文件

时间:2018-01-30 00:51:52

标签: java mongodb bson

使用MongoDB Java驱动程序库有一种方法可以将bson对象的写入流式传输到文件中,然后再从该文件中流式读取bson对象。查看文档,我没有看到如何将一系列bson对象编码到一个类似于文件中有一系列json对象的文件。

2 个答案:

答案 0 :(得分:2)

MongoDB GridFS是用于存储和检索文件的规范。

使用 GridFS 存储文件«GridFS使用两个集合将文件保存到数据库:fs.files和fs.chunks。根据文件的大小,数据存储在多个单独的“块”中。  *使用GridFS的MongoDB文件。 Refer to MyPost

有关GridFS的更多信息,请浏览我的Github wiki

public static void main(String[] args) throws IOException {
    mongoDB_GRIDFS("D:\\Yash\\JavaCSV.csv");
}
public static void mongoDB_GRIDFS(String csvlocation) throws IOException{
    Mongo Mongo = new Mongo( "localhost" , 27017 ); // Connect to MongoDB
    DB db = Mongo.getDB( "DBName" ); // Get database
    String bucketName = "BucketName";
    GridFS gridFs = new GridFS(db,bucketName); //Create instance of GridFS implementation  
    String imageName = "image1";
    upload(gridFs, csvlocation, imageName);
    download(gridFs, imageName);     
    Mongo.close();
}
public static void upload(GridFS gridFs, String csvlocation, String imageName) throws IOException{
    GridFSInputFile gridFsInputFile = gridFs.createFile(new File(csvlocation));
    gridFsInputFile.setId("777");
    gridFsInputFile.setFilename(imageName); //Set a name on GridFS entry
    gridFsInputFile.save(); //Save the file to MongoDB
}
public static void download(GridFS gridFs, String imageName) throws IOException{
    GridFSDBFile outputImageFile = gridFs.findOne(imageName);
    String outcsvLocation = "D:\\Yash\\mongoCSV.csv";//Location of the file read from MongoDB to be written
    outputImageFile.writeTo(new File(outcsvLocation));
}

Grid FS

CSV文件到JSON对象,JSON字符串到CSV文件。

JSON到BSON和BSON到JSON。

MongoDB Java Driver jar 附带了实用程序方法,用于将JSON解析为BSON并将BSON序列化为JSON。

  • BSON Library«独立的BSON库,带有新的Codec基础架构,可用于构建高性能编码器和解码器,而无需中间Map实例。

Example

DBObject dbObj = new Document("myKey", "myValue");
String db_json = com.mongodb.util.JSON.serialize( dbObj );

DBObject bson = ( DBObject ) com.mongodb.util.JSON.parse( jsonData );
System.out.println("BSON Object : "+ bson);

示例输出:

BSON Object : [ { "Key2" : "21" , "Key1" : "11" } , { "Key2" : "22" , "Key1" : "12"}]
Json : {"K1":"V1","K2":"V2"}
Map : {K1=V1, K2=V2}

答案 1 :(得分:1)

基于MongoDb Java driver: BSON的文档,我写了以下示例。这是你在找什么?

逻辑类:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

import org.apache.commons.io.IOUtils;
import org.bson.BsonBinaryReader;
import org.bson.BsonBinaryWriter;
import org.bson.BsonReader;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;
import org.bson.codecs.StringCodec;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.bson.io.BasicOutputBuffer;

public class Bson {

    FileOutputStream fop;
    FileInputStream fip;
    BsonWriter writer;
    BsonReader reader;
    Codec<Person> codec;
    EncoderContext ec;
    DecoderContext dc;
    BasicOutputBuffer output;

    public Bson() {
        PojoCodecProvider provider = PojoCodecProvider.builder()
                .register(Person.class)
                .build();

        CodecRegistry registry = CodecRegistries
                .fromRegistries(CodecRegistries.fromCodecs(new StringCodec()),
                        CodecRegistries.fromProviders(provider));
        codec = provider.get(Person.class, registry);
        ec = EncoderContext.builder().build();
        dc = DecoderContext.builder().build();
    }

    public static void main(String[] args) throws IOException {
        Bson bson = new Bson();
        // write data
        bson.initBsonWriter();
        bson.encodePerson(new Person("John", "Doe"));
        bson.encodePerson(new Person("John2", "Doe2"));
        bson.encodePerson(new Person("John3", "Doe3"));
        bson.closeFop();

        // read data
        bson.initBsonReader();
        Person person = bson.decodePerson();
        System.out.println(person);
        person = bson.decodePerson();
        System.out.println(person);
        person = bson.decodePerson();
        System.out.println(person);
        bson.closeFip();
    }

    public void initBsonWriter() throws IOException {
        openFop();
        output = new BasicOutputBuffer();
        writer = new BsonBinaryWriter(output);
        writer.writeStartDocument();
        writer.writeName("values");
        writer.writeStartArray();
    }

    public void initBsonReader() throws IOException {
        openFip();
        reader = new BsonBinaryReader(ByteBuffer.wrap(IOUtils.toByteArray(fip)));
        reader.readStartDocument();
        reader.readName();
        reader.readStartArray();
    }

    public void encodePerson(Person p) {
        codec.encode(writer, p, ec);
    }

    public Person decodePerson() {
        return codec.decode(reader, dc);
    }

    public void openFop() throws IOException {
        File file = new File("example.bson");
        fop = new FileOutputStream(file);

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }
    }

    public void closeFop() throws IOException {
        writer.writeEndArray();
        writer.writeEndDocument();
        output.pipe(fop);
        fop.flush();
        fop.close();
    }

    public void openFip() throws IOException {
        File file = new File("example.bson");
        fip = new FileInputStream(file);
    }

    public void closeFip() throws IOException {
        fip.close();
    }
}

POJO存储一些数据:

public class Person {
    private String firstName;
    private String lastName;

    public Person() { }

    public Person(final String firstName, final String lastName) {  
        this.firstName = firstName;
        this.lastName = lastName;}

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(final String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(final String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "Person [firstName=" + firstName + ", lastName=" + lastName + "]";
    }
}