You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.6 KiB
66 lines
1.6 KiB
1 month ago
|
package dbHelper
|
||
|
|
||
|
import (
|
||
|
"gitea.anxinyun.cn/container/common_models"
|
||
|
"log"
|
||
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func TestQuery(t *testing.T) {
|
||
|
es := NewESHelper([]string{"http://10.8.30.160:30092"}, "elastic", "public123")
|
||
|
|
||
|
requestBody := `
|
||
|
{
|
||
|
"size": 2,
|
||
|
"sort": [
|
||
|
{
|
||
|
"collect_time": {
|
||
|
"order": "desc"
|
||
|
}
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
`
|
||
|
rps := es.SearchRaw("native_raws", requestBody)
|
||
|
for _, rp := range rps {
|
||
|
d := rp.Source
|
||
|
log.Printf("%v", d)
|
||
|
}
|
||
|
//resp, err := es.request("native_raws", requestBody)
|
||
|
//if err != nil {
|
||
|
// log.Println(resp)
|
||
|
//}
|
||
|
|
||
|
}
|
||
|
|
||
|
func TestBulkWrite(t *testing.T) {
|
||
|
layout := "2006-01-02 15:04:05.000"
|
||
|
str := "2024-03-16 08:30:45.123"
|
||
|
rTime, _ := time.Parse(layout, str)
|
||
|
var createNativeRaws []common_models.EsRaw
|
||
|
createNativeRaws = append(createNativeRaws, common_models.EsRaw{
|
||
|
StructId: 1,
|
||
|
IotaDeviceName: "设备1",
|
||
|
Data: map[string]any{"temperature": 1.112, "humidity": 1.2},
|
||
|
CollectTime: rTime,
|
||
|
Meta: map[string]string{"temperature": "温度", "humidity": "湿度"},
|
||
|
IotaDevice: "id_1",
|
||
|
CreateTime: rTime,
|
||
|
})
|
||
|
createNativeRaws = append(createNativeRaws, common_models.EsRaw{
|
||
|
StructId: 1,
|
||
|
IotaDeviceName: "设备2",
|
||
|
Data: map[string]any{"temperature": 4.1, "humidity": 4.44},
|
||
|
CollectTime: rTime,
|
||
|
Meta: map[string]string{"temperature": "温度", "humidity": "湿度"},
|
||
|
IotaDevice: "id_2",
|
||
|
CreateTime: rTime,
|
||
|
})
|
||
|
|
||
|
es := NewESHelper([]string{"http://10.8.30.160:30092"}, "", "")
|
||
|
esIndex := "go_native_raws"
|
||
|
es.BulkWriteRaws2Es(esIndex, createNativeRaws)
|
||
|
time.Sleep(time.Second * 1)
|
||
|
}
|