如何在golang中创建嵌套数据集的结构?
golang新手&尝试制作一个脚本,用于批量上传到Elasticsearch服务器。我的json数据集是这样的如何在golang中创建嵌套数据集的结构?,go,Go,golang新手&尝试制作一个脚本,用于批量上传到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_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"
}]
}
}
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"`
}
我为上述数据集制作的Golang结构(通过参考google和其他在线信息)如下所示
{
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"
}]
}
}
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\u spec&filter\u 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)
}
}
主程序包
进口(
“fmt”
“golang.org/x/net/context”
“gopkg.in/olivere/elastic.v5”
“strconv”
)
类型产品结构{
ProductDisplayname字符串`json:“product\u displayname”`
ProductPrice字符串`json:“产品价格”`
流行字符串`json:“流行”`
条形码字符串`json:“条形码”`
ExclusiveFlag字符串`json:“exclusive_标志”`
ProductID字符串`json:“产品\u id”`
ProductName字符串`json:“产品名称”`
BrandName字符串`json:“品牌名称”`
BrandID字符串`json:“brand_id”`
ProductSpec结构{
DisplaySpec[]结构{
SpecID字符串`json:“spec_id”`
Sdv字符串`json:“Sdv”`
Snv字符串`json:“Snv”`
}`json:“显示规格”`
FilterSpec[]结构{
SpecID字符串`json:“spec_id”`
Sdv字符串`json:“Sdv”`
Snv字符串`json:“Snv”`
}`json:'filter_spec'`
}`json:“产品规格”`
}
func main(){
//创建上下文
ctx:=context.Background()
客户端,错误:=elastic.NewClient()
如果错误!=零{
格式打印项次(“%v”,错误)
}
//批量上传代码
n:=0
对于i:=0;i<1000;i++{
bulkRequest:=客户端.Bulk()
对于j:=0;j<10000;j++{
n++
产品数据:=产品{产品显示名称:“LG手写笔2加K535D(16 GB,棕色)”,产品价格:“24000.00”,流行度:“0.00”,条形码:“0”,独家标志:“0”,产品id:“17698276”,产品名称:“手写笔2加K535D(棕色)”,品牌名称:“LG”,品牌id:“1”,产品规格:{显示规格:[规格id:“103”,sdv:“24000”,snv:“24000.0000”,规格id:“104”,sdv:“GSM”,snv:“0.0000”}],过滤器规格:[{spec_id:“103”,sdv:“24000”,snv:“24000.0000”},{spec_id:“105”,sdv:“触摸屏”,snv:“0.0000”}]
req:=elastic.NewBulkIndexRequest().Index(“店面”).Type(“产品”).Id(strconv.Itoa(n)).Doc(产品数据)
bulkRequest=bulkRequest.Add(请求)
}
bulkResponse,错误:=bulkRequest.Do(ctx)
如果错误!=零{
fmt.Println(错误)
}
如果有响应!=无{
fmt.Println(批量响应)
}
fmt.Println(一)
}
}
工作流
1.-验证您的(您发布的内容无效)
2.-构建一个适当的结构
,您可以使用这个漂亮的
针对您的案例
structs
看起来不错,只是您没有通过大写首字母导出struct字段(谢谢@ANisus)
这看起来更自然
此外,他还犯了一个常见的Go初学者错误,即没有通过大写首字母导出struct字段。但这是一个很好的工具!还有一件事:如果他使用的是“json”,那么它是无效的。JSON对象键必须用引号括起来:
“barcode”
而不是barcode
再次感谢@ANisus将其合并到答案中。@ANisus:实际上,我使用的是https://github.com/olivere/elastic
用于与Elasticsearch通信&此处给出了一个示例,如tweet:=tweet{User:“olivere”,消息:“包strconv实现与基本数据类型的字符串表示形式之间的转换。”+strconv.Itoa(n)}
。这就是为什么我刚刚从我的JSON字符串中删除了这些引号。@klashxx我已经发布了我的主go文件main.go
。你能看一下吗?告诉我有什么问题。