Skip to content

Instantly share code, notes, and snippets.

@paulrblakey
Last active August 29, 2015 13:56
Show Gist options
  • Save paulrblakey/9003683 to your computer and use it in GitHub Desktop.
Save paulrblakey/9003683 to your computer and use it in GitHub Desktop.
<?php
/**
* DynamoDB Dump File Ingestion Script
* - takes a dynamodb dumpfile and breaks it down into individual records
* - splits each record back into its component parts in an array
* - returns it all
*
* Author : Paul Blakey.
* Date : 2/14/14
*
* License :
* The MIT License (MIT)
*
* Copyright (c) 2014 Paul Blakey
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
Class DynamoDBIngester
{
private $_record_store = array();
public function parseRecord($record_string)
{
// keys are surrounded by chr(2) & chr(3)
// split the record into an array with chr(2)
$raw_variables = explode(chr(2),$record_string);
$output_array = array();
foreach($raw_variables as $var)
{
list($var_name,$var_data) = explode(chr(3),$var);
$stripped_data = json_decode($var_data,true);
$output_array[$var_name] = array_pop(array_values($stripped_data));
}
return $output_array;
}
/**
* ingests a file and feeds it to parseRecord.
* @param string $filepath the path to the file.
* @return array converted records as array
*/
public function ingestFile($filepath)
{
// open the file
// read each line
// store the parsed record in $_recordStore
// return $_recordStore
$this->_record_store = array();
$file = fopen($filepath, 'r');
while($line = fgets($file))
{
$this->_record_store[] = $this->parseRecord($line);
}
return $this->_record_store;
}
}
// for command line usage
// - php DynamoDBIngester.php filename=/path/to/file
if($argv && count($argv) > 1) {
$args = array('filename' => '');
foreach ($argv as $k=>$v)
{
// skip the script name
if ($k == 0) continue;
list($key,$val) = explode('=',$v);
$args[$key] = $val;
}
$new_ingester = new DynamoDBIngester();
print_r($new_ingester->ingestFile($args['filename']));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment