如何使用Go APIs从DynamoDB表中返回排序键的最大时间戳?

huangapple go评论72阅读模式
英文:

How to return the largest timestamp for a sort key from dynamodb table using Go APIs?

问题

我有一个如下所示的表结构:

列名 类型
bubbleId 字符串
bubbleLastUpdated 字符串
... ...

bubbleId 是我的主键,bubbleLastUpdated 是我的排序键。

省略号(...)代表结构中与此问题无关的其余数据。

在可能的情况下,如何构建 QueryInput 对象,以返回 bubbleLastUpdated 的最大值?

我在我的 QueryInput 对象中使用了以下代码:

results, err := d.dynamoClient.Query(&dynamodb.QueryInput{
	TableName: aws.String(tableName),
	ScanIndexForward: aws.Bool(false),
	Limit: aws.Int64(1),
})

当然,这会返回一个错误,说请求中必须有 KeyConditionExpression 参数。我对键表达式构建有些了解,但我不想为这些提供条件。不幸的是,在大量搜索之后,我最终来到了这里。我可能在寻找错误的东西,或者漏掉了一些简单的东西。

一个示例将非常有帮助。

谢谢!

英文:

I have a table structure like this:

column type
bubbleId String
bubbleLastUpdated String
... ...

bubbleId is my Primary Key and bubbleLastUpdated is my Sort Key

The ellipses(...) represent the rest of the data in the structure which doesn't pertain to this question.

Using the expression builder where possible, how should the QueryInput object be built, such that the largest value for bubbleLastUpdated is returned?

I'm using the following in my QueryInput object:

	results, err := d.dynamoClient.Query(&dynamodb.QueryInput{
		TableName: aws.String(tableName),
		ScanIndexForward: aws.Bool(false),
		Limit: aws.Int64(1),
	})

Of course, this returns an error saying there must be a KeyConditionExpression parameter in the request. I'm somewhat familiar with key expression builder, but I don't want to actually provide conditionals for these. Unfortunately, after a ton of searching, I've ended up here. I'm either looking for the wrong thing or missing something simple.

An example would be most helpful.

Thanks!

答案1

得分: 0

你所称之为主键实际上被称为分区键。你必须至少指定分区键才能使用query

如果你没有分区键,那么可以使用scan,但这样效率低下,你应该考虑重新设计表格或添加全局二级索引(GSI)。

你可以像这样在查询中使用分区键:

var queryInput = &dynamodb.QueryInput{
	TableName:        aws.String(tableName),
	ScanIndexForward: aws.Bool(false),
	Limit:            aws.Int64(1),
	KeyConditions: map[string]*dynamodb.Condition{
		"bubbleId": {
			ComparisonOperator: aws.String("EQ"),
			AttributeValueList: []*dynamodb.AttributeValue{
				{
					S: aws.String("bubbleId"),
				},
			},
		},
	},
}

var resp, err = d.dynamoClient.Query(queryInput)
if err != nil {
	return nil, err
}
英文:

What you call primary key is actually called partition key. You have to specify at least the partition key to be able to use query.

If you don't have the partition key, then you can use scan, but it is inefficient, and you should consider redesigning the table, or adding global secondary index (gsi).

You can use the partition key in the query like this:

	var queryInput = &dynamodb.QueryInput{
		TableName:        aws.String(tableName),
		ScanIndexForward: aws.Bool(false),
		Limit:            aws.Int64(1),
		KeyConditions: map[string]*dynamodb.Condition{
			"bubbleId": {
				ComparisonOperator: aws.String("EQ"),
				AttributeValueList: []*dynamodb.AttributeValue{
					{
						S: aws.String("bubbleId"),
					},
				},
			},
		},
	}

	var resp, err = d.dynamoClient.Query(queryInput)
	if err != nil {
		return nil, err
	}

huangapple
  • 本文由 发表于 2021年8月9日 06:58:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/68705309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定