Created
February 21, 2020 21:22
-
-
Save mbonig/76cb27ad15e9e9b5c4515231d643e830 to your computer and use it in GitHub Desktop.
Simple method for loading csvs into dynamodb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.handler = async(event) => { | |
console.log(JSON.stringify(event.Records)); | |
if (event.Records) { | |
for (const record of event.Records) { | |
await process(record.s3); | |
} | |
} | |
}; | |
const AWS = require('aws-sdk'); | |
const s3 = new AWS.S3(); | |
const ddb = new AWS.DynamoDB.DocumentClient({ convertEmptyValues: true }); | |
async function process(s3Pointer) { | |
const params = { | |
Bucket: s3Pointer.bucket.name, | |
Key: s3Pointer.object.key | |
}; | |
const file = await s3.getObject(params).promise(); | |
const body = file.Body.toString('utf-8'); | |
console.log(`${s3Pointer.object.key}:`, body); | |
const records = []; | |
for (const line of body.split('\n')) { | |
const [summons_number, plate_id, registration_state, plate_type, issue_date, violation_code, vehicle_body_type, vehicle_make, issuing_agency, street_code1, street_code2, street_code3, vehicle_expiration_date, violation_location, violation_precinct, issuer_precinct, issuer_code, issuer_command, issuer_squad, violation_time, time_first_observed, violation_county, violation_in_front_of_or_opposite, house_number, street_name, intersecting_street, date_first_observed, law_section, sub_division, violation_legal_code, days_parking_in_effect, from_hours_in_effect, to_hours_in_effect, vehicle_color, unregistered_vehicle, vehicle_year, meter_number, feet_from_curb, violation_post_code, violation_description, no_standing_or_stopping_violation, hydrant_violation, double_parking_violation] = line.split(','); | |
const map = { | |
summons_number, | |
plate_id, | |
registration_state, | |
plate_type, | |
issue_date, | |
violation_code, | |
vehicle_body_type, | |
vehicle_make, | |
issuing_agency, | |
street_code1, | |
street_code2, | |
street_code3, | |
vehicle_expiration_date, | |
violation_location, | |
violation_precinct, | |
issuer_precinct, | |
issuer_code, | |
issuer_command, | |
issuer_squad, | |
violation_time, | |
time_first_observed, | |
violation_county, | |
violation_in_front_of_or_opposite, | |
house_number, | |
street_name, | |
intersecting_street, | |
date_first_observed, | |
law_section, | |
sub_division, | |
violation_legal_code, | |
days_parking_in_effect, | |
from_hours_in_effect, | |
to_hours_in_effect, | |
vehicle_color, | |
unregistered_vehicle, | |
vehicle_year, | |
meter_number, | |
feet_from_curb, | |
violation_post_code, | |
violation_description, | |
no_standing_or_stopping_violation, | |
hydrant_violation, | |
double_parking_violation | |
}; | |
if (summons_number) { | |
records.push(map); | |
} | |
} | |
const chunks = records.reduce((all, one, i) => { | |
const ch = Math.floor(i / 20); | |
all[ch] = [].concat((all[ch] || []), one); | |
return all; | |
}, []); | |
for (const chunk of chunks) { | |
var writeParams = { | |
RequestItems: { | |
'parkingTickets': chunk.map((x) => { | |
let issue_date; | |
try { | |
const issueDate = new Date(x.issue_date); | |
issue_date = `${issueDate.getFullYear()}-${issueDate.getMonth().toString().padStart(2,'0')}-${issueDate.getDate().toString().padStart(2,'0')}`; | |
} | |
catch (err) { | |
issue_date = `invalid - ${x.issue_date}`; | |
} | |
const violation_time = x.violation_time ? `${x.violation_time.slice(-1)}${x.violation_time.slice(0,-1)}` : 'invalid'; | |
return { | |
PutRequest: { | |
Item: { | |
...x, | |
issue_date, | |
type_and_date: `${x.plate_type}-${issue_date}`, | |
violation_time | |
} | |
} | |
}; | |
}) | |
} | |
}; | |
console.log(JSON.stringify(writeParams)); | |
try { | |
await ddb.batchWrite(writeParams).promise(); | |
} | |
catch (err) { | |
console.error(err); | |
console.info(JSON.stringify(writeParams)); | |
} | |
console.log('Wrote batch!'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment