golang操作es批量索引数据Bulk 减少开销提⾼效率现有130万条数据⼀条⼀条索引的话需要不停的跑需要两天左右如果使⽤bulk 五分钟就完事⼉了
func IndexPrice() {
es := tool.ES{cad三视图
Index: "financials.us.gama",
感恩老师的朗诵稿Type: "esstockprice",
}
养生秘方
var MaxId int
MaxId = 0
var price *model.PriceRecord
团员推优演讲稿
// 每次取两万条数据索引两万条记录最⼤id 下⼀次根据maxid查询
for {
bulkRequest := tool.Client.Bulk()
// 每次只取2万条数据
priceList := price.GetPrice2(MaxId)
关于学校的作文if len(priceList) == 0 {
break
}
var upDateList []int
for _, i := range priceList {
upDateList = append(upDateList, i.ID)
if i.ID > MaxId {
MaxId = i.ID
}
id := i.Symbol + "_" + i.DateTime
var esPrice *ESPrice
esPrice = &ESPrice{
ShortName: i.Symbol,
TradeDate: i.DateTime,
StockPriceId: id,
IsTradeDate: 1,
Open: i.Open,
广播稿100字左右
Clo: i.Clo,
Volume: i.Volume,
High: i.High,
Low: i.Low,
}
req := elastic.NewBulkIndexRequest().
Index(es.Index).
Type(es.Type).
Id(id).
Doc(esPrice)
bulkRequest = bulkRequest.Add(req)
}
bulkRespon, err := bulkRequest.Do(context.Background())
灰色英文怎么读if err != nil {
fmt.Println(err)棚车
}el{
// 索引成功就修改数据库状态 is_index = 1
if !price.UpdatePriceList(upDateList){
fmt.Println("更新数据库状态错误")
}
}
fmt.Println("耗时:",bulkRespon.Took, "索引了:",len(bulkRespon.Items))
}
}