将数据挖掘 SQL 查询转换为 Grails 等效项?

发布于 2024-10-30 10:47:56 字数 694 浏览 8 评论 0原文

我想构建一个网络界面来呈现日志数据。这应该是一个尝试使用 Grails 构建一个小型 Web 应用程序的好机会,我一直想尝试一下,但我在理解如何将今天的手动 sql 查询“翻译”为可用于 Grails 的东西。

我在《Grails In Action》一书中没有找到太多关于将现有数据表改造到域类上的信息。所以我把它带到这里来进行一些澄清:)

这基本上是我今天记录到的日志表的模式:

LOG(id,method,timestamp,millis_used,username,hostname,...etc)

我可以看到自己在做一些事情,比如使用 hasMany = {logs: Log } 和一个带有 belongsTo = { user: User } 的 Log 类,但不是我如何有效地使用它来查询我的数据,特别是在处理数十万个数据时记录行。我通常对数据进行查询,例如 “查找过去 30 天用于 method='fooBar' 和 user='john_doe' 的平均时间”“计算其中的行数5 月至 12 月,method='fooBaz' 和 host='localhost'”

您将如何检索这样的信息?您是否会忘记映射日志条目,而只是在表上使用某种直接 SQL(HQL?)查询,或者这个(我不知道)GORM 野兽可以用于类似的事情吗?

I would like to build a web interface to present logdata. This should be a nice opportunity to try to build a little web app using Grails, which I have been wanting to try out for quite a while, but I am having some trouble understanding how I can "translate" the manual sql queries of today into something usable for Grails.

I have not found too much info on retrofitting existing data tables onto domain classes in the book Grails In Action. So I am bringing it here for some clarification :)

This is basically the schema for the log table I am logging to today:

LOG(id,method,timestamp,millis_used,username,hostname,...etc)

I can see myself doing something like creating domain classes User and Host with mappings like hasMany = { logs: Log } and a Log class with belongsTo = { user: User }, but not how I can use this in a effective way to query my data, especially if dealing with hundred of thousands of log rows. I am usually doing queries on the data like "find the average time used for method='fooBar' and user='john_doe' the last 30 days" or "count the number of rows where method='fooBaz' and host='localhost' from May to December".

How would you go about to retrieve info like this? Would you just forget about mapping the log entries and just use some kind of direct SQL (HQL?) queries on the tables or can this (unknown to me) GORM beast be used for stuff like this?

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

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

发布评论

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

评论(2

睫毛上残留的泪 2024-11-06 10:47:56

首先,我同意Ted的回答。在设置 GORM 域之前,请查看 Burt 的演示

其次,我建议您查看条件。用于 Hibernate Criteria 功能的 grails DSL 构建了一个非常干净且可维护的代码库。以下是一些示例:

示例标准:

def avg = Log.createCriteria().get {
    projections {
        avg 'duration'
    }
    user {
        eq 'userName', 'user1'
    }
}
println("Average = ${avg}")

示例域对象:

class User {

    String userName
    String firstName
    String lastName

    static constraints = {
        userName nullable:false, maxSize:32
        firstName nullable:false, maxSize:50
        lastName nullable:false, maxSize:50
    }

}

class Host {

    String hostname

    static mapping = {
        version false
    }

    static constraints = {
        hostname nullable:false, maxSize:64
    }
}

class Log {

    Host host
    User user
    String method
    String logMessage
    Date dateCreated
    long duration

    static mapping = {
        autoTimestamp true  //Note: this will automatically update dateCreated and lastUpdate
        version false
    }

    static constraints = {
        host nullable:false
        user nullable:false
        method nullable:false, maxSize:50
        logMessage nullable:false, maxSize:255
        duration nullable:false
    }
}

First, I agree with Ted's response. Check out Burt's presentation before you setup your GORM domains.

Second, I recommend you take a look at Criterias. The grails DSL for the Hibernate Criteria functionality makes for a very clean and maintainable codebase. Here are some examples:

Example Criteria:

def avg = Log.createCriteria().get {
    projections {
        avg 'duration'
    }
    user {
        eq 'userName', 'user1'
    }
}
println("Average = ${avg}")

Example Domain Objects:

class User {

    String userName
    String firstName
    String lastName

    static constraints = {
        userName nullable:false, maxSize:32
        firstName nullable:false, maxSize:50
        lastName nullable:false, maxSize:50
    }

}

class Host {

    String hostname

    static mapping = {
        version false
    }

    static constraints = {
        hostname nullable:false, maxSize:64
    }
}

class Log {

    Host host
    User user
    String method
    String logMessage
    Date dateCreated
    long duration

    static mapping = {
        autoTimestamp true  //Note: this will automatically update dateCreated and lastUpdate
        version false
    }

    static constraints = {
        host nullable:false
        user nullable:false
        method nullable:false, maxSize:50
        logMessage nullable:false, maxSize:255
        duration nullable:false
    }
}
小矜持 2024-11-06 10:47:56

由于每个用户或每个主机可能有尽可能多的记录,我不会使用 hasMany 关系。日志记录往往写入量很大,并且维护每个成员的日志集/列表会产生成本,这是不值得的。

HQL 可能是您最好的选择。您可以将其设计得非常像本机 SQL。

Burt Beckwith 有一个很棒的演示,讨论了 grails 与 GORM 的一些性能,值得您花时间观看:http://www.infoq.com/presentations/GORM-Performance

With as many records as you're likely to have for logging per user or per host, I wouldn't use a hasMany relationship. Logging tends to be write heavy and there are costs to maintaining the set/list of logs per member that wouldn't be worth it.

HQL is probably your best bet. You can craft it to look a lot like native SQL.

Burt Beckwith has a great presentation that talks about some of the performance in grails with GORM that'd be worth your time to watch: http://www.infoq.com/presentations/GORM-Performance

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