通过Matlab和Java驱动程序从MongoDB(gridfs)读取数据 [英] Read data from MongoDB (gridfs) via Matlab and Java driver

查看:204
本文介绍了通过Matlab和Java驱动程序从MongoDB(gridfs)读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Matlab和Java驱动程序.将一些大数据写入数据库后,我想稍后再读取它们. 这些文件大约有100MB,因此我将它们写入GridFS.

I am working with Matlab and the Java driver. After writing some big data to the DB I want to read them out later. The files are about 100MB big and because of that I am writing them into GridFS.

我通过以下方式将Java驱动程序中的数据读入Matlab:

I read the data with the Java driver into Matlab this way:

dbgridfs = GridFS(db, 'data_gridfs');
file1 = dbgridfs.findOne('bigdata');

在该代码之后,我将在Matlab工作区中获得一个对象: 名称大小字节类 file1 1x1 com.mongodb.gridfs.GridFSDBFile

After that code I will get an object in my Matlab workspace: Name Size Bytes Class file1 1x1 com.mongodb.gridfs.GridFSDBFile

现在我将(Java?)对象转换为nativ Matlab变量时遇到了问题.

Now I have a problem with converting the (Java?)-object into a nativ Matlab variable.

我在不同的网站上进行了很多搜索,但没有得到.目前,我正在将数据写入硬盘,然后将其读取到本地Matlab变量中-但这是一种非常肮脏的方式,不要问我有关性能的问题:(

I searched a lot on different sites, but I don't get it. At the moment I am writing the data on my harddisk and after that I read it into a native Matlab variable - but this is a really dirty way and don't ask me relating to the performance :(

我是否缺少Java驱动程序提供的现有解决方案,并且/或者您知道一些可以帮助我的代码吗? 感谢您的帮助.

Are there any existing solutions I missed with the Java driver and/or do you know some code which could help me? Thanks for your help.

致谢 马特

推荐答案

我从未回答过任何论坛问题,但特别是从stackoverflow中受益匪浅,因此我认为我必须至少回馈一次.由于上述问题现在困扰着我近两天,因此我最终找到了解决方案,我认为这是一个回馈社区的好话题.

I have never responded to any forum question, but I have benefitted a lot especially from stackoverflow, so I thought I have to give something back at least once. Since the above issue was bugging me now for nearly two days and I finally stitched together a solution I thought this would be a good topic to give something back to the community.

我遇到了使用Java驱动程序从MongoDB/GridFS检索图像(.png)的相同问题.首先,如上所述,您需要检索文件Java对象:

I had the same issue of retrieving an image (.png) from a MongoDB/GridFS with the Java driver. First, as also described above, you need to retrieve the file Java object:

    import com.mongodb.*;
    import com.mongodb.gridfs.*;

    mongoClient = MongoClient('server_name',27017);
    db = mongoClient.getDB('database_name');
    imgData = GridFS(db,'image_data');

上面的代码部分正在获取带有图像数据的集合.然后,可以检索图像数据Java对象:

The above code part is getting the collection with the image data. Afterwards, the image data Java object can be retrieved:

    javaIObj = imgData.findOne('image_name.png');

现在需要ByteArrayOutputStream,可以在其中将数据流而不是文件传递到其中:

Now ByteArrayOutputStream is needed where the data stream can be piped into instead of a file:

    import java.io.ByteArrayOutputStream;

    baos = ByteArrayOutputStream();
    javaIObj.writeTo(baos);

再次需要将输出流传递到ByteArrayInputStream中,该ByteArrayInputStream可用于构造ImageIO对象:

The output stream needs again to be piped into a ByteArrayInputStream which can be used to construct an ImageIO object:

    import java.io.ByteArrayInputStream;
    import javax.imageio.ImageIO;    

    bais = ByteArrayInputStream(baos.toByteArray());
    jbi = ImageIO.read(bais);

现在可以检索并显示作为矩阵的Matlab实际图像:

Now the actual Matlab image as matrix can be retrieved and displayed:

    nrows = jbi.getHeight; ncols = jbi.getWidth;
    data = jbi.getData.getPixels(0,0,ncols,nrows,[]);
    matImg = reshape(data,ncols,nrows)';

    imagesc(matImg);

我不知道这是否是完美的解决方案,但是它对我有用.

I do not know if this is the perfect solution, but it worked for me.

干杯!

这篇关于通过Matlab和Java驱动程序从MongoDB(gridfs)读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