Query

Description

This function searches for items based on the values of the primary key values. 

For more information, see Query.

Category

History

ColdFusion (2021 release): Added this function.

Syntax

serviceHandle.query(requestParameters)
serviceHandle.query(requestParameters)
serviceHandle.query(requestParameters)

Parameters

See request parameters of Query.

Example

<cfscript>
cred = {
"credentialAlias" : "myalias",
"vendorName" : "AWS",
"region" : "us-east-2",
"secretAccessKey" : "xxxxx",
"accessKeyId" : "xxxx"
}
config = {
"serviceName" = "DYNAMODB"
}
dynamo = getCloudService(cred, config)
// Stage 1: create a table
tableName="MusicTableForDemo"
tableStruct={
TableName : "#tableName#",
KeySchema:[
{ AttributeName: "Artist", KeyType: "HASH"}, // Partition key
{ AttributeName: "SongTitle", KeyType: "RANGE"} // Sort key
],
AttributeDefinitions:[
{ AttributeName: "Artist", AttributeType: "S" },
{ AttributeName: "SongTitle", AttributeType: "S" }
],
ProvisionedThroughput:{
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
}
try{
createResponse=dynamo.createTable(tableStruct)
writeOutput("Table created successfully")
}
catch (any e){
writeDump(e)
}
// Stage 2: batch write item
batchWriteItemStruct={
"RequestItems":{
"#tableName#":[
{
"PutRequest":{
"Item":{
"Artist":{"S":"Beatles"},
"SongTitle":{"S":"Yesterday"}
}
}
},
{
"PutRequest":{
"Item":{
"Artist":{"S":"Rolling STones"},
"SongTitle":{"S":"Angie"}
}
}
},
{
"PutRequest":{
"Item":{
"Artist":{"S":"Beatles"},
"SongTitle":{"S":"Hey Jude"}
}
}
},
{
"PutRequest":{
"Item":{
"Artist":{"S":"Beatles"},
"SongTitle":{"S":"Let it be"}
}
}
},
{
"PutRequest":{
"Item":{
"Artist":{"S":"Lionel Richie"},
"SongTitle":{"S":"Hello"}
}
}
},
{
"PutRequest":{
"Item":{
"Artist":{"S":"Adele"},
"SongTitle":{"S":"Hello, I Love You"}
}
}
},
{
"PutRequest":{
"Item":{
"Artist":{"S":"Adele"},
"SongTitle":{"S":"Hello"}
}
}
}
]
}
}
try{
qriteItemResponse=dynamo.batchWriteItem(batchWriteItemStruct,{"hasType":true})
writeOutput("Items inserted successfully")
writeDump(qriteItemResponse)
}
catch (any e){
writeDump(e)
}
// Stage 3: query the table
// Return a single song, by primary key
queryStruct={
"TableName":"#tableName#",
"KeyConditionExpression": "Artist =:a and SongTitle =:t",
"ExpressionAttributeValues": {
":a": "Beatles",
":t": "Yesterday"
}
}
// Output of first case
try{
queryResponse=dynamo.query(queryStruct,{"customResponse":true})
writeOutput("Items returned successfully")
writeDump(queryResponse)
}
catch (ay e){
writeDump(e)
}
// Return all songs by a particular artist
queryStructArtist={
"TableName":"#tableName#",
"KeyConditionExpression":"Artist =:a",
"ExpressionAttributeValues":{
":a": "Beatles"
}
}
// Output of second case
try{
queryResponseArtist=dynamo.query(queryStructArtist,{"customResponse":true})
writeOutput("Items returned successfully")
writeDump(queryResponseArtist)
}
catch (ay e){
writeDump(e)
}
// Return all of the songs by an artist matching first part of title
queryStructFirstPart={
"TableName":"#tableName#",
"KeyConditionExpression":"Artist =:a and begins_with(SongTitle,:t)",
"ExpressionAttributeValues":{
":a":"Adele",
":t":"Hello"
}
}
// Output of third case
try{
queryResponseFirstPart=dynamo.query(queryStructFirstPart,{"customResponse":true})
writeOutput("Items returned successfully")
writeDump(queryResponseFirstPart)
}
catch (ay e){
writeDump(e)
}
</cfscript>
<cfscript> cred = { "credentialAlias" : "myalias", "vendorName" : "AWS", "region" : "us-east-2", "secretAccessKey" : "xxxxx", "accessKeyId" : "xxxx" } config = { "serviceName" = "DYNAMODB" } dynamo = getCloudService(cred, config) // Stage 1: create a table tableName="MusicTableForDemo" tableStruct={ TableName : "#tableName#", KeySchema:[ { AttributeName: "Artist", KeyType: "HASH"}, // Partition key { AttributeName: "SongTitle", KeyType: "RANGE"} // Sort key ], AttributeDefinitions:[ { AttributeName: "Artist", AttributeType: "S" }, { AttributeName: "SongTitle", AttributeType: "S" } ], ProvisionedThroughput:{ ReadCapacityUnits: 10, WriteCapacityUnits: 10 } } try{ createResponse=dynamo.createTable(tableStruct) writeOutput("Table created successfully") } catch (any e){ writeDump(e) } // Stage 2: batch write item batchWriteItemStruct={ "RequestItems":{ "#tableName#":[ { "PutRequest":{ "Item":{ "Artist":{"S":"Beatles"}, "SongTitle":{"S":"Yesterday"} } } }, { "PutRequest":{ "Item":{ "Artist":{"S":"Rolling STones"}, "SongTitle":{"S":"Angie"} } } }, { "PutRequest":{ "Item":{ "Artist":{"S":"Beatles"}, "SongTitle":{"S":"Hey Jude"} } } }, { "PutRequest":{ "Item":{ "Artist":{"S":"Beatles"}, "SongTitle":{"S":"Let it be"} } } }, { "PutRequest":{ "Item":{ "Artist":{"S":"Lionel Richie"}, "SongTitle":{"S":"Hello"} } } }, { "PutRequest":{ "Item":{ "Artist":{"S":"Adele"}, "SongTitle":{"S":"Hello, I Love You"} } } }, { "PutRequest":{ "Item":{ "Artist":{"S":"Adele"}, "SongTitle":{"S":"Hello"} } } } ] } } try{ qriteItemResponse=dynamo.batchWriteItem(batchWriteItemStruct,{"hasType":true}) writeOutput("Items inserted successfully") writeDump(qriteItemResponse) } catch (any e){ writeDump(e) } // Stage 3: query the table // Return a single song, by primary key queryStruct={ "TableName":"#tableName#", "KeyConditionExpression": "Artist =:a and SongTitle =:t", "ExpressionAttributeValues": { ":a": "Beatles", ":t": "Yesterday" } } // Output of first case try{ queryResponse=dynamo.query(queryStruct,{"customResponse":true}) writeOutput("Items returned successfully") writeDump(queryResponse) } catch (ay e){ writeDump(e) } // Return all songs by a particular artist queryStructArtist={ "TableName":"#tableName#", "KeyConditionExpression":"Artist =:a", "ExpressionAttributeValues":{ ":a": "Beatles" } } // Output of second case try{ queryResponseArtist=dynamo.query(queryStructArtist,{"customResponse":true}) writeOutput("Items returned successfully") writeDump(queryResponseArtist) } catch (ay e){ writeDump(e) } // Return all of the songs by an artist matching first part of title queryStructFirstPart={ "TableName":"#tableName#", "KeyConditionExpression":"Artist =:a and begins_with(SongTitle,:t)", "ExpressionAttributeValues":{ ":a":"Adele", ":t":"Hello" } } // Output of third case try{ queryResponseFirstPart=dynamo.query(queryStructFirstPart,{"customResponse":true}) writeOutput("Items returned successfully") writeDump(queryResponseFirstPart) } catch (ay e){ writeDump(e) } </cfscript>
<cfscript> 
  cred = { 
    "credentialAlias" : "myalias", 
    "vendorName" : "AWS", 
    "region" : "us-east-2", 
    "secretAccessKey" : "xxxxx", 
    "accessKeyId" : "xxxx" 
  } 
  config = { 
    "serviceName" = "DYNAMODB" 
  } 
  dynamo = getCloudService(cred, config) 
 // Stage 1: create a table 
  tableName="MusicTableForDemo" 
  tableStruct={ 
    TableName : "#tableName#", 
    KeySchema:[ 
        { AttributeName: "Artist", KeyType: "HASH"},  // Partition key 
        { AttributeName: "SongTitle", KeyType: "RANGE"}  // Sort key 
    ], 
    AttributeDefinitions:[ 
        { AttributeName: "Artist", AttributeType: "S" }, 
        { AttributeName: "SongTitle", AttributeType: "S" } 
    ], 
    ProvisionedThroughput:{ 
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10 
    } 
 } 
 
 try{ 
    createResponse=dynamo.createTable(tableStruct) 
    writeOutput("Table created successfully") 
 } 
 catch (any e){ 
    writeDump(e) 
 } 
 
// Stage 2: batch write item 
  batchWriteItemStruct={ 
    "RequestItems":{ 
      "#tableName#":[ 
        { 
          "PutRequest":{ 
            "Item":{ 
              "Artist":{"S":"Beatles"}, 
              "SongTitle":{"S":"Yesterday"} 
            } 
          } 
        }, 
        { 
          "PutRequest":{ 
            "Item":{ 
              "Artist":{"S":"Rolling STones"}, 
              "SongTitle":{"S":"Angie"} 
            } 
          } 
        }, 
        { 
          "PutRequest":{ 
            "Item":{ 
              "Artist":{"S":"Beatles"}, 
              "SongTitle":{"S":"Hey Jude"} 
            } 
          } 
        }, 
        { 
          "PutRequest":{ 
            "Item":{ 
              "Artist":{"S":"Beatles"}, 
              "SongTitle":{"S":"Let it be"} 
            } 
          } 
        }, 
        { 
          "PutRequest":{ 
            "Item":{ 
              "Artist":{"S":"Lionel Richie"}, 
              "SongTitle":{"S":"Hello"} 
            } 
          } 
        }, 
        { 
          "PutRequest":{ 
            "Item":{ 
              "Artist":{"S":"Adele"}, 
              "SongTitle":{"S":"Hello, I Love You"} 
            } 
          } 
        }, 
        { 
          "PutRequest":{ 
            "Item":{ 
              "Artist":{"S":"Adele"}, 
              "SongTitle":{"S":"Hello"} 
            } 
          } 
        } 
      ] 
    } 
  } 
   
  try{ 
    qriteItemResponse=dynamo.batchWriteItem(batchWriteItemStruct,{"hasType":true}) 
    writeOutput("Items inserted successfully") 
    writeDump(qriteItemResponse) 
 
  } 
  catch (any e){ 
    writeDump(e) 
  } 
 
// Stage 3: query the table 
// Return a single song, by primary key 
queryStruct={ 
  "TableName":"#tableName#", 
  "KeyConditionExpression": "Artist =:a and SongTitle =:t", 
  "ExpressionAttributeValues": { 
        ":a": "Beatles", 
        ":t": "Yesterday" 
    } 
} 
 
// Output of first case 
try{ 
  queryResponse=dynamo.query(queryStruct,{"customResponse":true}) 
  writeOutput("Items returned successfully") 
  writeDump(queryResponse) 
} 
catch (ay e){ 
  writeDump(e) 
} 
 
// Return all songs by a particular artist 
queryStructArtist={ 
  "TableName":"#tableName#", 
  "KeyConditionExpression":"Artist =:a", 
  "ExpressionAttributeValues":{ 
    ":a": "Beatles" 
  } 
} 
 
// Output of second case 
try{ 
  queryResponseArtist=dynamo.query(queryStructArtist,{"customResponse":true}) 
  writeOutput("Items returned successfully") 
  writeDump(queryResponseArtist) 
} 
catch (ay e){ 
  writeDump(e) 
} 
 
// Return all of the songs by an artist matching first part of title 
queryStructFirstPart={ 
  "TableName":"#tableName#", 
  "KeyConditionExpression":"Artist =:a and begins_with(SongTitle,:t)", 
  "ExpressionAttributeValues":{ 
    ":a":"Adele", 
    ":t":"Hello" 
  } 
} 
 
// Output of third case 
try{ 
  queryResponseFirstPart=dynamo.query(queryStructFirstPart,{"customResponse":true}) 
  writeOutput("Items returned successfully") 
  writeDump(queryResponseFirstPart) 
} 
catch (ay e){ 
  writeDump(e) 
} 
</cfscript> 

Output

Query output
Query output

Get help faster and easier

New user?