问题是: 我在PostgreSQL中有一个数据库,我读取Golang中的所有数据并从中创建数组。问题是:如何将这个数组放入Influxdb?
答案 0 :(得分:0)
package main
import (
"database/sql"
"log"
_ "./pq"
"fmt"
)
type DbInfo struct {
id string
person_id int
timestamp int
age string
gender string
attention string
interest int
happines int
surprise int
anger int
disgust int
fear string
sadness int
neutrall string
}
var DBObject DbInfo
var DbArray []DbInfo
func main() {
db, err := sql.Open("****", "postgres://postgres:********/cam?
sslmode=disable")
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query(`SELECT avg(age), avg(id), avg(neutrall),avg(fear)
FROM public.stat`) //stringTimeDayAgoQuery )
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
//err := rows.Scan(&DBObject.id, &DBObject.person_id,
&DBObject.timestamp,&DBObject.age,
&DBObject.gender,&DBObject.attention,&DBObject.interest,&DBObject.happines,
&DBObject.surprise,&DBObject.anger,&DBObject.disgust,&DBObject.fear,
&DBObject.sadness,&DBObject.neutrall)
err := rows.Scan(&DBObject.age,&DBObject.id,&DBObject.neutrall,
&DBObject.fear)
if err != nil {
log.Fatal(err)
}
DbArray = append(DbArray, DBObject)
}
fmt.Println(DbArray)
} 在这段代码中。我从postgreSQL读取数据。我把它读作数组,现在我想将输出导入Influxdb。因为我想使用Grafana,它不适用于postgresql。