MySQL中如何存储矩阵信息?

发布于 2024-08-29 05:38:31 字数 487 浏览 2 评论 0原文

我正在开发一个分析音乐相似性的应用程序。为此,我处理音频数据并将结果存储在 txt 文件中。对于每个音频文件,我创建 2 个文件,其中 1 个包含 16 个值(每个值可以是这样的:2.7000023942731723),另一个文件包含 16 行,每行包含 16 个值,如前面所示。

我想将这两个文件的内容存储在我的 MySQL 数据库的表中。

我的表如下所示:

Name varchar(100)
Author varchar (100)

为了添加这两个文件的内容,我认为我需要使用 BLOB 数据类型:

file1 blob
file2 blob

我的问题是我应该如何将此信息存储在数据库中?我正在使用 Java,其中有一个包含 16 个值(对于 file1)的双精度数组和一个包含 file2 信息的矩阵。我应该将这些值作为字符串处理并将它们添加到数据库中的列中吗?

谢谢

I'm working on an application that analizes music similarity. In order to do that I proccess audio data and store the results in txt files. For each audio file I create 2 files, 1 containing and 16 values (each value can be like this:2.7000023942731723) and the other file contains 16 rows, each row containing 16 values like the one previously shown.

I'd like to store the contents of these 2 file in a table of my MySQL database.

My table looks like:

Name varchar(100)
Author varchar (100)

in order to add the content of those 2 file I think I need to use the BLOB data type:

file1 blob
file2 blob

My question is how should I store this info in the data base? I'm working with Java where I have a double array containing the 16 values (for the file1) and a matrix containing the file2 info. Should I process the values as strings and add them to the columns in my database?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流年里的时光 2024-09-05 05:38:31

希望我不会因为这个疯狂的答案而被负面的言论遗忘,但我正在尝试跳出框框思考。我的第一个问题是,在潜在查询后您如何处理这些数据?如果我做类似的事情,我可能会使用 matlab 或 Octave 之类的东西,它们有表示矩阵的特定符号。它基本上是一堆逗号和分号分隔的文本,在正确的位置带有方括号。我只会存储一个我的数学软件或模块可以本地解析的字符串。毕竟,听起来您不想根据数据点进行某种查询。

Hope I don't get negative repped into oblivion with this crazy answer, but I am trying to think outside the box. My first question is, how are you processing this data after a potential query? If I were doing something similar, I would likely use something like matlab or octave, which have a specific notation for representing matricies. It is basically a bunch of comma and semicolon delimited text with square brackets at the right spots. I would store just a string that my mathematics software or module can parse natively. After all, it doesn't sound like you want to do some kind of query based on a data point.

抱着落日 2024-09-05 05:38:31

我认为如果您打算将其保留在关系数据库中,则需要对这样的模式进行规范化。

听起来您有一个与其文件具有一对多关系的矩阵表。

如果您坚持使用一个非规范化表,一种方法是将文件的名称、其作者、其矩阵的名称以及其行和列位置存储在拥有该文件的命名矩阵中。

请澄清一件事:这是线性代数意义上的矩阵吗?数学实体?

如果是,并且您只使用整个矩阵,那么也许您可以将其作为 blob 存储在单列中。这仍然迫使您在每次进入和离开数据库时序列化和反序列化为字符串或 blob。

I think you need to normalize a schema like this if you intend to keep it in a relational database.

Sounds like you have a matrix table that has a one-to-many relationship with its files.

If you insist on one denormalized table, one way to do it would be to store the name of the file, its author, the name of its matrix, and its row and column position in the named matrix that owns it.

Please clarify one thing: Is this a matrix in the linear algebra sense? A mathematical entity?

If yes, and you only use the matrix in its entirety, then maybe you can store it in a single column as a blob. That still forces you to serialize and deserialize to a string or blob every time it goes into and comes out of the database.

软糯酥胸 2024-09-05 05:38:31

您是否需要查询数据(例如所有大于 2.7 的值)或只是存储数据(您总是从数据库加载整个文件)?

鉴于评论中的信息,我会将文件保存在 BLOB 或 TEXT 中,就像其他答案中所说的那样。您甚至不需要行分隔符,因为您可以对值列表进行模运算来获取矩阵的行。

Do you need to query the data (say for all the values that are bigger than 2.7) or just store it (you always load the whole file from the database)?

Given the information in the comment I would save the files in a BLOB or TEXT like said in other answers. You don't even need a line delimiter since you can do a modulus operation on the list of values to get the row of the matrix.

奢望 2024-09-05 05:38:31

我认为 dedalo 面临的问题是他正在使用数组(我假设一个是锯齿状的,一个是多维的)并且他想要将它们序列化为 blob。

但是,数组不能直接序列化,因此他询问如何进行此操作。

最简单的方法是循环遍历数组并按照戴夫的建议构建一个字符串并存储该字符串。正如 duffymo 指出的那样,这将允许您查看数据库中值的内容,而不是在需要检查数据时反序列化数据。

如果您想知道如何将数组序列化为 BLOB...(这看起来有点过分)

您可以序列化一维数组锯齿状数组,eg:

public class Test {
    public static void main(String[] args) throws Exception {

        // Serialize an int[]
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test.ser"));
        out.writeObject(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
        out.flush();
        out.close();

        // Deserialize the int[]
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("test.ser"));
        int[] array = (int[]) in.readObject();
        in.close();

        // Print out contents of deserialized int[]
        System.out.println("It is " + (array instanceof Serializable) + " that int[] implements Serializable");
        System.out.print("Deserialized array: " + array[0]);
        for (int i=1; i<array.length; i++) {
            System.out.print(", " + array[i]);
        }
        System.out.println();
    }
}

至于MySQL中存储什么数据类型,只有四种 Blob 类型可供选择
四种 BLOB 类型是 TINYBLOB、BLOB、MEDIUMBLOB 和 LONGBLOB

根据序列化对象的大小选择最佳的一种。我想 BLOB 就足够了。

I think the problem that dedalo is facing is that he's working with arrays (I assume one is jagged, one is multi-demensional) and he wants to serialize these to blob.

But, arrays aren't directly serializable so he's asking how to go about doing this.

The simplest way to go about it would be to loop through the array and build a string as Dave suggested and store the string. This would allow you to view the contents from the value in the database instead of deserializing the data whenever you need to inpsect it, as duffymo points out.

If you'd like to know how to serialize the array into BLOB...(this just seems like overkill)

You are able to serialize one-dimensional arrays and jagged arrays, e.g.:

public class Test {
    public static void main(String[] args) throws Exception {

        // Serialize an int[]
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test.ser"));
        out.writeObject(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
        out.flush();
        out.close();

        // Deserialize the int[]
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("test.ser"));
        int[] array = (int[]) in.readObject();
        in.close();

        // Print out contents of deserialized int[]
        System.out.println("It is " + (array instanceof Serializable) + " that int[] implements Serializable");
        System.out.print("Deserialized array: " + array[0]);
        for (int i=1; i<array.length; i++) {
            System.out.print(", " + array[i]);
        }
        System.out.println();
    }
}

As for what data type to store it as in MySQL, there are only four blob types to choose from:
The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB

Choose the best one depends on the size of the serialized object. I'd imagine BLOB would be good enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文