Last active
September 11, 2020 07:46
-
-
Save tobinbc/56ce7fbde53a4218a0ec0805e8d5fc11 to your computer and use it in GitHub Desktop.
This file contains 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
const AWS = require('aws-sdk') | |
const glue = new AWS.Glue({ apiVersion: '2017-03-31' }); | |
let StorageDescriptor | |
exports.handler = async ({ Records }) => { | |
// Get the table data or use cache if this is a subsequent container usage. | |
if(!StorageDescriptor){ | |
({ Table:{StorageDescriptor} } = await glue.getTable({ | |
DatabaseName: process.env.DATABASE_NAME, | |
Name: process.env.TABLE_NAME, | |
}).promise()) | |
} | |
for (let i = 0; i < Records.length; i++) { | |
let Values = Records[i].s3.object.key.split('/').shift() //First element is 'processed' | |
try { | |
let result = await glue.getPartition({ | |
DatabaseName: process.env.DATABASE_NAME, | |
TableName: process.env.TABLE_NAME, | |
PartitionValues: Values | |
}).promise() | |
} catch (e) { | |
// exception... need a new partition! | |
if(e.code === 'EntityNotFoundException'){ | |
let params = { | |
DatabaseName: process.env.DATABASE_NAME, | |
TableName: process.env.TABLE_NAME, | |
PartitionInput: { | |
StorageDescriptor: { | |
...StorageDescriptor, | |
Location: `${StorageDescriptor.Location}${Values.join('/')}/` // <-- Note extra '/' at end, and no '/' between. | |
}, | |
Values, | |
}, | |
} | |
await glue.createPartition(params).promise() | |
} else { | |
throw e | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment