简体   繁体   English

将一个大的 json 文件拆分成多个较小的文件

[英]Split a large json file into multiple smaller files

I have a large JSON file, about 5 million records and a file size of about 32GB, that I need to get loaded into our Snowflake Data Warehouse.我有一个 JSON 的大文件,大约有 500 万条记录,文件大小约为 32GB,我需要将其加载到我们的 Snowflake 数据仓库中。 I need to get this file broken up into chunks of about 200k records (about 1.25GB) per file.我需要将这个文件分解成每个文件大约 200k 条记录(大约 1.25GB)的块。 I'd like to do this in either Node.JS or Python for deployment to an AWS Lambda function, unfortunately I haven't coded in either, yet.我想在 Node.JS 或 Python 中执行此操作以部署到 AWS Lambda function,不幸的是我还没有编码。 I have C# and a lot of SQL experience, and learning both node and python are on my to do list, so why not dive right in, right??我有 C# 和很多 SQL 经验,学习节点和 python 在我的待办事项列表中,所以为什么不直接投入,对吧?

My first question is "Which language would better serve this function? Python, or Node.JS?"我的第一个问题是“哪种语言更适合这个 function?Python,还是 Node.JS?”

I know I don't want to read this entire JSON file into memory (or even the output smaller file).我知道我不想将整个 JSON 文件读入 memory(甚至是 output更小的文件)。 I need to be able to "stream" it in and out into the new file based on a record count (200k), properly close up the json objects, and continue into a new file for another 200k, and so on.我需要能够根据记录数 (200k) 将其“流入”流出到新文件中,正确关闭 json 对象,并继续进入另一个 200k 的新文件,依此类推。 I know Node can do this, but if Python can also do this, I feel like it would be easier to quickly start using for other ETL stuff I'll be doing soon.我知道 Node 可以做到这一点,但如果 Python 也可以做到这一点,我觉得快速开始使用我很快就会做的其他 ETL 事情会更容易。

My second question is "Based on your recommendation above, can you also recommend what modules I should require/import to help me get started? Primarily as it relates to not pulling the entire json file into memory? Maybe some tips, tricks, or 'How would you do it's? And if you're feeling really generous, some code example to help push me into the deep end on this?我的第二个问题是“根据您上面的建议,您是否也可以推荐我应该需要/导入哪些模块来帮助我开始?主要是因为它与不将整个 json 文件拉入 memory 有关?也许一些提示,技巧或'你会怎么做?如果你真的很慷慨,一些代码示例可以帮助我深入研究这个问题?

