Mehrere REST -API -Aufrufe von 1M -Dateneinträgen mit Datenbanken + Scala?Python

Python-Programme
Anonymous
 Mehrere REST -API -Aufrufe von 1M -Dateneinträgen mit Datenbanken + Scala?

Post by Anonymous »

Ich versuche einen API -Anruf zu erhalten, um alle Gebäude in La County zu erhalten. Die Website für den Datensatz ist hier < /p>
Der Landkreis verfügt über 3 Millionen Gebäude, die Gebäude auf 1 Million gefiltert haben. Sie können sich meine query_params im Code ansehen. >
Auf der ESRI -Entwickler -Website verstehe ich, dass 1 Einzel -API -Anruf auf 10.000 Ergebnisse begrenzt ist. Aufgrund meines Problems muss ich jedoch alle 1 Million Gebäude abrufen. Br />import aiohttp
import asyncio
import nest_asyncio

nest_asyncio.apply() # Required if running in Jupyter Notebook

# Base URL for the API query
BASE_URL = "https://services.arcgis.com/RmCCgQtiZLD ... er/1/query"

# Parameters for the query
QUERY_PARAMS = {
"where": "(HEIGHT < 33) AND UseType = 'RESIDENTIAL' AND SitusCity IN('LOS ANGELES CA','BEVERLY HILLS CA', 'PALMDALE')",
"outFields": "*",
"outSR": "4326",
"f": "json",
"resultRecordCount": 1000, # Fetch 1000 records per request
}

async def fetch_total_count():
"""Fetch total number of matching records."""
params = QUERY_PARAMS.copy()
params["returnCountOnly"] = "true"

async with aiohttp.ClientSession() as session:
async with session.get(BASE_URL, params=params) as response:
data = await response.json()
return data.get("count", 0) # Extract total count

async def fetch(session, offset):
"""Fetch a batch of records using pagination."""
params = QUERY_PARAMS.copy()
params["resultOffset"] = offset

async with session.get(BASE_URL, params=params) as response:
return await response.json()

async def main():
"""Fetch all records asynchronously with pagination."""
all_data = []
total_count = await fetch_total_count()
print(f"Total Records to Retrieve: {total_count}")

semaphore = asyncio.Semaphore(10) # Limit concurrency to prevent API overload

async with aiohttp.ClientSession() as session:
async def bound_fetch(offset):
async with semaphore:
data = await fetch(session, offset)
return data

# Generate tasks for pagination
tasks = [bound_fetch(offset) for offset in range(0, total_count, 1000)]
results = await asyncio.gather(*tasks)

for data in results:
if "features" in data:
all_data.extend(data["features"])

print(f"Total Records Retrieved: {len(all_data)}")
return all_data

# Run the async function
all_data = asyncio.run(main())
< /code>
Ich habe mich an Databricks + Scala gewandt, um das Datenabruf schneller zu beschleunigen. Aber ich bin brandneu für Big Data Computing. Ich bin mir ein wenig bewusst, dass Sie Ihre API -Anrufe "parallisieren" und sie zu einem Big DataFrame kombinieren müssen?
Kann mir jemand Vorschläge geben?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post