当解析包含从上到下一行嵌套项的大容量操作JSONL文件时,当我到达一个新的顶级父对象时,这是否意味着我已经遍历了上一个父对象的所有子对象?
上下文
在处理大容量操作JSONL文件时,我做了一些需要父级和所有子级的处理。我希望保持我的内存需求尽可能小,所以我需要知道什么时候我完成了处理一个对象及其所有子对象。
澄清示例
使用文档页 JSONL示例:
{"id":"gid://shopify/Product/1921569226808"}
{"id":"gid://shopify/ProductVariant/19435458986123","title":"52","__parentId":"gid://shopify/Product/1921569226808"}
{"id":"gid://shopify/ProductVariant/19435458986040","title":"70","__parentId":"gid://shopify/Product/1921569226808"}
{"id":"gid://shopify/Product/1921569259576"}
{"id":"gid://shopify/ProductVariant/19435459018808","title":"34","__parentId":"gid://shopify/Product/1921569259576"}
{"id":"gid://shopify/Product/1921569292344"}
{"id":"gid://shopify/ProductVariant/19435459051576","title":"Default Title","__parentId":"gid://shopify/Product/1921569292344"}
{"id":"gid://shopify/Product/1921569325112"}
{"id":"gid://shopify/ProductVariant/19435459084344","title":"36","__parentId":"gid://shopify/Product/1921569325112"}
{"id":"gid://shopify/Product/1921569357880"}
{"id":"gid://shopify/ProductVariant/19435459117112","title":"47","__parentId":"gid://shopify/Product/1921569357880"}如果我从上到下逐行读取文件,并在第4行按下id gid://shopify/Product/1921569259576的Product,这是否意味着我已经看到了JSONL文件包含的所有上一个产品的(gid://shopify/Product/1921569226808)产品变体?
发布于 2022-11-07 20:53:41
很多人所做的就是使用tac来反转文件。然后,当解析该文件时,首先很好地处理子文件,然后知道何时命中父文件,您拥有所有东西,并且可以移动。
显然,这比让父母,然后孩子,然后想知道,我是否打了所有的孩子,还是更多。
试试看!它起作用了!
我的伪代码(您可以将其转换为任何脚本语言)如下所示:
inventory_file = Tempfile.new
inventory_file.binmode
uri = URI(result.data.node.url)
IO.copy_stream(uri.open, inventory_file) # store large amount of JSON Lines data in a tempfile
inventory_file.rewind # move from EOF to beginning of file
y = "#{Rails.root}/tmp/#{shop_domain}.reversed.jsonl"
`tac #{inventory_file.path} > #{y}`
puts "Completed Reversal of file using tac, so now we can quickly iterate the inventory"
f = File.foreach(y)
variants = {}
f.each_entry do |line|
data = JSON.parse(line)
# play with my data
end
https://stackoverflow.com/questions/74352569
复制相似问题