Protovis - 处理文本源

发布于 2024-11-27 18:18:08 字数 1102 浏览 0 评论 0原文

假设我有一个文本文件,其中包含这样的行:

[4/20/11 17:07:12:875 CEST] 00000059 FfdcProvider  W com.test.ws.ffdc.impl.FfdcProvider logIncident FFDC1003I: FFDC Incident emitted on D:/Prgs/testing/WebSphere/AppServer/profiles/ProcCtr01/logs/ffdc/server1_3d203d20_11.04.20_17.07.12.8755227341908890183253.txt com.test.testserver.management.cmdframework.CmdNotificationListener 134
[4/20/11 17:07:27:609 CEST] 0000005d wle           E   CWLLG2229E: An exception occurred in an EJB call.  Error: Snapshot with ID Snapshot.8fdaaf3f-ce3f-426e-9347-3ac7e8a3863e not found.
                             com.lombardisoftware.core.TeamWorksException: Snapshot with ID Snapshot.8fdaaf3f-ce3f-426e-9347-3ac7e8a3863e not found.
   at com.lombardisoftware.server.ejb.persistence.CommonDAO.assertNotNull(CommonDAO.java:70)

无论如何,是否可以轻松地将这样的数据源导入到 protovis 中,如果没有,将其解析为 JSON 格式的最简单方法是什么。例如,第一个条目可能会像这样解析:

[
  {
 "Date": "4/20/11 17:07:12:875 CEST",
 "Status": "00000059",
 "Msg": "FfdcProvider  W com.test.ws.ffdc.impl.FfdcProvider logIncident FFDC1003I",
 },
]

谢谢,大卫

lets say I have a text file with lines as such:

[4/20/11 17:07:12:875 CEST] 00000059 FfdcProvider  W com.test.ws.ffdc.impl.FfdcProvider logIncident FFDC1003I: FFDC Incident emitted on D:/Prgs/testing/WebSphere/AppServer/profiles/ProcCtr01/logs/ffdc/server1_3d203d20_11.04.20_17.07.12.8755227341908890183253.txt com.test.testserver.management.cmdframework.CmdNotificationListener 134
[4/20/11 17:07:27:609 CEST] 0000005d wle           E   CWLLG2229E: An exception occurred in an EJB call.  Error: Snapshot with ID Snapshot.8fdaaf3f-ce3f-426e-9347-3ac7e8a3863e not found.
                             com.lombardisoftware.core.TeamWorksException: Snapshot with ID Snapshot.8fdaaf3f-ce3f-426e-9347-3ac7e8a3863e not found.
   at com.lombardisoftware.server.ejb.persistence.CommonDAO.assertNotNull(CommonDAO.java:70)

Is there anyway to easily import a data source such as this into protovis, if not what would the easiest way to parse this into a JSON format. For example for the first entry might be parsed like so:

[
  {
 "Date": "4/20/11 17:07:12:875 CEST",
 "Status": "00000059",
 "Msg": "FfdcProvider  W com.test.ws.ffdc.impl.FfdcProvider logIncident FFDC1003I",
 },
]

Thanks, David

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

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

发布评论

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

评论(1

素染倾城色 2024-12-04 18:18:08

Protovis 本身不提供任何用于解析文本文件的实用程序,因此您的选择是:

  • 使用 Javascript 将文本解析为对象,最有可能使用正则表达式。
  • 使用您选择的文本解析语言或实用程序预处理文本,导出 JSON 文件。

您选择哪个取决于几个因素:

  • 数据是否有些静态,或者您每次查看数据时都会在新文件或动态文件上运行它?对于静态数据,预处理可能是最简单的;对于动态数据,这可能会增加一个烦人的额外步骤。

  • 您有多少数据?用 Javascript 解析 20K 文本文件完全没问题;解析 2MB 文件会非常慢,并且会导致浏览器在工作时挂起(除非您使用 Workers)。

  • 如果涉及大量处理,您愿意将该负载放在服务器上(通过使用服务器端脚本进行预处理)还是放在客户端上(通过在浏览器中执行)?

如果您想在 Javascript 中执行此操作,根据您提供的示例,您可能会执行以下操作:

// Assumes var text = 'your text';
// use the utility of your choice to load your text file into the
// variable (e.g. jQuery.get()), or just paste it in.
var lines = text.split(/[\r\n\f]+/),
    // regex to match your log entry beginning
    patt = /^\[(\d\d?\/\d\d?\/\d\d? \d\d:\d\d:\d\d:\d{3} [A-Z]+)\] (\d{8})/,
    items = [],
    currentItem;

// loop through the lines in the file
lines.forEach(function(line) {
    // look for the beginning of a log entry
    var initialData = line.match(patt);
    if (initialData) {
        // start a new item, using the captured matches
        currentItem = {
            Date: initialData[1],
            Status: initialData[2],
            Msg: line.substr(initialData[0].length + 1)
        }
        items.push(currentItem);
    } else {
        // this is a continuation of the last item
        currentItem.Msg += "\n" + line;  
    }
});

// items now contains an array of objects with your data

Protovis itself doesn't offer any utilities for parsing text files, so your options are:

  • Use Javascript to parse the text into an object, most likely using regex.
  • Pre-process the text using the text-parsing language or utility of your choice, exporting a JSON file.

Which you choose depends on several factors:

  • Is the data somewhat static, or are you going to be running this on a new or dynamic file each time you look at it? With static data, it might be easiest to pre-process; with dynamic data, this may add an annoying extra step.

  • How much data do you have? Parsing a 20K text file in Javascript is totally fine; parsing a 2MB file will be really slow, and will cause the browser to hang while it's working (unless you use Workers).

  • If there's a lot of processing involved, would you rather put that load on the server (by using a server-side script for pre-processing) or on the client (by doing it in the browser)?

If you wanted to do this in Javascript, based on the sample you provided, you might do something like this:

// Assumes var text = 'your text';
// use the utility of your choice to load your text file into the
// variable (e.g. jQuery.get()), or just paste it in.
var lines = text.split(/[\r\n\f]+/),
    // regex to match your log entry beginning
    patt = /^\[(\d\d?\/\d\d?\/\d\d? \d\d:\d\d:\d\d:\d{3} [A-Z]+)\] (\d{8})/,
    items = [],
    currentItem;

// loop through the lines in the file
lines.forEach(function(line) {
    // look for the beginning of a log entry
    var initialData = line.match(patt);
    if (initialData) {
        // start a new item, using the captured matches
        currentItem = {
            Date: initialData[1],
            Status: initialData[2],
            Msg: line.substr(initialData[0].length + 1)
        }
        items.push(currentItem);
    } else {
        // this is a continuation of the last item
        currentItem.Msg += "\n" + line;  
    }
});

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