AmazonDynamoDB的简单使用

该博客展示了如何使用AmazonDynamoDB客户端进行本地数据查询。通过AmazonDynamoDBClientBuilder创建客户端,设置查询条件,循环遍历表中的数据,获取并处理id和data字段,直至遍历完整个数据集。示例代码详细说明了DynamoDB的数据查询流程。
摘要由CSDN通过智能技术生成
// 连接DB
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
      					.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
            			.build();
	// 设置查询条件
    Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
		expressionAttributeValues.put(":val1", new AttributeValue().withS(Constant.STATUS));
		expressionAttributeValues.put(":val2", new AttributeValue().withS(Constant.DATE));

		Map<String, AttributeValue> lastKeyEvaluated = null;
		Integer i = 0;
		// 循环查询数据
		do {
			ScanRequest scanRequest = new ScanRequest()
					// 表名
					.withTableName(properties.get(Constant.TABLENAME))
					// 上一页中最后一个项目的主键值
					.withExclusiveStartKey(lastKeyEvaluated)
					// where条件
					.withFilterExpression("status = :val1 and  date = :val2")
					// 只查出id和data字段
					.withProjectionExpression("id,data")
					.withExpressionAttributeValues(expressionAttributeValues);
			// 获取返回结果
			ScanResult result = client.scan(scanRequest);

			for (Map<String, AttributeValue> item : result.getItems()) {
				dataList.add(
						String.valueOf(item.get("id").getS()) + "," + String.valueOf(item.get("data").getS()));
			}

			lastKeyEvaluated = result.getLastEvaluatedKey();

			i++;
		} while (lastKeyEvaluated != null);

参考文档:https://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Introduction.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值