GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Java-learning/docs/mass-data/3-find-same-url.md at master · Tyson0314/Java-learning

Expand file tree

/

3-find-same-url.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
41 lines (28 loc) · 1.86 KB
/

3-find-same-url.md

Copy path

File metadata and controls

41 lines (28 loc) · 1.86 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions
sidebar heading
title 查找两个大文件共同的URL
category 海量数据
tag
海量数据
head
meta
name content
keywords
海量数据面试题,大文件共同的URL,海量数据
meta
name content
description
海量数据常见面试题总结,让天下没有难背的八股文!

查找两个大文件共同的URL

题目

给定 a、b 两个文件,各存放 50 亿个 URL,每个 URL 各占 64B,找出 a、b 两个文件共同的 URL。内存限制是 4G。

分析

每个 URL 占 64B,那么 50 亿个 URL占用的空间大小约为 320GB。

5,000,000,000 * 64B ≈ 320GB

由于内存大小只有 4G,因此,不可能一次性把所有 URL 加载到内存中处理。

可以采用分治策略,也就是把一个文件中的 URL 按照某个特征划分为多个小文件,使得每个小文件大小不超过 4G,这样就可以把这个小文件读到内存中进行处理了。

首先遍历文件a,对遍历到的 URL 进行哈希取余 hash(URL) % 1000,根据计算结果把遍历到的 URL 存储到 a0, a1,a2, ..., a999,这样每个大小约为 300MB。使用同样的方法遍历文件 b,把文件 b 中的 URL 分别存储到文件 b0, b1, b2, ..., b999 中。这样处理过后,所有可能相同的 URL 都在对应的小文件中,即 a0 对应 b0, ..., a999 对应 b999,不对应的小文件不可能有相同的 URL。那么接下来,我们只需要求出这 1000 对小文件中相同的 URL 就好了。

接着遍历 ai( i∈[0,999]),把 URL 存储到一个 HashSet 集合中。然后遍历 bi 中每个 URL,看在 HashSet 集合中是否存在,若存在,说明这就是共同的 URL,可以把这个 URL 保存到一个单独的文件中。

总结

最后总结一下:

  1. 分而治之,进行哈希取余;
  2. 对每个子文件进行 HashSet 统计。
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.