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

时间:2017-03-28 15:48:02

标签: python json node.js lambda snowflake-datawarehouse

我有一个大型JSON文件,大约500万条记录,文件大小约为32GB,我需要加载到Snowflake数据仓库中。我需要将此文件分解为每个文件大约200k条记录(大约1.25GB)的块。我想在Node.JS或Python中执行此操作以部署到AWS Lambda函数,遗憾的是我还没有编写任何代码。我有C#和很多SQL经验,学习节点和python都在我的待办事项清单上,所以为什么不直接潜入,对吧!?

我的第一个问题是"哪种语言更适合这个功能? 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"
}

4 个答案:

答案 0 :(得分:4)

在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

答案 1 :(得分:2)

回答Python或Node是否会更好地完成任务的问题将是一个意见,我们不允许在Stack Overflow上发表意见。你必须自己决定你有更多的经验和想要使用的东西--Python或Node。

如果您使用Node,有一些模块可以帮助您完成该任务,即进行流式JSON解析。例如。那些模块:

如果你使用Python,那么这里也有流式JSON解析器:

答案 2 :(得分:0)

考虑使用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快吗?

答案 3 :(得分:0)

雪花有一个very special treatment for JSON,如果我们理解它们,就可以很容易地绘制出设计。

  1. JSON / Parquet / Avro / XML被视为半结构数据
  2. 它们被存储为Snowflake中的Variant数据类型。
  3. 在将JSON数据加载到舞台位置的同时,标记strip_outer_array = true

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

  4. 加载到雪花中时,每行大小不得超过16Mb。

  5. 如果文件大小在10-100Mb范围内分割,则雪花数据加载works well

使用utilities,它可以基于每行拆分文件,并且文件大小注释超过100Mb,这为数据带来了并行性和准确性。

根据您的数据集大小,您将获得约31K个小文件(大小为100Mb)。

  • 这意味着31k并行进程运行,但是,这是不可能的。
  • 因此,选择大型x仓库(16个v核和32个线程)
  • 31k / 32 =(大约)1000发
  • 根据您的网络带宽,加载数据所需的时间不会超过几分钟。即使我们认为每轮3秒,也可能会在50分钟内加载数据。

查看仓库配置和throughput详细信息,然后参考semi-structured data loading best practiceenter image description here enter image description here