Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

aws appsync - DynamoDB Query with Global Secondary Index throws 'ValidationException: Query key condition not supported'

When trying to execute the query from the AppSync console in AWS, the following error keeps popping up, as seen in the logs of CloudWatch: 'ValidationException: Query key condition not supported'

I have tried changing the schema with different types and the Global Secondary Index where I swap the Partition Key and Sort Key. Also changing the code to scan rather then query works fine.

Using AWS-CDK I have setup an GraphQL API. For the DynamoDB table I want to use a Global Secondary Index. For that I have setup in my schema.graphql:

type ObjectName {
  id: ID!
  trackTimeStampUtc: Int!
  channel: String!
  ...
}

with:

channel (String!) as Partition Key

trackTimeStampUtc (Int!) as Sort key

as so in Stack.ts:

const table = new ddb.Table(this, 'tableName', {
  billingMode: ddb.BillingMode.PAY_PER_REQUEST,
  timeToLiveAttribute: 'ttl',
  partitionKey: {
    name: 'id',
    type: ddb.AttributeType.STRING,
  },
});

table.addGlobalSecondaryIndex({
  indexName: 'search-index',
  partitionKey: {
    name: 'channel',
    type: ddb.AttributeType.STRING,
  },
  sortKey: {
    name: 'trackTimeStampUtc',
    type: ddb.AttributeType.NUMBER,
  },
});

And in the Lambda handler with the function consuming the Params:

params:  {
  Index: 'search-index',
  TableName: 'tableName',
  KeyConditionExpression: '#ch = :channel and #ts between :start and :end',
  ExpressionAttributeNames: { 
    '#ch': 'channel', 
    '#ts': 'trackTimeStampUtc' 
  },
  ExpressionAttributeValues: { 
    ':channel': 'test', 
    ':start': 1610971398, 
    ':end': 1611667818 
  }
}

Which is sent using await docClient.query(params).promise();

While I am sure I am doing something wrong I just can't seem to figure it out. Setting this up using .scan with some slight adjustment works fine, but having a GSI would be better. Please can anyone help?

Update 27-01 11:52:

I realize that the combination of Partition Key and Sort Key might not be the most efficient for the purpose it serves, regardless I wouldn't have any other combination work anyway. So getting this to work, for me, is understanding how it should work.

Update 29-01 15:18:

The issue is solved. Turned out I had Index in the Params for refering to the index name, instead of IndexName. Changed that fixed the issue and the query worked. Thank you again Peter.

question from:https://stackoverflow.com/questions/65917253/dynamodb-query-with-global-secondary-index-throws-validationexception-query-ke

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...