2

我想使用 OrientDB 作为 .csv 文件的数据库,并使用 OrientJS 将它们以原始形式存储在记录的二进制字段中。此外,我想将名称和描述存储为字符串。我研究了文档,并能够通过以下方式存储原始二进制记录

 var fs = require('fs');
 var testcsv = fs.readFile('test.csv',
 function(error, data){
    if(error) throw error;
    var binary = new Buffer(data);
    binary['@type'] = 'b';
    binary['@class']='CSV';
    db.record.create(binary);
})

但是,我发现无法存储具有“二进制”类型字段的记录。我尝试了几种方法,所有方法似乎都不起作用。例如:

     var fs = require('fs');
     var testcsv = fs.readFile('test.csv',
     function(error, data){
        if(error) throw error;
        var binary = new Buffer(data);
        binary['@type'] = 'b';
        binary['@class']='CSV'; 
        db.record.create({
           '@class': 'Test',
           'file': binary,
           'name': 'X',
           'description': 'Y'
        });
     })

如果“字段”未声明为“二进制”,则默认情况下将其设置为“嵌入”类型,并且 .csv 为“存储”。如果“字段”被声明为“二进制”,则会引发错误:

Unhandled rejection OrientDB.RequestError: Error on unmarshalling field 'file' in record #22:-1 with value: file: ... 
DB name="mydb"
    at child.Operation.parseError (...node_modules\orientjs\lib\transport\binary\protocol33\operation.js:864:13)
    at child.Operation.consume (...node_modules\orientjs\lib\transport\binary\protocol33\operation.js:455:35)
    at Connection.process (...node_modules\orientjs\lib\transport\binary\connection.js:399:17)
    at Connection.handleSocketData (...node_modules\orientjs\lib\transport\binary\connection.js:290:20)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:548:20)

当我尝试了许多其他方法时,我一无所知。我是不是误会了什么?非常感谢您的帮助!

4

1 回答 1

0

这是一种解决方法,将二进制文件编码base64为字符串属性,然后将其存储为字符串属性。

这是一个示例,我对svg文件进行编码,然后在从数据库加载后再次对其进行解码:

// First npm install the following packages
npm install orientjs
npm install await-to-js

然后创建一个文件app.js并运行它:

const OrientDBClient = require("orientjs").OrientDBClient;
const to = require('await-to-js').default;

// EXAMPLE FILE: AN SVG IMAGE
var svg = `<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400">
<circle cx="100" cy="100" r="50" stroke="black" stroke-width="5" fill="red" /></svg>`

connect().then(async function(db) {

    // Add class to database
    [err, result] = await to(
        db.session.class.create('Example', 'V')
    );
    if (err) { console.error(err); };

    // Add property image to class
    [err, result] = await to(
        db.session.command('CREATE PROPERTY Example.image STRING').all()
    );
    if (err) { console.error(err); };

    // Add property name to class
    [err, result] = await to(
        db.session.command('CREATE PROPERTY Example.name STRING').all()
    );
    if (err) { console.error(err); };

    // *****************************************************************
    // ********************* USING BASE64 ENCODING *********************
    // *****************************************************************

    // Convert to base64
    var buf = Buffer.from(svg, 'utf-8').toString("base64");

    // Add node to class with image encoded as base64
    [err, result] = await to(
        db.session.insert().into('Example').set({image: buf, name: 'ABC'}).one()
    );
    if (err) { console.error(err); };

    // Retrieve base64 encoded svg from database
    [err, result] = await to(
        db.session.select('image').from('Example').where({name: 'ABC'}).one()
    );
    if (err) { console.error(err); };

    // Output svg XML to the console
    var output = Buffer.from(result.image, 'base64');
    console.log(output.toString('ascii'));
})

async function connect() {
    // Connect to Server
    [err,client] = await to(OrientDBClient.connect({
        host:   'localhost',    // Specify your host here
        port:   '2424'          // Make sure you call the HTTP port
    }));
    if (err) {
        console.log("Cannot reach OrientDB. Server is offline");
        return false;
    }

    // Connect to Database.
    [err,session] = await to(client.session({ 
        name:       'demodb',   // Name of your database
        username:   'admin',    // Username of your database
        password:   'admin'     // Password of your database
    }));
    if (err) {
        console.log("Database doesn't exist.");
        return false;
    }

    // Add handler
    session.on('beginQuery', (obj) => {
        console.log(obj);
    });

    // Check if session opens
    console.log("Successfully connected to database.");
    var graph = {client, session};
    return graph;
}

此代码将创建一个Example具有属性的类,image然后name创建一个顶点,将svg其保存为base64编码字符串。它还展示了如何将图像再次检索到 javascript 中。

于 2019-10-17T12:40:02.457 回答