如何在golang中为嵌套数据集创建结构?

时间:2017-03-14 10:04:02

标签: go

golang& amp;尝试制作一个脚本,以便批量上传到Elasticsearch服务器。我的json数据集是这样的......

{
    product_displayname: "LG Stylus 2 Plus K535D (16 GB, Brown)",
    product_price: "24000.00",
    popularity: "0.00",
    barcode: "",
    exclusive_flag: "0",
    product_id: "176982",
    product_name: "Stylus 2 Plus K535D (Brown)",
    brand_name: "LG",
    brand_id: "1",
    product_spec : {
        display_spec: [{
            spec_id: "103",
            sdv: "24000",
            snv: "24000.0000"
        }, {
            spec_id: "104",
            sdv: "GSM",
            snv: "0.0000"
        }],
        filter_spec: [{
            spec_id: "103",
            sdv: "24000",
            snv: "24000.0000"
        }, {
            spec_id: "105",
            sdv: "Touch Screen",
            snv: "0.0000"
        }]
    }
}

Golang Structure我为上述数据集制作(通过引用google和其他在线信息)就像这样......

type Product struct {
    product_displayname string `json:"product_displayname"`
    product_price       string `json:"product_price"`
    popularity          string `json:"popularity"`
    barcode             string `json:"barcode"`
    exclusive_flag      string `json:"exclusive_flag"`
    product_id          string `json:"product_id"`
    product_name        string `json:"product_name"`
    brand_name          string `json:"brand_name"`
    brand_id            string `json:"brand_id"`
    product_spec
}

type product_spec struct {
    display_spec []display_speclist
    filter_spec []filter_speclist
}

type display_speclist struct {
    spec_id string `json:"spec_id"`
    sdv     string `json:"sdv"`
    snv     string `json:"snv"`
}

type filter_speclist struct {
    spec_id string `json:"spec_id"`
    sdv     string `json:"sdv"`
    snv     string `json:"snv"`
}

但是,每当我尝试在我的批量上传脚本中使用上面的结构和样本数据时,我就会出现以下错误

github.com/crazyheart/elastic-bulk-upload/main.go:70: syntax error: missing operand
github.com/crazyheart/elastic-bulk-upload/main.go:70: unknown escape sequence
github.com/crazyheart/elastic-bulk-upload/main.go:71: syntax error: non-declaration statement outside function body

我觉得我在golang结构中映射嵌套字段display_spec & filter_spec时犯了一些错误。但无法弄清楚它是什么。

main.go

package main

import (
    "fmt"
    "golang.org/x/net/context"
    "gopkg.in/olivere/elastic.v5"
    "strconv"
)

type Product struct {
    ProductDisplayname string `json:"product_displayname"`
    ProductPrice string `json:"product_price"`
    Popularity string `json:"popularity"`
    Barcode string `json:"barcode"`
    ExclusiveFlag string `json:"exclusive_flag"`
    ProductID string `json:"product_id"`
    ProductName string `json:"product_name"`
    BrandName string `json:"brand_name"`
    BrandID string `json:"brand_id"`
    ProductSpec struct {
        DisplaySpec []struct {
            SpecID string `json:"spec_id"`
            Sdv string `json:"sdv"`
            Snv string `json:"snv"`
        } `json:"display_spec"`
        FilterSpec []struct {
            SpecID string `json:"spec_id"`
            Sdv string `json:"sdv"`
            Snv string `json:"snv"`
        } `json:"filter_spec"`
    } `json:"product_spec"`
}

func main() {
    // Create a context
    ctx := context.Background()

    client, err := elastic.NewClient()
    if err != nil {
        fmt.Println("%v", err)
    }

    // Bulk upload code
    n := 0
    for i := 0; i < 1000; i++ {
        bulkRequest := client.Bulk()
        for j := 0; j < 10000; j++ {
            n++
            product_data := Product{product_displayname:"LG Stylus 2 Plus K535D (16 GB, Brown)",product_price:"24000.00",popularity:"0.00",barcode:"",exclusive_flag:"0",product_id:"17698276",product_name:"Stylus 2 Plus K535D (Brown)",brand_name:"LG",brand_id:"1",product_spec:{display_spec:[{spec_id:"103",sdv:"24000",snv:"24000.0000"},{spec_id:"104",sdv:"GSM",snv:"0.0000"}],filter_spec:[{spec_id:"103",sdv:"24000",snv:"24000.0000"},{spec_id:"105",sdv:"Touch Screen",snv:"0.0000"}]} }
            req := elastic.NewBulkIndexRequest().Index("shopfront").Type("products").Id(strconv.Itoa(n)).Doc(product_data)
            bulkRequest = bulkRequest.Add(req)
        }

        bulkResponse, err := bulkRequest.Do(ctx)
        if err != nil {
            fmt.Println(err)
        }
        if bulkResponse != nil {
            fmt.Println(bulkResponse)
        }
        fmt.Println(i)
    }
}

1 个答案:

答案 0 :(得分:0)

<强>工作流

1.-验证您的json(您发布的那个无效)。

2.-建立一个合适的struct,你可以帮助自己使用这个漂亮的tool

适合您的情况

structs似乎没有问题,除非你没有通过大写首字母来导出结构字段(谢谢@ANisus)。

这(缩短)似乎更自然

type Product struct {
    ProductDisplayname string `json:"product_displayname"`
    ProductSpec struct {
        DisplaySpec []struct {
            SpecID string `json:"spec_id"`
            Sdv string `json:"sdv"`
            Snv string `json:"snv"`
        } `json:"display_spec"`
        FilterSpec []struct {
            SpecID string `json:"spec_id"`
            Sdv string `json:"sdv"`
            Snv string `json:"snv"`
        } `json:"filter_spec"`
    } `json:"product_spec"`
}
相关问题