在 PowerShell 中解析大型 JSON 文件

2024-04-29

Context

在这篇文章中:

ConvertFrom-Json 大文件 https://stackoverflow.com/q/76784490/268581

我询问有关反序列化 1.2GB JSON 文件的问题。

这个答案发布在那里:

https://stackoverflow.com/a/76791900/268581 https://stackoverflow.com/a/76791900/268581

确实有效,但速度非常慢。

样本数据

这样您就不必使用 1.2GB 文件,这里有一个用于解决此问题的小数据示例。这只是原始大型 JSON 文件中的前几项。

example.json:

[{"py/object": "polygon.websocket.models.models.EquityTrade", "event_type": "T", "symbol": "O:AMD230728C00115000", "exchange": 304, "id": null, "tape": null, "price": 0.38, "size": 1, "conditions": [227], "timestamp": 1690471217275, "sequence_number": 1477738810, "trf_id": null, "trf_timestamp": null}, {"py/object": "polygon.websocket.models.models.EquityTrade", "event_type": "T", "symbol": "O:AFRM230728C00019500", "exchange": 302, "id": null, "tape": null, "price": 0.07, "size": 10, "conditions": [209], "timestamp": 1690471217278, "sequence_number": 1477739110, "trf_id": null, "trf_timestamp": null}, {"py/object": "polygon.websocket.models.models.EquityTrade", "event_type": "T", "symbol": "O:TSLA230804C00270000", "exchange": 325, "id": null, "tape": null, "price": 4.8, "size": 7, "conditions": [219], "timestamp": 1690471217282, "sequence_number": 341519150, "trf_id": null, "trf_timestamp": null}, {"py/object": "polygon.websocket.models.models.EquityTrade", "event_type": "T", "symbol": "O:TSLA230804C00270000", "exchange": 312, "id": null, "tape": null, "price": 4.8, "size": 1, "conditions": [209], "timestamp": 1690471217282, "sequence_number": 341519166, "trf_id": null, "trf_timestamp": null}, {"py/object": "polygon.websocket.models.models.EquityTrade", "event_type": "T", "symbol": "O:TSLA230804C00270000", "exchange": 312, "id": null, "tape": null, "price": 4.8, "size": 1, "conditions": [209], "timestamp": 1690471217282, "sequence_number": 341519167, "trf_id": null, "trf_timestamp": null}, {"py/object": "polygon.websocket.models.models.EquityTrade", "event_type": "T", "symbol": "O:TSLA230804C00270000", "exchange": 319, "id": null, "tape": null, "price": 4.8, "size": 5, "conditions": [219], "timestamp": 1690471217282, "sequence_number": 341519170, "trf_id": null, "trf_timestamp": null}, {"py/object": "polygon.websocket.models.models.EquityTrade", "event_type": "T", "symbol": "O:TSLA230804C00270000", "exchange": 312, "id": null, "tape": null, "price": 4.8, "size": 19, "conditions": [209], "timestamp": 1690471217284, "sequence_number": 341519682, "trf_id": null, "trf_timestamp": null}, {"py/object": "polygon.websocket.models.models.EquityTrade", "event_type": "T", "symbol": "O:TSLA230804C00270000", "exchange": 301, "id": null, "tape": null, "price": 4.8, "size": 2, "conditions": [219], "timestamp": 1690471217290, "sequence_number": 341519926, "trf_id": null, "trf_timestamp": null}, {"py/object": "polygon.websocket.models.models.EquityTrade", "event_type": "T", "symbol": "O:TSLA230804C00270000", "exchange": 301, "id": null, "tape": null, "price": 4.8, "size": 15, "conditions": [219], "timestamp": 1690471217290, "sequence_number": 341519927, "trf_id": null, "trf_timestamp": null}, {"py/object": "polygon.websocket.models.models.EquityTrade", "event_type": "T", "symbol": "O:META230728C00315000", "exchange": 302, "id": null, "tape": null, "price": 4.76, "size": 1, "conditions": [227], "timestamp": 1690471217323, "sequence_number": 1290750877, "trf_id": null, "trf_timestamp": null}]

Code

这是有效的(缓慢的)代码。 1.2GB 文件运行需要几个小时。

$path = ".\example.json"

$stream = [System.IO.File]::Open($path, [System.IO.FileMode]::Open)

$i = 0
$stream.ReadByte() # read '['
$i++

$json = ''

$data = @()

while ($i -lt $stream.Length)
{
    $byte = $stream.ReadByte(); $i++

    $char = [Convert]::ToChar($byte)
            
    if ($char -eq '}')
    {
        $json = $json + [Convert]::ToChar($byte)
        
        $data = $data + ($json | ConvertFrom-Json)

        $json = ''

        $stream.ReadByte() | Out-Null # read comma;
        $i++

        if ($data.Count % 100 -eq 0)
        {
            Write-Host $data.Count
        }
    }
    else
    {
        $json = $json + [Convert]::ToChar($byte)
    }
}

$stream.Close()

运行后,你应该有记录$data:

PS C:\Users\dharm\Dropbox\Documents\polygon-io.ps1> $data | ft *

py/object                                   event_type symbol                exchange id tape price size conditions     timestamp sequence_number trf_id trf_timestamp
---------                                   ---------- ------                -------- -- ---- ----- ---- ----------     --------- --------------- ------ -------------
polygon.websocket.models.models.EquityTrade T          O:AMD230728C00115000       304          0.38    1 {227}      1690471217275      1477738810
polygon.websocket.models.models.EquityTrade T          O:AFRM230728C00019500      302          0.07   10 {209}      1690471217278      1477739110
polygon.websocket.models.models.EquityTrade T          O:TSLA230804C00270000      325           4.8    7 {219}      1690471217282       341519150
polygon.websocket.models.models.EquityTrade T          O:TSLA230804C00270000      312           4.8    1 {209}      1690471217282       341519166
polygon.websocket.models.models.EquityTrade T          O:TSLA230804C00270000      312           4.8    1 {209}      1690471217282       341519167
polygon.websocket.models.models.EquityTrade T          O:TSLA230804C00270000      319           4.8    5 {219}      1690471217282       341519170
polygon.websocket.models.models.EquityTrade T          O:TSLA230804C00270000      312           4.8   19 {209}      1690471217284       341519682
polygon.websocket.models.models.EquityTrade T          O:TSLA230804C00270000      301           4.8    2 {219}      1690471217290       341519926
polygon.websocket.models.models.EquityTrade T          O:TSLA230804C00270000      301           4.8   15 {219}      1690471217290       341519927
polygon.websocket.models.models.EquityTrade T          O:META230728C00315000      302          4.76    1 {227}      1690471217323      1290750877

Question

有什么好方法可以提高效率?

Notes

这个答案:

https://stackoverflow.com/a/43747641/268581 https://stackoverflow.com/a/43747641/268581

确实说明了使用 Newtonsoft Json.NET 的 C# 方法。

这是它的代码:

JsonSerializer serializer = new JsonSerializer();
MyObject o;
using (FileStream s = File.Open("bigfile.json", FileMode.Open))
using (StreamReader sr = new StreamReader(s))
using (JsonReader reader = new JsonTextReader(sr))
{
    while (reader.Read())
    {
        // deserialize only when there's "{" character in the stream
        if (reader.TokenType == JsonToken.StartObject)
        {
            o = serializer.Deserialize<MyObject>(reader);
        }
    }
}

一种方法是下载 Newtonsoft Json.NET DLL,并将其转换为 PowerShell。一个挑战是这一行:

o = serializer.Deserialize<MyObject>(reader);

正如您所看到的,它正在进行通用方法调用。我不清楚如何将其转换为 Windows PowerShell 5.1。

仅依赖于本机 JSON 反序列化库的解决方案将是首选,但如有必要,Newtonsoft 方法也是可以接受的。


这是适用于 PowerShell 7.3.6 的版本。

只需几分钟,这对于我的情况来说是可以接受的。

Add-Type -Path 'C:\Users\dharm\OneDrive\Documents\WindowsPowerShell\Modules\newtonsoft.json\1.0.2.201\libs\Newtonsoft.Json.dll'

class Row {
    [string]$symbol
    [Int64]$timestamp
}

$serializer = [Newtonsoft.Json.JsonSerializer]::new()

# $stream = [System.IO.File]::Open("..\polygon-io.py\data-2023-07-27-13-33-26.json", [System.IO.FileMode]::Open)

$stream = [System.IO.File]::Open("C:\Users\dharm\Dropbox\Documents\polygon-io.py\data-2023-07-27-13-33-26.json", [System.IO.FileMode]::Open)

$reader = [System.IO.StreamReader]::new($stream)

$json_reader = [Newtonsoft.Json.JsonTextReader]::new($reader)

$ls = New-Object System.Collections.Generic.List[Row]

$i = 0

while ($json_reader.Read())
{
    if ($i % 1000 -eq 0) { Write-Host $i }
    
    if ($json_reader.TokenType -eq [Newtonsoft.Json.JsonToken]::StartObject)
    {        
        $obj = $serializer.Deserialize[Row]($json_reader)
        
        $ls.Add($obj)
    }

    $i++
}

$stream.Close()
$reader.Close()
$json_reader.Close()

# ----------------------------------------------------------------------
# examine result
# ----------------------------------------------------------------------

$ls.Count

$ls | Select-Object -First 10 | ft *
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 PowerShell 中解析大型 JSON 文件 的相关文章

随机推荐

  • 创建 XML 文档的最佳 .net 方法

    我试图找出编写 XML 文档的最佳方法 下面是我尝试根据从 ERP 系统中提取的数据创建的简单示例 我读过有关 XMLWriter 的内容 但我想看看是否还有其他更好的方法 任何建议将不胜感激 XML 示例
  • 为什么我的 .git 文件很大?

    我不小心在 git 目录中添加并提交了一些非常大 100MB 的 PSD 文件 当这些文件位于目录中时 我对它们进行了一系列编辑 但后来意识到它们不应该在那里 并将它们从目录中删除 然后我跑了 git add all git commit
  • 从现有数组创建新结果

    如果我有一个如下所示的示例数据 我需要从结果数组中获取 FinalResult 数组 let result type Science link www educatorsector com type Sports News link www
  • 当数据库更新时,WTForms 中的选择验证不会更新

    我明白了SelectFieldWTForms 中的方法采用 can 参数choices其形式为 choices value1 display of value 1 value2 display of value 2 我需要根据对数据库的调用
  • 未定义不是对象(评估“React.PropTypes.Number”)错误

    刚刚更新自expo 21 0 0 to 22 0 0我收到此错误 这是我的 package json name APP version 0 1 0 private true devDependencies react native scri
  • 无法在内部存储中找到保存到文本文件的数据,如何在内部存储android studio中保存文件?

    我想在我的 Android 手机上创建一个文本文件并在其中写入一些文本 当我点击按钮时 它说saved to data user 0 com example savetotextfile files example txt 但我找不到这些文
  • Python“str”对象没有属性“read”

    Python 3 3 2 导入 json 和 urllib request Json link www google com orderid 100000222 link www google com orderid 100000222 l
  • 将 SSIM 损失函数与 Keras 结合使用

    我需要使用 Sewar 的 SSIM 作为损失函数 以便比较我的模型的图像 当我尝试编译模型时出现错误 我导入该函数并编译模型 如下所示 from sewar full ref import ssim model compile ssim
  • 滚动平均 pandas DataFrame 的所有值

    我有一个 pandas DataFrame 我想在滚动的基础上计算所有值的平均值 对于所有列 对于滚动窗口中的所有观察值 我有一个带循环的解决方案 但感觉效率很低 请注意 我可以有NaNs在我的数据中 因此计算总和并除以窗口的形状并不安全
  • 将升级锁排除在 N3568 提案之外的原因是什么

    我对此进行了一些谷歌搜索 但只找到了很少的信息 N3568 http www open std org jtc1 sc22 wg21 docs papers 2013 n3568 html Wording包括升级锁概念的规范 但升级部件当时
  • groovy 中具有默认值的命名参数

    是否可以在 groovy 中使用具有默认值的命名参数 我的计划是创建一种对象工厂 可以在没有任何参数的情况下调用它 以获得具有默认值的对象 另外 我需要显式设置对象的任何参数的功能 例如 我相信这可以通过 Python 关键字参数实现 我现
  • Java 相当于 C# 中带有 @ 的逐字字符串

    快问 Java 中是否存在应用于字符串的 等效项 例如我可以做 c afolder afile 在 C 中 让它在处理时忽略转义字符 而不必这样做 c afolder aFile Java 有等效的吗 嗯 stackoverflow 正在逃
  • 清单合并失败:属性 application@appComponentFactory

    一切都很好 但我正在尝试添加这个库https github com wdullaer MaterialDateTimePicker https github com wdullaer MaterialDateTimePicker有了这个 i
  • 增加字符串的最后一个字母

    这是我希望 Java 的 String 类有一个 ReplaceLast 方法的地方 但它没有 而且我的代码得到了错误的结果 我正在编写一个程序 该程序在数据结构中搜索与字符串前缀匹配的任何项目 但是 由于我使用的是迭代器 iter nex
  • Spring部署期间依赖注入问题

    我正在启动一个 Primefaces Spring Hibernate 项目 并且仍在学习如何正确处理这些组件 但就在此时 我面临着一个与 spring 依赖注入相关的问题 这让我很害怕 我已经在网上寻找答案两天了 但找不到我的代码有什么问
  • Groovy hasProperty/respondTo

    All the examples http mrhaki blogspot com 2009 10 groovy goodness check if method or html我看到使用hasProperty or respondsTo显
  • React Datepicker(无法获取输入值)

    我是反应新手 我需要使用反应日期选择器 https github com Hacker0x01 react datepicker 当我更改日期时 我想获取输入的值 如果我点击 2017 年 10 月 20 日 我想将 2017 年 10 月
  • 在 PLSQL Oracle 中抛出特定错误消息...在休眠中捕获?

    是否可以在 PL SQL oracle 存储过程中抛出特定的错误消息 并在调用它时在 Hibernate 中捕获它 您可以从 PL SQL 代码中抛出用户定义的错误消息 20000 到 20999 之间的错误代码保留用于用户指定的错误消息
  • 检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到形状为 (800, 1000) 的数组

    我正在尝试使用 CNN 进行情感分析 我的代码我的数据具有 1000 1000 形状 当我将数据传递给 convolution2D 时 它会抛出一个错误 我无法解决 我尝试了以下解决方案 但仍然面临问题 在构建 CNN 时 我收到 Kera
  • 在 PowerShell 中解析大型 JSON 文件

    Context 在这篇文章中 ConvertFrom Json 大文件 https stackoverflow com q 76784490 268581 我询问有关反序列化 1 2GB JSON 文件的问题 这个答案发布在那里 https