理想情况下,转换为 json 不应将整个表加载到内存中。但是由于这个已知问题,服务器在表到 json 的转换过程中会出现 OOM。该修复程序将很快在即将发布的版本中提供。
您的用例是否迭代表并将每一行转换为 json?如果是这种情况,一旦上述问题得到解决,您应该能够按照以下方式进行操作,而不会填满内存。
import ballerina/io;
import ballerina/mysql;
endpoint mysql:Client testDB {
host: "localhost",
port: 3306,
name: "testdb",
username: "root",
password: "123",
poolOptions: { maximumPoolSize: 5 },
dbOptions: { useSSL: false }
};
function main(string... args) {
var selectRet = testDB->select("SELECT * FROM employee", ());
table dt;
match selectRet {
table tableReturned => dt = tableReturned;
error err => io:println("Select data from the table failed: "
+ err.message);
}
var ret = <json>dt;
json jsonData;
match ret {
json j => jsonData = j;
error e => io:println("Error occurred while converting the table to json" + e.message);
}
foreach j in jsonData {
match j {
string js => {
io:println("string value: ", js);
}
json jx => {
io:println("non-string value: ", jx);
}
}
}
}