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

2024-04-18 06:38:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大的JSON文件,大约500万条记录,文件大小大约32GB,我需要把它加载到我们的雪花数据仓库中。我需要把这个文件分解成每个文件约200k条记录(约1.25GB)。我想在Node.JS或Python中执行此操作,以便部署到AWS Lambda函数,不幸的是,我还没有在其中编写代码。我有C#和很多SQL经验,学习node和python都在我的任务列表中,所以为什么不直接跳进去呢,对吧!?

我的第一个问题是“哪种语言更适合这个功能?”?或者Node.JS?”

我知道我不想将整个JSON文件读入内存(甚至不想将输出的文件读入更小的文件)。我需要能够根据记录计数(200k)将它“流式”输入输出到新文件中,正确地关闭json对象,然后继续输入新文件再输入200k,依此类推。我知道Node可以做到这一点,但如果Python也可以做到这一点,我觉得很快就可以开始使用其他ETL工具了。

我的第二个问题是“根据你上面的建议,你能推荐我需要/导入哪些模块来帮助我开始吗?主要是因为它与不将整个json文件拉入内存有关?也许是一些提示,技巧,或者“你会怎么做?”?如果你真的很慷慨,一些代码示例可以帮助我深入了解这一点?

我不能包含JSON数据的示例,因为它包含个人信息。但我可以提供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"
}

Tags: 文件namenumberdatetimestringobjecttypeitems
3条回答

在linux命令提示符下使用此代码

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

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

考虑使用jq预处理json文件

它可以分割和流式传输您的大型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.

有关更多信息,请参见official documentation和此questions

额外:对于您的第一个问题,jq是由C编写的,它比python/node快,不是吗?

回答Python或Node是否更适合这个任务的问题是一个意见,我们不允许对堆栈溢出发表意见。你必须决定你在哪些方面有更多的经验,你想用什么来工作-Python或Node。

如果使用Node,则有一些模块可以帮助您完成该任务,它们可以执行流式JSON解析。E、 g.这些单元:

如果使用Python,这里还有流式JSON解析器:

相关问题 更多 >