TableName:Name of table
KeySchema: Put primary and secondary key
AttributeDefinitions:Put KeySchema Definition don't add extra attributes.no of attribute in keySchema must be AttributeDefinitions
KeySchema==Attribute Definitions
ProvisionedThroughput:Put read Write Capacity per second
params = {
TableName:"Music",
KeySchema:[{
AttributeName:"year",
KeyType:"HASH"
},
{
AttributeName:"title",
KeyType:"RANG"
}
],
AttributeDefinitions:[{
AttributeName:"year",
AttributeType:"S"
},
{
AttributeName:"title",
AttributeType:"S"
}
],
ProvisionedThroughput:{
ReadCapacityUnits:10,
WriteCapacityUnits:10
},
};
dynamodb.createTable(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
Explanation :-
params :- params is object which contains attributes like TableName,KeySchema,ProvisionThroughput.
TableName- Name Of Table.
KeySchema- KeySchema is array of object where you can create object for partition key and second object for sort key.
KeyType:1-PartionKey-HASH
2.SortKey-RANG
AttributeDefinations-AttributeDefinitions is array of object which contains defenition of KeySchema.
ProvisionedThroughput- It contains read write capicity
-------------------------put item in table --------------------
params = {
TableName:"Music",
Item:{
"year":{"S":"2007"},
"title":{"S":"Drama"},
"movieName":{"S":"Hlchal"},
"ActorName":{"S":"Akshay Khana"}
},
ReturnValues: "ALL_OLD"
};
dynamodb.putItem(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
KeySchema: Put primary and secondary key
AttributeDefinitions:Put KeySchema Definition don't add extra attributes.no of attribute in keySchema must be AttributeDefinitions
KeySchema==Attribute Definitions
ProvisionedThroughput:Put read Write Capacity per second
params = {
TableName:"Music",
KeySchema:[{
AttributeName:"year",
KeyType:"HASH"
},
{
AttributeName:"title",
KeyType:"RANG"
}
],
AttributeDefinitions:[{
AttributeName:"year",
AttributeType:"S"
},
{
AttributeName:"title",
AttributeType:"S"
}
],
ProvisionedThroughput:{
ReadCapacityUnits:10,
WriteCapacityUnits:10
},
};
dynamodb.createTable(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
Explanation :-
params :- params is object which contains attributes like TableName,KeySchema,ProvisionThroughput.
TableName- Name Of Table.
KeySchema- KeySchema is array of object where you can create object for partition key and second object for sort key.
KeyType:1-PartionKey-HASH
2.SortKey-RANG
AttributeDefinations-AttributeDefinitions is array of object which contains defenition of KeySchema.
ProvisionedThroughput- It contains read write capicity
-------------------------put item in table --------------------
params = {
TableName:"Music",
Item:{
"year":{"S":"2007"},
"title":{"S":"Drama"},
"movieName":{"S":"Hlchal"},
"ActorName":{"S":"Akshay Khana"}
},
ReturnValues: "ALL_OLD"
};
dynamodb.putItem(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
---------------------------------get item from table------------------
params = {
TableName:"Music",
Key:{
"year":{"S":"2007"},
'title':{"S":"Drama"}
}
};
dynamodb.getItem(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
-------------------getItem with projection ------------------------------------------
params = {
TableName:"Songs",
Key:{
"year":{"S":"2007"},
'title':{"S":"Drama"}
},
ProjectionExpression: '#cogId, ActorName',
ExpressionAttributeNames: {
'#cogId': 'title'
}
};
dynamodb.getItem(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
-------------------getItem with projection ------------------------------------------
params = {
TableName:"Songs",
Key:{
"year":{"S":"2007"},
'title':{"S":"Drama"}
},
ProjectionExpression: '#cogId, ActorName',
ExpressionAttributeNames: {
'#cogId': 'title'
}
};
dynamodb.getItem(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
---------------------------------------get all item form table---------------------------
params = {
TableName:"Music",
Limit:50
};
dynamodb.scan(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
{"Items":[{"title":{"S":"LoveStory"},"ActorName":{"S":"SunnyDeol"},"year":{"S":"2003"},"movieName":{"S":"Gddar"}},{"title":{"S":"LoveTrangle"},"ActorName":{"S":"boby Deoal"},"year":{"S":"2006"},"movieName":{"S":"hamrazz"}},{"title":{"S":"LoveStory"},"ActorName":{"S":"SunnyDeol"},"year":{"S":"2002"},"movieName":{"S":"Gddar"}},{"title":{"S":"LoveStory"},"ActorName":{"S":"SunnyDeol"},"year":{"S":"2001"},"movieName":{"S":"Gddar"}},{"title":{"S":"Drama"},"ActorName":{"S":"Akshay Khana"},"year":{"S":"2007"},"movieName":{"S":"Hlchal"}},{"title":{"S":"LoveStory"},"ActorName":{"S":"SunnyDeol"},"year":{"S":"2000"},"movieName":{"S":"Gddar"}}],"Count":6,"ScannedCount":6}
return Items as result
var result={"Items":[{"title":{"S":"LoveStory"},"ActorName":{"S":"SunnyDeol"},"year":{"S":"2003"},"movieName":{"S":"Gddar"}},{"title":{"S":"LoveTrangle"},"ActorName":{"S":"boby Deoal"},"year":{"S":"2006"},"movieName":{"S":"hamrazz"}},{"title":{"S":"LoveStory"},"ActorName":{"S":"SunnyDeol"},"year":{"S":"2002"},"movieName":{"S":"Gddar"}},{"title":{"S":"LoveStory"},"ActorName":{"S":"SunnyDeol"},"year":{"S":"2001"},"movieName":{"S":"Gddar"}},{"title":{"S":"Drama"},"ActorName":{"S":"Akshay Khana"},"year":{"S":"2007"},"movieName":{"S":"Hlchal"}},{"title":{"S":"LoveStory"},"ActorName":{"S":"SunnyDeol"},"year":{"S":"2000"},"movieName":{"S":"Gddar"}}],"Count":6,"ScannedCount":6}
var mystr="Items"
var resultobj={}
for(data in result){
if(data==mystr){
resultobj.result=result[data]
console.log(result[data])
}
}
-----------------------------------------------------------delete an item by primary key-----------
params = {
TableName:"Music",
Key:{
"year":{"S":"2007"},
"title":{"S":"LoveStory"}
}
};
dynamodb.deleteItem(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
-----------------------------------------------fillter expression with scan--------
params = {
TableName:"Songs",
FilterExpression: "begins_with(#actor, :name)",
ProjectionExpression: '#cogId, #actor',
ExpressionAttributeNames: {
'#cogId': 'title',
'#actor':'ActorName'
},
ExpressionAttributeValues: {
':name':{S:'Ak'}
}
};
dynamodb.scan(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
-------------------------------filterExpression with query ----------
params = {
TableName:"Songs",
KeyConditionExpression: "#year = :year",
FilterExpression: "begins_with(#actor, :name)",
ProjectionExpression: '#cogId, #actor',
ExpressionAttributeNames: {
'#cogId': 'title',
'#actor':'ActorName',
'#year':'year'
},
ExpressionAttributeValues: {
':name':{S:'Ak'},
':year':{S:'2009'}
}
};
dynamodb.query(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
------------------------end -----------------------------------------------
difference between scan and query
In scan we can omit KeyConditionExpression and apply filter
but in query we have to compulsory put KeyConditionExpression to filter record.
in scan we can oration and sort key as parameter but in query we can do that.
-------------------------------batchGetItem ---------------------------------------
var params = {
RequestItems: {
Songs: {
Keys: [
{
"year": {S:'2009'},
'title':{S:'Drama9'}
},
{
"year": {S:'2008'},
'title':{S:'Drama8'}
}
],
AttributesToGet: [
'ActorName','title','year'
],
ConsistentRead: false,
},
},
ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
};
dynamodb.batchGetItem(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response
});
----------------------------------------------------------------------------------------
---------------------------betchGetItem from two tables ------------------------
var params = {
RequestItems: {
Songs: {
Keys: [
{
"year": {S:'2009'},
'title':{S:'Drama9'}
},
{
"year": {S:'2008'},
'title':{S:'Drama8'}
}
],
AttributesToGet: [
'ActorName','title','year'
],
ConsistentRead: false,
},
Music: {
Keys: [
{
"year": {S:'2009'},
'title':{S:'Bahubali'}
}
],
AttributesToGet: [
'MovieName','title','ActorName'
],
ConsistentRead: false,
}
},
ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
};
dynamodb.batchGetItem(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response
});
---------------------------------------------------------------------------------
params = {
TableName:"Songs",
FilterExpression: "begins_with(#actor, :name)",
ProjectionExpression: '#cogId, #actor',
ExpressionAttributeNames: {
'#cogId': 'title',
'#actor':'ActorName'
},
ExpressionAttributeValues: {
':name':{S:'Ak'}
}
};
dynamodb.scan(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
-------------------------------filterExpression with query ----------
params = {
TableName:"Songs",
KeyConditionExpression: "#year = :year",
FilterExpression: "begins_with(#actor, :name)",
ProjectionExpression: '#cogId, #actor',
ExpressionAttributeNames: {
'#cogId': 'title',
'#actor':'ActorName',
'#year':'year'
},
ExpressionAttributeValues: {
':name':{S:'Ak'},
':year':{S:'2009'}
}
};
dynamodb.query(params, function(err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
------------------------end -----------------------------------------------
difference between scan and query
In scan we can omit KeyConditionExpression and apply filter
but in query we have to compulsory put KeyConditionExpression to filter record.
in scan we can oration and sort key as parameter but in query we can do that.
-------------------------------batchGetItem ---------------------------------------
var params = {
RequestItems: {
Songs: {
Keys: [
{
"year": {S:'2009'},
'title':{S:'Drama9'}
},
{
"year": {S:'2008'},
'title':{S:'Drama8'}
}
],
AttributesToGet: [
'ActorName','title','year'
],
ConsistentRead: false,
},
},
ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
};
dynamodb.batchGetItem(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response
});
----------------------------------------------------------------------------------------
---------------------------betchGetItem from two tables ------------------------
var params = {
RequestItems: {
Songs: {
Keys: [
{
"year": {S:'2009'},
'title':{S:'Drama9'}
},
{
"year": {S:'2008'},
'title':{S:'Drama8'}
}
],
AttributesToGet: [
'ActorName','title','year'
],
ConsistentRead: false,
},
Music: {
Keys: [
{
"year": {S:'2009'},
'title':{S:'Bahubali'}
}
],
AttributesToGet: [
'MovieName','title','ActorName'
],
ConsistentRead: false,
}
},
ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
};
dynamodb.batchGetItem(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response
});
---------------------------------------------------------------------------------
No comments:
Post a Comment