将数据挖掘 SQL 查询转换为 Grails 等效项?
我想构建一个网络界面来呈现日志数据。这应该是一个尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我同意Ted的回答。在设置 GORM 域之前,请查看 Burt 的演示。
其次,我建议您查看条件。用于 Hibernate Criteria 功能的 grails DSL 构建了一个非常干净且可维护的代码库。以下是一些示例:
示例标准:
示例域对象:
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:
Example Domain Objects:
由于每个用户或每个主机可能有尽可能多的记录,我不会使用 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