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的更多相关文章

  1. Golang原生sql操作Mysql数据库增删改查

    Golang要操作mysql数据库,首先需要在当期系统配置GOPATH,因为需要使用go get命令把驱动包下载到GOPATH下使用. 首先配置好你的GOPATH,执行以下命令,下载安装mysql驱动 ...

  2. Golang 接口型函数和http.Handler接口

    一.接口型函数 参考Golang必备技巧:接口型函数 1.原始接口实现 type Handler interface { Do(k, v interface{}) } func Each(m map[ ...

  3. Find Query Window的运作(手电筒)

    Find Query Window的運作?(手电筒) 提示: 在點選 Toolbar的 Find鈕時,系統會觸發 Query_Find此 Trigger. 執行 App_Find.Query_Find ...

  4. 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 ...

  5. 8、vue路由跳转 params与query 路由传参

    params与query router文件下index.js里面,是这么定义路由的: { path: '/about', name: 'About', component: About } 用quer ...

  6. 【原创】3. MYSQL++ Query类型与SQL语句执行过程(非template与SSQLS版本)

    我们可以通过使用mysqlpp:: Query来进行SQL语句的增删改查. 首先来看一下mysqlpp::Query的一些最简单的调用, conn.connect(mysqlpp::examples: ...

  7. 计算广告(5)----query意图识别

    目录: 一.简介: 1.用户意图识别概念 2.用户意图识别难点 3.用户意图识别分类 4.意图识别方法: (1)基于规则 (2)基于穷举 (3)基于分类模型 二.意图识别具体做法: 1.数据集 2.数 ...

  8. jqgrid+bootstrap样式实践

    jqgrid+bootstrap样式实践,报错数据加载,选中,删除等功能 需要引入的样式 bootstrap.min.css ui.jqgrid.css 需要引入的JS jquery.min.js b ...

  9. 八月22日,django知识点总结:

    八月22日,知识点总结: python manage.py makemigrations python manage.py migrate unique=true是指这个字段的值在这张表里不能重复,所 ...

随机推荐

  1. scrollView的讲解

    今天就讲下UIScrollView的一些事情,这个可以拖动的组件无论在应用还是游戏开发都会经常用到,所以我们就一定要更加熟悉它了.下面我们开始下手咯. (1)初始化 一般的组件初始化都可以alloc和 ...

  2. 无废话ExtJs 入门教程二十一[继承:Extend]

    无废话ExtJs 入门教程二十一[继承:Extend] extjs技术交流,欢迎加群(201926085) 在开发中,我们在使用视图组件时,经常要设置宽度,高度,标题等属性.而这些属性可以通过“继承” ...

  3. 3.使用OGG进程进行初始化数据

    开始初始化数据的时候要满足下面的条件: 1.disable掉目标段表的外键约束 2.disable掉目标端表的触发器 3.删除目标段表的索引,加快初始化速度 4.目标端表结构创建完成 源端配置初始化抽 ...

  4. HTML5编码规范

    为每个 HTML 页面的第一行添加标准模式(standard mode)的声明,这样能够确保在每个浏览器中拥有一致的展现. 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一致展 ...

  5. ios 模拟器内部网络连接问题

    今日,一运行程序,打印出来头疼的的日志 "Error Domain=kCFErrorDomainCFNetwork Code=-1001 "The request timed ou ...

  6. 《DSP using MATLAB》示例Example5.10

    代码: n = 0:10; x = 10*(0.8) .^ n; [xec, xoc] = circevod(x); %% -------------------------------------- ...

  7. 还是畅通工程[HDU1233]

    还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  8. Win 10 文件浏览器无法打开

    今天遇到个很奇怪的问题,文件浏览器File Explorer无法正常显示,点击打开后任务栏上已经显示打开了,但是屏幕上却看不到任何窗口,开始以为机子中了恶意的木马,然后就疯狂的查毒,然而并没有解决问题 ...

  9. HTLM5表单

    1.HTML5占位符文本   使用placeholder可以给文本框,搜索框添加提示性文本 <input type="text" name="url" p ...

  10. jQuery实现的简单小功能(实用)

    1.返回顶部使用JQuery的animate和scrollTop方法可以创建简单地返回顶部的动画: // Back to top $('#top').click(function (e) { e.pr ...