I can't include a sample of the JSON data, as it contains personal information.我不能包含 JSON 数据的样本,因为它包含个人信息。 But I can provide the JSON schema...但我可以提供 JSON 架构......

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "items": {
    "properties": {
      "activities": {
        "properties": {
          "activity_id": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "frontlineorg_id": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "import_id": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "is_source": {
            "items": {
              "type": "boolean"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "address": {
        "properties": {
          "city": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "congress_dist_name": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "congress_dist_number": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "congress_end_yr": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "congress_number": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "congress_start_yr": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "county": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "formatted": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "latitude": {
            "items": {
              "type": "number"
            },
            "type": "array"
          },
          "longitude": {
            "items": {
              "type": "number"
            },
            "type": "array"
          },
          "number": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "observes_dst": {
            "items": {
              "type": "boolean"
            },
            "type": "array"
          },
          "post_directional": {
            "items": {
              "type": "null"
            },
            "type": "array"
          },
          "pre_directional": {
            "items": {
              "type": "null"
            },
            "type": "array"
          },
          "school_district": {
            "items": {
              "properties": {
                "school_dist_name": {
                  "items": {
                    "type": "string"
                  },
                  "type": "array"
                },
                "school_dist_type": {
                  "items": {
                    "type": "string"
                  },
                  "type": "array"
                },
                "school_grade_high": {
                  "items": {
                    "type": "string"
                  },
                  "type": "array"
                },
                "school_grade_low": {
                  "items": {
                    "type": "string"
                  },
                  "type": "array"
                },
                "school_lea_code": {
                  "items": {
                    "type": "integer"
                  },
                  "type": "array"
                }
              },
              "type": "object"
            },
            "type": "array"
          },
          "secondary_number": {
            "items": {
              "type": "null"
            },
            "type": "array"
          },
          "secondary_unit": {
            "items": {
              "type": "null"
            },
            "type": "array"
          },
          "state": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "state_house_dist_name": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "state_house_dist_number": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "state_senate_dist_name": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "state_senate_dist_number": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "street": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suffix": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "timezone": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "utc_offset": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "zip": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "age": {
        "type": "integer"
      },
      "anniversary": {
        "properties": {
          "date": {
            "type": "null"
          },
          "insert_datetime_utc": {
            "type": "null"
          },
          "suppressed_datetime_utc": {
            "type": "null"
          }
        },
        "type": "object"
      },
      "baptism": {
        "properties": {
          "church_id": {
            "type": "null"
          },
          "date": {
            "type": "null"
          },
          "insert_datetime_utc": {
            "type": "null"
          },
          "suppressed_datetime_utc": {
            "type": "null"
          }
        },
        "type": "object"
      },
      "birth_dd": {
        "type": "integer"
      },
      "birth_mm": {
        "type": "integer"
      },
      "birth_yyyy": {
        "type": "integer"
      },
      "church_attendance": {
        "properties": {
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "likelihood": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "cohabiting": {
        "properties": {
          "confidence": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "likelihood": {
            "items": {
              "type": "null"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "dating": {
        "properties": {
          "bool": {
            "type": "null"
          },
          "insert_datetime_utc": {
            "type": "null"
          },
          "suppressed_datetime_utc": {
            "type": "null"
          }
        },
        "type": "object"
      },
      "divorced": {
        "properties": {
          "bool": {
            "items": {
              "type": "null"
            },
            "type": "array"
          },
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "likelihood_considering": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "education": {
        "properties": {
          "est_level": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "email": {
        "properties": {
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "is_work_school": {
            "items": {
              "type": "boolean"
            },
            "type": "array"
          },
          "string": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "engaged": {
        "properties": {
          "insert_datetime_utc": {
            "type": "null"
          },
          "likelihood": {
            "type": "null"
          },
          "suppressed_datetime_utc": {
            "type": "null"
          }
        },
        "type": "object"
      },
      "est_income": {
        "properties": {
          "est_level": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "ethnicity": {
        "type": "string"
      },
      "first_name": {
        "type": "string"
      },
      "formatted_birthdate": {
        "type": "string"
      },
      "gender": {
        "type": "string"
      },
      "head_of_household": {
        "properties": {
          "bool": {
            "type": "null"
          },
          "insert_datetime_utc": {
            "type": "null"
          },
          "suppressed_datetime_utc": {
            "type": "null"
          }
        },
        "type": "object"
      },
      "home_church": {
        "properties": {
          "church_id": {
            "type": "null"
          },
          "group_participant": {
            "type": "null"
          },
          "insert_datetime_utc": {
            "type": "null"
          },
          "is_coaching": {
            "type": "null"
          },
          "is_giving": {
            "type": "null"
          },
          "is_serving": {
            "type": "null"
          },
          "membership_date": {
            "type": "null"
          },
          "regular_attendee": {
            "type": "null"
          },
          "suppressed_datetime_utc": {
            "type": "null"
          }
        },
        "type": "object"
      },
      "hub_poid": {
        "type": "integer"
      },
      "insert_datetime_utc": {
        "type": "string"
      },
      "ip_address": {
        "properties": {
          "insert_datetime_utc": {
            "type": "null"
          },
          "string": {
            "type": "null"
          },
          "suppressed_datetime_utc": {
            "type": "null"
          }
        },
        "type": "object"
      },
      "last_name": {
        "type": "string"
      },
      "marriage_segment": {
        "properties": {
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "string": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "married": {
        "properties": {
          "bool": {
            "items": {
              "type": "boolean"
            },
            "type": "array"
          },
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "middle_name": {
        "type": "string"
      },
      "miscellaneous": {
        "properties": {
          "attribute": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "value": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "name_suffix": {
        "type": "null"
      },
      "name_title": {
        "type": "null"
      },
      "newlywed": {
        "properties": {
          "bool": {
            "type": "null"
          },
          "insert_datetime_utc": {
            "type": "null"
          },
          "suppressed_datetime_utc": {
            "type": "null"
          }
        },
        "type": "object"
      },
      "parent": {
        "properties": {
          "bool": {
            "items": {
              "type": "boolean"
            },
            "type": "array"
          },
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "likelihood_expecting": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "person_id": {
        "type": "integer"
      },
      "phone": {
        "properties": {
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "number": {
            "items": {
              "type": "integer"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "type": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "property_rights": {
        "properties": {
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "string": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "psychographic_cluster": {
        "properties": {
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "string": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "religion": {
        "properties": {
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "string": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "religious_segment": {
        "properties": {
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "string": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      },
      "separated": {
        "properties": {
          "bool": {
            "type": "null"
          },
          "insert_datetime_utc": {
            "type": "null"
          },
          "suppressed_datetime_utc": {
            "type": "null"
          }
        },
        "type": "object"
      },
      "significant_other": {
        "properties": {
          "first_name": {
            "type": "null"
          },
          "insert_datetime_utc": {
            "type": "null"
          },
          "last_name": {
            "type": "null"
          },
          "middle_name": {
            "type": "null"
          },
          "name_suffix": {
            "type": "null"
          },
          "name_title": {
            "type": "null"
          },
          "suppressed_datetime_utc": {
            "type": "null"
          }
        },
        "type": "object"
      },
      "suppressed_datetime_utc": {
        "type": "string"
      },
      "target_group": {
        "properties": {
          "insert_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "string": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "suppressed_datetime_utc": {
            "items": {
              "type": "string"
            },
            "type": "array"
          }
        },
        "type": "object"
      }
    },
    "type": "object"
  },
  "type": "array"
}

Use this code in linux command prompt在 linux 命令提示符下使用此代码

split -b 53750k <your-file>
cat xa* > <your-file>

Refer to this link: https://askubuntu.com/questions/28847/text-editor-to-edit-large-4-3-gb-plain-text-file请参阅此链接: https : //askubuntu.com/questions/28847/text-editor-to-edit-large-4-3-gb-plain-text-file

Answering the question whether Python or Node will be better for the task would be an opinion and we are not allowed to voice our opinions on Stack Overflow.回答 Python 或 Node 是否更适合该任务的问题将是一种意见,我们不允许对 Stack Overflow 发表意见。 You have to decide yourself what you have more experience in and what you want to work with - Python or Node.你必须自己决定你在什么方面有更多的经验以及你想使用什么——Python 或 Node.js。

If you go with Node, there are some modules that can help you with that task, that do streaming JSON parsing.如果您使用 Node,则有一些模块可以帮助您完成该任务,即流式 JSON 解析。 Eg those modules:例如那些模块:

If you go with Python, there are streaming JSON parsers here as well:如果你使用 Python,这里也有流式 JSON 解析器:

Snowflake has a very special treatment for JSON and if we understand them, it would be easy to draw the design. Snowflake 对 JSON非常特殊的处理,如果我们理解它们,绘制设计就会很容易。

  1. JSON/Parquet/Avro/XML is considered as semi-structure data JSON/Parquet/Avro/XML 被认为是半结构化数据
  2. They are stored as Variant data type in Snowflake.它们在 Snowflake 中存储为 Variant 数据类型。
  3. While loading the JSON data into stage location, flag the strip_outer_array=true在将 JSON 数据加载到舞台位置时,标记 strip_outer_array=true

    copy into <table> from @~/<file>.json file_format = (type = 'JSON' strip_outer_array = true);

  4. Each row size can not exceed 16Mb compressed when loaded in snowflake.在雪花中加载时,每行大小不能超过 16Mb 压缩。

  5. Snowflake data loading works well if the file size is split in the range of 10-100Mb in size.如果文件大小在 10-100Mb 范围内拆分,则雪花数据加载效果很好

Use the utilities which can split the file based on per line and have the file size note more than 100Mb and that brings the power of parallelism as well as accuracy for your data.使用可以基于每行拆分文件并使文件大小注释超过 100Mb 的实用程序,并为您的数据带来并行性和准确性的力量。

As per your data set size, you will get around 31K small files (of 100Mb size).根据您的数据集大小,您将获得大约 31K 个小文件(100Mb 大小)。

  • It means that the 31k parallel process run, however, it is not possible.这意味着31k并行进程运行,但是,这是不可能的。
  • So choose an x-large size warehouse (16 v-core & 32 threads)所以选择一个 x-large 大小的仓库(16 个 v-core & 32 个线程)
  • 31k/32 = (approximately) 1000 rounds 31k/32 =(大约)1000发
  • This will not take more than a few minutes to load data based on your network bandwidth.根据您的网络带宽加载数据不会超过几分钟。 Even if we think of 3sec per round, it may load the data in 50min.即使我们考虑每轮 3 秒,它也可能在 50 分钟内加载数据。

Look at the warehouse configuration & throughput details and refer semi-structured data loading best practice .查看仓库配置和吞吐量详细信息并参考半结构化数据加载最佳实践 在此处输入图片说明 在此处输入图片说明

consider to use jq to preprocessing your json files考虑使用 jq 来预处理你的 json 文件

it could split and stream your large json files它可以拆分和流式传输您的大型 json 文件

jq is like sed for JSON data - you can use it to slice 
and filter and map and transform structured data with 
the same ease that sed, awk, grep and friends let you play with text.

see the official documentation and this questions for more.有关更多信息,请参阅官方文档和此问题

extra: for your first questions jq is written by C, it's faster than python/node isn't it ?额外:对于您的第一个问题,jq 是由 C 编写的,它比 python/node 快,不是吗?

The easiest approach that worked for me was this:对我有用的最简单的方法是:

json_file = <your_file>
chunks = 200
for i in range(0,len(json_file), chunks):
    print(json_file[i:i+chunks])

To split and compress at the same time with bash, resulting in files of ~100MB each:使用 bash 同时拆分和压缩,生成每个约 100MB 的文件:

cat bigfile.json  | split -C 1000000000 -d -a4 - output_prefix --filter='gzip > $FILE.gz'

See more: https://stackoverflow.com/a/68718176/132438查看更多: https : //stackoverflow.com/a/68718176/132438

You can use Python3 with the following script:您可以通过以下脚本使用 Python3:

import json

def split_json(file_path):
    with open(file_path, 'r') as json_file:
        data = json.load(json_file)

    chunk_size = len(data) // 3
    for i in range(3):
        with open(f"part{i}.json", 'w') as outfile:
            outfile.write(json.dumps(data[i*chunk_size:(i+1)*chunk_size]))

file_path = input("Enter the file path of the JSON file: ")
split_json(file_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM