Skip to content

Instantly share code, notes, and snippets.

@kojiisd
Last active September 11, 2017 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kojiisd/1fd2cffcf006eae7f42a9f7c638ff9ce to your computer and use it in GitHub Desktop.
Save kojiisd/1fd2cffcf006eae7f42a9f7c638ff9ce to your computer and use it in GitHub Desktop.
Three.jsのかっこいいサンプルとAWSを連携させてみた ref: http://qiita.com/kojiisd/items/1ee337fcdabc200e0921
args = sys.argv
if len(args) < 4:
print "Usage: python data-insert.py <FileName> <Region> <Table>"
sys.exit()
dynamodb = boto3.resource('dynamodb', region_name=args[2])
table = dynamodb.Table(args[3])
if __name__ == "__main__":
print "Data insert start."
target = pandas.read_csv(args[1])
for rowIndex, row in target.iterrows():
itemDict = {}
for col in target:
if row[col] != None and type(row[col]) == float and math.isnan(float(row[col])) and row[col] != float('nan'):
continue
elif row[col] == float('inf') or row[col] == float('-inf'):
continue
elif type(row[col]) == float:
itemDict[col] = Decimal(str(row[col]))
else:
itemDict[col] = row[col]
print itemDict
table.put_item(Item=itemDict)
print "Data insert finish."
$ aws s3 ls --recursive s3://aws-three/ | awk '{print $4}' | xargs -I{} aws s3api put-object-acl --acl public-read --bucket aws-three --key "{}"
var table = [
"H", "Hydrogen", "1.00794", 1, 1,
"He", "Helium", "4.002602", 18, 1,
"Li", "Lithium", "6.941", 1, 2,
"Be", "Beryllium", "9.012182", 2, 2,
:
:
apigClient.rootPost(params, body, additionalParams)
.then(function(result){
var resultObjArray= new Array();
// Add success callback code here.
var resultJson = JSON.parse(result.data);
for (var index = 0; index < resultJson.length; index++) {
var resultObj = new Object();
resultObj.id = resultJson[index].id;
resultObj.score = resultJson[index].score
resultObjArray[index] = resultObj;
}
for (var index = 0; index < resultObjArray.length; index++) {
var resultObj = resultObjArray[index];
if (resultObj.score != 0) {
$('#' + resultObj.id).css('background-color', 'rgba(255,127,127,' + ( Math.random() * 0.5 + 0.25 ) + ')')
}
}
}).catch( function(result){
// Add error callback code here.
alert("Failed");
alert(JSON.stringify(result));
});
<button id="getData">Get Data from DynamoDB</button>
$(document).ready(function(){
$("#getData").click(function() {
apigClient.rootPost(params, body, additionalParams)
.then(function(result){
var resultObjArray= new Array();
// Add success callback code here.
var resultJson = JSON.parse(result.data);
for (var index = 0; index < resultJson.length; index++) {
var resultObj = new Object();
resultObj.id = resultJson[index].id;
resultObj.score = resultJson[index].score
resultObjArray[index] = resultObj;
}
for (var index = 0; index < resultObjArray.length; index++) {
var resultObj = resultObjArray[index];
if (resultObj.score != 0) {
$('#' + resultObj.id).css('background-color', 'rgba(255,127,127,' + ( Math.random() * 0.5 + 0.25 ) + ')')
}
}
}).catch( function(result){
// Add error callback code here.
alert("Failed");
alert(JSON.stringify(result));
});
});
});
var params = {
TableName: "test-three",
KeySchema: [
{
AttributeName: "id",
KeyType: "HASH"
},
{
AttributeName: "timestamp",
KeyType: "RANGE"
}
],
AttributeDefinitions: [
{
AttributeName: "id",
AttributeType: "S"
},
{
AttributeName: "timestamp",
AttributeType: "S"
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
};
dynamodb.createTable(params, function(err, data) {
if (err) ppJson(err);
else ppJson(data);
});
id,score,timestamp
"H",0,"2017-07-23T16:00:00"
"H",0,"2017-07-23T16:01:00"
"H",0,"2017-07-23T16:02:00"
"H",0,"2017-07-23T16:03:00"
"H",0,"2017-07-23T16:04:00"
"H",1,"2017-07-23T16:05:00"
"H",0,"2017-07-23T16:06:00"
$ python data-insert.py sample-data.csv us-east-1 test-three
Data insert start.
{'timestamp': '2017-07-23T16:00:00', 'score': 0, 'id': 'H'}
{'timestamp': '2017-07-23T16:01:00', 'score': 0, 'id': 'H'}
{'timestamp': '2017-07-23T16:02:00', 'score': 0, 'id': 'H'}
{'timestamp': '2017-07-23T16:03:00', 'score': 0, 'id': 'H'}
{'timestamp': '2017-07-23T16:04:00', 'score': 0, 'id': 'H'}
{'timestamp': '2017-07-23T16:05:00', 'score': 1, 'id': 'H'}
{'timestamp': '2017-07-23T16:06:00', 'score': 0, 'id': 'H'}
Data insert finish.
<script type="text/javascript" src="../extralib/axios/dist/axios.standalone.js"></script>
<script type="text/javascript" src="../extralib/CryptoJS/rollups/hmac-sha256.js"></script>
<script type="text/javascript" src="../extralib/CryptoJS/rollups/sha256.js"></script>
<script type="text/javascript" src="../extralib/CryptoJS/components/hmac.js"></script>
<script type="text/javascript" src="../extralib/CryptoJS/components/enc-base64.js"></script>
<script type="text/javascript" src="../extralib/url-template/url-template.js"></script>
<script type="text/javascript" src="../extralib/apiGatewayCore/sigV4Client.js"></script>
<script type="text/javascript" src="../extralib/apiGatewayCore/apiGatewayClient.js"></script>
<script type="text/javascript" src="../extralib/apiGatewayCore/simpleHttpClient.js"></script>
<script type="text/javascript" src="../extralib/apiGatewayCore/utils.js"></script>
<script type="text/javascript" src="../extralib/apigClient.js"></script>
var apigClient = apigClientFactory.newClient();
var params = {
// This is where any modeled request parameters should be added.
// The key is the parameter name, as it is defined in the API in API Gateway.
"Content-Type": "application/x-www-form-urlencoded"
};
var body = {
"label_id": "id",
"label_range": "timestamp",
"id": [
"H"
],
"aggregator": "latest",
"time_from": "2017-07-23T16:00:00.000",
"time_to": "2017-07-23T16:06:00.000",
"params": {
"range": "timestamp"
}
};
var additionalParams = {
// If there are any unmodeled query parameters or headers that must be
// sent with the request, add them here.
headers: {
},
queryParams: {
}
};
apigClient.rootPost(params, body, additionalParams)
.then(function(result){
// Add success callback code here.
alert("Success");
alert(JSON.stringify(result));
}).catch( function(result){
// Add error callback code here.
});
for ( var i = 0; i < table.length; i += 5 ) {
var element = document.createElement( 'div' );
element.className = 'element';
element.style.backgroundColor = 'rgba(0,127,127,' + ( Math.random() * 0.5 + 0.25 ) + ')';
var element = document.createElement( 'div' );
element.className = 'element';
element.id = table[ i ]; // ここが追加したところ
element.style.backgroundColor = 'rgba(0,127,127,' + ( Math.random() * 0.5 + 0.25 ) + ')';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment