本文介绍了DynamoDB,如何使用BEGINS_WITH查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用进行查询。
,并使用带有DynamoDb的无服务器框架。

i'm using DocumentClient for query.and using serverless framework with DynamoDb.

我正在尝试使用BEGINS_WITH进行查询,但不提供任何主键。

i'm trying to query with BEGINS_WITH without providing any primary key.

这是我的数据的样子:

[
  {
    id: 1,
    some_string: "77281829121"
  },
  {
    id: 2,
    some_string: "7712162hgvh"
  },
  {
    id: 3,
    some_string: "7212121"
  }
]

这是我的 serverless.yml [我想是表配置]:

here is my serverless.yml [i.e Table config i guess]:

Resources:
 IPRecord:
      Type: 'AWS::DynamoDB::Table'
      Properties:
        TableName: ${file(./serverless.js):Tables.IPRecord.name}
        BillingMode: PAY_PER_REQUEST
        AttributeDefinitions:
          - AttributeName: 'id'
            AttributeType: 'S'
          - AttributeName: 'some_string'
            AttributeType: 'S'
        KeySchema:
          - AttributeName: 'id'
            KeyType: 'HASH'
        GlobalSecondaryIndexes:
          - IndexName: ${file(./serverless.js):Tables.IPRecord.index.ID}
            KeySchema:
              # ...some more index goes here
              - AttributeName: 'some_string'
                KeyType: 'RANGE'
            Projection:
              ProjectionType: 'ALL'

Q:
使用DocumentClinet我想查询第一个 some_string 的几个元素。
将返回所有匹配的文档。
像在这种情况下一样,我要查询 {some_string: 77} ,它将返回

Q:Using DocumentClinet i want to query with the first few elements of some_string.which will return all the docs, that is matching.like in this case i want to query {some_string:"77"} and it will return

 [{
    id: 1,
    some_string: "77281829121"
  },
  {
    id: 2,
    some_string: "7712162hgvh"
  }]

当前,我的查询看起来像这样[this给出错误] [在本地DynamoDB JS Shell中运行]:

currently my query looks like this [this gives error ][Running in Local DynamoDB JS shell]:

var params = {
    TableName: '<TABLE_NAME>',
    IndexName: '<INDEX_NAME>',
    KeyConditionExpression: 'begins_with(some_string,:value)',
    ExpressionAttributeValues: {
      ':value': '77'
    }
};
docClient.query(params, function(err, data) {
    if (err) ppJson(err);
    else ppJson(data);
});

上面的查询似乎需要一个主键,在我的情况下为 id 。如果我通过了,那么它将指向单个文档。

seems like this above query needs a primary key, and in my case that is id. if i pass that, then it will point to a single doc.

这是我到目前为止所取得的成就:

Here is what i have achived so far:

var params = {
    TableName: '<TABLE_NAME>',
    FilterExpression: 'begins_with(some_string,:value)',
    ExpressionAttributeValues: {
      ':value': '77'
    },
    Select:'COUNT' //as i only required COUNT
};
docClient.scan(params, function(err, data) {
    if (err) ppJson(err);
    else ppJson(data);
});

以上查询符合我的要求,但始终欢迎
任何更好的方法或解决方案。

this above query does what i want.butany better approach or solution always welcome.

推荐答案

如果beginswith查询中的字符数总是随机的,那么我看不到有解决方案dynamodb。

if number of characters in your beginswith query is always going to be random, i don't see an option solving it with dynamodb.

但是,假设至少要有3个字符。那么您可以执行以下操作。

but let's say there are going to be at least 3 characters. then you can do the following.

将您的动态模型更新为

 IPRecord:
      Type: 'AWS::DynamoDB::Table'
      Properties:
        TableName: ${file(./serverless.js):Tables.IPRecord.name}
        BillingMode: PAY_PER_REQUEST
        AttributeDefinitions:
          - AttributeName: 'id'
            AttributeType: 'S'
          - AttributeName: 'some_string'
            AttributeType: 'S'
        KeySchema:
          - AttributeName: 'id'
            KeyType: 'HASH'
          - AttributeName: 'some_string'
            KeyType: 'RANGE'

而不是存储

[
  {
    id: 1,
    some_string: "77281829121"
  },
  {
    id: 2,
    some_string: "7712162hgvh"
  },
  {
    id: 3,
    some_string: "7212121"
  }
]

存储为

[
  {
    id: 772,
    uniqueid:1,
    some_string: "77281829121"
  },
  {
    id: 771,
    uniqueid:2,
    some_string: "7712162hgvh"
  },
  {
    id: 721,
    uniqueid:3,
    some_string: "7212121"
  }
]

其中id始终是原始some_string的前3个字符。

Where id is always the first 3 character of original some_string.

现在假设您要查询您可以执行以 abcx 开头的所有项目

Now let's say you have to query all items that start with abcx you can do

选择*其中id = abc和some_string以abcx

开头,但您应始终尝试在id中包含更多字符,以便随机分配负载。例如,如果只有2个字符,则只能有36 * 36个id。如果有3个字符,则可以有36 * 36 * 36个id。

but you should always try to have more number of characters in id so that load is randomly distributed. for example if there are only 2 character only 36*36 ids are possible if there are 3 character 36*36*36 ids are possible.

这篇关于DynamoDB,如何使用BEGINS_WITH查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:49