【问题标题】:JDBC select queryJDBC 选择查询
【发布时间】:2018-07-23 11:27:24
【问题描述】:

如何在不指定记录类型的情况下SELECT * FROM table 然后迭代结果(未加载到内存中,我们的表很大)?

我需要的是逐行迭代,同时将每一行转换为JSON

我基本上想做这样的事情:

var selectRet  = testdb->select("SELECT * FROM some_table", ());
.
.
.
foreach row in tb { io:println(<json> row);}

在学习了 `ballerina.io 文档一周后,如果不首先使用类型行记录 { ..... } 指定确切的 ROW 结构,我仍然无法完成此操作,当您的表有 200 个时,这非常不方便列。

谢谢

【问题讨论】:

    标签: ballerina


    【解决方案1】:

    理想情况下,转换为 json 不应将整个表加载到内存中。但是由于这个known issue 服务器在表到 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);
                }
            }
        }
    }
    

    【讨论】:

    • 是的,正是...尝试将每一行转换为 json ,进行一些转换并输出到不同的数据库。感谢您的示例,除了 OOM 问题外,它运行良好。希望尽快修复。非常感谢。
    【解决方案2】:

    您可以将选择查询返回的表转换为 JSON,而无需将表转换为记录数组。看看下面的示例。我尝试使用最新版本的 Ballerina 0.980.1。我在这里使用了示例员工数据库。

    // This function returns an optional type 'error?' 
    function performSelect() returns error? {
        endpoint mysql:Client testDB {
            host: "localhost",
            port: 3306,
            name: "employees",
            username: "root",
            password: "root12345678",
            poolOptions: { maximumPoolSize: 5 },
            dbOptions: { useSSL: false, allowPublicKeyRetrieval:true, serverTimezone:"UTC" }
        };
    
        // If the select query results in an error, 
        // then the 'check' operator returns it to the caller of this function 
        table resultTable = check testDB->select("SELECT * FROM employees", ());
    
        // convert the table to a json object
        json resultJson = check <json>resultTable;
        io:println(resultJson);
        testDB.stop();
    
        // Return nil since there are no errors occurred
        return ();
    }
    

    【讨论】:

    • 感谢 Sameera,我知道这个解决方案,但它占用了 RAM。据我所知,这会立即转换整个结果并保存在内存中。如果你有 200GB 的表,那就不好了。有没有可以迭代的解决方案?
    猜你喜欢
    • 2011-08-27
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 2017-08-28
    相关资源
    最近更新 更多