func (i *InfluxDB) Write(bp client.BatchPoints) error { bp.Database = i.Database if _, err := i.conn.Write(bp); err != nil { return err } return nil }
func ReadDB(c *client.Client, sdb, ddb, cmd string) client.BatchPoints { q := client.Query{ Command: cmd, Database: sdb, } //get type client.BatchPoints var batchpoints client.BatchPoints response, err := c.Query(q) if err != nil { fmt.Printf("Fail to get response from database, read database error: %s\n", err.Error()) } res := response.Results if len(res) == 0 { fmt.Printf("The response of database is null, read database error!\n") } else { res_length := len(res) for k := 0; k < res_length; k++ { //show progress of reading series count := len(res[k].Series) bar := pb.StartNew(count) for _, ser := range res[k].Series { //get type client.Point var point client.Point point.Measurement = ser.Name point.Tags = ser.Tags for _, v := range ser.Values { point.Time, _ = time.Parse(time.RFC3339, v[0].(string)) field := make(map[string]interface{}) l := len(v) for i := 1; i < l; i++ { if v[i] != nil { field[ser.Columns[i]] = v[i] } } point.Fields = field point.Precision = "s" batchpoints.Points = append(batchpoints.Points, point) } bar.Increment() time.Sleep(3 * time.Millisecond) } bar.FinishPrint("Read series has finished!\n") } batchpoints.Database = ddb batchpoints.RetentionPolicy = "default" } return batchpoints }
// Choose a random server in the cluster to write to until a successful write // occurs, logging each unsuccessful. If all servers fail, return error. func (i *InfluxDB) Write(bp client.BatchPoints) error { bp.Database = i.Database // This will get set to nil if a successful write occurs err := errors.New("Could not write to any InfluxDB server in cluster") p := rand.Perm(len(i.conns)) for _, n := range p { if _, e := i.conns[n].Write(bp); e != nil { log.Println("ERROR: " + e.Error()) } else { err = nil break } } return err }