duanpenpan5796 2018-07-06 21:16
浏览 53
已采纳

Lambda Golang PutItem和MarshalMap到DynamoDB

I'm trying to load data from Lambda using Golang into DynamoDB, however the marshalling method is just generating empty items. I have a type struct defined as below...

type Flight struct {
    id          string
    destination string
    airline     string
    time        string
    latest      string
}

I am then populating a slice as below.....

func PutToDynamo(Flights []Flight, kind string) {

Flights := []Flight{}

for _, row := range rows {
    columns := row.FindAll("td")

    f := columns[0].Text() //all these are strings
    a := columns[1].Text()
    i := columns[2].Text()
    t := columns[4].Text()
    l := columns[5].Text()

    flight := Flight{
        id:          i,
        destination: f,
        airline:     a,
        time:        t,
        latest:      l,
    }
    Flights = append(Flights, flight)

Which I then try and load into DynamoDB.

func PutToDynamo(flights []Flight, kind string) {
    for _, flight := range flights {
        av, err := dynamodbattribute.MarshalMap(flight)

        input := &dynamodb.PutItemInput{
            Item:      av,
            TableName: aws.String("flights"),
        }

        _, err = svc.PutItem(input)
        fmt.Println("FLIGHT: ", input.GoString())

If I print flight, I can see all the information I expect. However, input.GoString() is just returning the below...

 {
   Item: {
   },
   TableName: "flights"
 }

and I get an error thrown from DynamoDB in lambda.

ValidationException: One or more parameter values were invalid: Missing the key id in the item

Any ideas?? I've tried putting json:"id" etc in the struct, but no difference.

Thanks!

  • 写回答

1条回答 默认 最新

  • dotibrb048760 2018-07-06 21:53
    关注

    The fields from your structure are unexported, and this may be the reason why dynamo marshal is not marshaling it (quick look at the source code suggests that dynamo and json marshal methods are a bit similar). According to regular, golang json Marshal, from documentation:

    The json package only accesses the exported fields of struct types (those that begin with an uppercase letter). Therefore only the the exported fields of a struct will be present in the JSON output.

    There are two ways you can solve this:

    a) Change your struct to have exported fields.

    type Flight struct {
        Id          string
        Destination string
        Airline     string
        Time        string
        Latest      string
    }
    

    b) If you don't want to change your struct (for example if its referenced a lot in your code and refactoring would be a hassle), you can add a custom Marshal and Unmarhsal functions and hide your struct between a custom made export struct (code below is untested - its just to give a brief idea):

    type Flight struct {
        id          string
        destination string
        airline     string
        time        string
        latest      string
    }
    
    type FlightExport struct {
            Id          string
            Destination string
            Airline     string
            Time        string
            Latest      string
    }
    
    func(f *Flight) MarshalJSON()([] byte, error) {
        fe: = &FlightExport {
            ID: f.id,
            Destination: f.destination,
            Airline: f.airline,
            Time: f.time,
            Latest: f.latest
        }
    
            return json.Marshal(fe)
    }
    
    func(f *Flight) UnmarshalJSON(data[] byte) error {
        var fe FlightExport
        err: = json.Unmarshal(data, &fe)
        if err != nil {
            return err
        }
    
        f.id = fe.ID
        f.destination = fe.Destination
        f.airline = fe.Airline
        f.time = fe.Time
        f.latest = fe.Latest
    
        return nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探