dynamodb golang query one Item
golang dynamodb query oneItem and unmarshal to object
// +build example package main import (
// "flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"os"
"time"
) func exitWithError(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit()
} func main() {
cfg := Config{}
if err := cfg.Load(); err != nil {
exitWithError(fmt.Errorf("failed to load config, %v", err))
} // Create the config specifiing the Region for the DynamoDB table.
// If Config.Region is not set the region must come from the shared
// config or AWS_REGION environment variable.
awscfg := &aws.Config{}
if len(cfg.Region) > {
awscfg.WithRegion(cfg.Region)
} // Create the session that the DynamoDB service will use.
sess, err := session.NewSession(awscfg)
if err != nil {
exitWithError(fmt.Errorf("failed to create session, %v", err))
} // Create the DynamoDB service client to make the query request with.
svc := dynamodb.New(sess) // Build the query input parameters
params := &dynamodb.ScanInput{
TableName: aws.String(cfg.Table),
}
if cfg.Limit > {
params.Limit = aws.Int64(cfg.Limit)
}
fmt.Println("params is: ", params)
// Make the DynamoDB Query API call
result, err := svc.Scan(params)
if err != nil {
exitWithError(fmt.Errorf("failed to make Query API call, %v", err))
} items := []Item{} // Unmarshal the Items field in the result value to the Item Go type.
err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &items)
if err != nil {
exitWithError(fmt.Errorf("failed to unmarshal Query result items, %v", err))
} // Print out the items returned
for i, item := range items {
fmt.Printf("%d: UserID: %d, Time: %s Msg: %s Count: %d SecretKey:%s DeviceId: %s CampaginId:%s \n ", i, item.UserID, item.Time, item.Msg, item.Count, item.SecretKey, item.DeviceId, item.CampaginId)
//fmt.Printf("\tNum Data Values: %d\n", len(item.Data))
} /* //1231241 deviceid
params_del := &dynamodb.ScanInput{
TableName: aws.String(cfg.Table), }
*/
//query oneItem demo
params_get := &dynamodb.GetItemInput{
Key: map[string]*dynamodb.AttributeValue{
"deviceid": {
S: aws.String(""),
},
},
TableName: aws.String(cfg.Table), // Required
} resp, err_get := svc.GetItem(params_get)
oneItem := Item{}
if err_get == nil {
// resp is now filled
fmt.Printf("resp type is %T \n ", resp.Item)
err = dynamodbattribute.UnmarshalMap(resp.Item, &oneItem)
if err == nil {
fmt.Printf(" UserID: %d, Time: %s Msg: %s Count: %d SecretKey:%s DeviceId: %s CampaginId:%s \n ", oneItem.UserID, oneItem.Time, oneItem.Msg, oneItem.Count, oneItem.SecretKey, oneItem.DeviceId, oneItem.CampaginId)
} else {
fmt.Println(" Unmarshal err :", err)
}
//fmt.Println("convert to Struct obj err is ", err, "oneItem is:", oneItem)
} else {
fmt.Println("GetItem err is: ", err_get)
} } type Item struct {
UserID int // Hash key, a.k.a. partition key
Time time.Time // Range key, a.k.a. sort ke
Msg string `dynamo:"Message"`
Count int `dynamo:",omitempty"`
SecretKey string `dynamo:"-"` // Ignored
DeviceId string `dynamo:"deviceid"`
CampaginId string `dynamo:"campid"`
} type Config struct {
Table string // required
Region string // optional
Limit int64 // optional } func (c *Config) Load() error {
//flag.Int64Var(&c.Limit, "limit", 0, "Limit is the max items to be returned, 0 is no limit")
//flag.StringVar(&c.Table, "table", "", "Table to Query on")
//flag.StringVar(&c.Region, "region", "", "AWS Region the table is in")
//flag.Parse()
c.Limit =
c.Region = "ap-southeast-1"
c.Table = "xxx_your_table_name"
if len(c.Table) == {
// flag.PrintDefaults()
return fmt.Errorf("table name is required.")
} return nil
}
dynamodb golang query one Item的更多相关文章
- Golang原生sql操作Mysql数据库增删改查
Golang要操作mysql数据库,首先需要在当期系统配置GOPATH,因为需要使用go get命令把驱动包下载到GOPATH下使用. 首先配置好你的GOPATH,执行以下命令,下载安装mysql驱动 ...
- Golang 接口型函数和http.Handler接口
一.接口型函数 参考Golang必备技巧:接口型函数 1.原始接口实现 type Handler interface { Do(k, v interface{}) } func Each(m map[ ...
- Find Query Window的运作(手电筒)
Find Query Window的運作?(手电筒) 提示: 在點選 Toolbar的 Find鈕時,系統會觸發 Query_Find此 Trigger. 執行 App_Find.Query_Find ...
- Understanding Item Import and Debugging Problems with Item Import (Doc ID 268968.1)
In this Document Purpose Details Scenario 1: Testing the basic item import with minimum columns po ...
- 8、vue路由跳转 params与query 路由传参
params与query router文件下index.js里面,是这么定义路由的: { path: '/about', name: 'About', component: About } 用quer ...
- 【原创】3. MYSQL++ Query类型与SQL语句执行过程(非template与SSQLS版本)
我们可以通过使用mysqlpp:: Query来进行SQL语句的增删改查. 首先来看一下mysqlpp::Query的一些最简单的调用, conn.connect(mysqlpp::examples: ...
- 计算广告(5)----query意图识别
目录: 一.简介: 1.用户意图识别概念 2.用户意图识别难点 3.用户意图识别分类 4.意图识别方法: (1)基于规则 (2)基于穷举 (3)基于分类模型 二.意图识别具体做法: 1.数据集 2.数 ...
- jqgrid+bootstrap样式实践
jqgrid+bootstrap样式实践,报错数据加载,选中,删除等功能 需要引入的样式 bootstrap.min.css ui.jqgrid.css 需要引入的JS jquery.min.js b ...
- 八月22日,django知识点总结:
八月22日,知识点总结: python manage.py makemigrations python manage.py migrate unique=true是指这个字段的值在这张表里不能重复,所 ...
随机推荐
- 激活webstorm2016如何激活webstorm2016永久激活webstorm2016
没有那么麻烦,我这个方法是简单粗暴: 1.搜webstrom2016,最新的是2016.3 2.官方下载 3.断网,改本地时间,你打算用多久,就把本地时间往未来调多久 4.安装webstorm 5.一 ...
- SQL语句汇总
1.查询出来数据保留小数点2位,并且0.01时候,不会展示为.01. select to_char(0.1,'fm9999999990.00') from dual; 2.wm_concat ...
- iOS 状态栏隐藏显示
在 info.plist 文件中 配置 View controller-based status bar appearance 对应的值为 NO 这样可以使用 application 对象来设置 状态 ...
- LeetCode之237. Delete Node in a Linked List
------------------------------------------------ 因为不知道前序是谁,所以只好采用类似于数组实现的列表移动值, 又因为如果当前是最后一个元素了但是已经没 ...
- unity3d中串口的使用
工作中遇到了串口的问题,通过查资料&实验写出了下面代码: 关于串口的代码我在网上看了好多,一种是用事件来做,另外一种使用线程来做:(经过试验,unity无法用串口自带的事件进行数据读取): 看 ...
- [BI项目记]-搭建代码管理环境之服务端
上一篇介绍如何搭建环境进行文档版本的管理,这篇主要介绍搭建环境进行代码版本的管理. 即使是BI项目也要进行代码版本管理.代码版本管理的工具有很多,VSS, SVN等都是当下大家经常提起的,这里主要介绍 ...
- Devexpress使用经验1
1. 使用RibbonForm时,修改左上角图标:添加ApplicationMenu1,在属性中找Ribbon->ApplicationIcon,设置图标 2. 隐藏DevExpress Xtr ...
- 破解加密PDF文件pdfcrack
破解加密PDF文件pdfcrack PDF是常见的文档格式.它允许用户设置双重密码来保护文档.第一重是用户密码(user password),当打开PDF文档,输入该密码.第二重是所有者密码(ow ...
- input的实时监控
<input id="phone" type="tel" placeholder="请输入手机号"> $("#phon ...
- C# Winform TreeView 的一些基本用法
下面是treeview的用法TreeView组件是由多个类来定义的,TreeView组件是由命名空间"System.Windows .Forms"中的"TreeView& ...