func (c *CommandLine) connect(cmd string) error { var cl *client.Client var u url.URL // Remove the "connect" keyword if it exists path := strings.TrimSpace(strings.Replace(cmd, "connect", "", -1)) // If they didn't provide a connection string, use the current settings if path == "" { path = net.JoinHostPort(c.Host, strconv.Itoa(c.Port)) } var e error u, e = client.ParseConnectionString(path, c.Ssl) if e != nil { return e } config := client.NewConfig() config.URL = u config.Username = c.Username config.Password = c.Password config.UserAgent = "InfluxDBShell/" + version cl, err := client.NewClient(config) if err != nil { return fmt.Errorf("Could not create client %s", err) } c.Client = cl if _, v, e := c.Client.Ping(); e != nil { return fmt.Errorf("Failed to connect to %s\n", c.Client.Addr()) } else { c.Version = v } return nil }
func TestSetWriteConsistency(t *testing.T) { t.Parallel() c := cli.New(CLIENT_VERSION) config := client.NewConfig() client, _ := client.NewClient(config) c.Client = client // set valid write consistency consistency := "all" c.SetWriteConsistency("consistency " + consistency) if c.WriteConsistency != consistency { t.Fatalf("WriteConsistency is %s but should be %s", c.WriteConsistency, consistency) } // set different valid write consistency and validate change consistency = "quorum" c.SetWriteConsistency("consistency " + consistency) if c.WriteConsistency != consistency { t.Fatalf("WriteConsistency is %s but should be %s", c.WriteConsistency, consistency) } // set invalid write consistency and verify there was no change invalidConsistency := "invalid_consistency" c.SetWriteConsistency("consistency " + invalidConsistency) if c.WriteConsistency == invalidConsistency { t.Fatalf("WriteConsistency is %s but should be %s", c.WriteConsistency, consistency) } }
func TestSetFormat(t *testing.T) { t.Parallel() c := cli.New(CLIENT_VERSION) config := client.NewConfig() client, _ := client.NewClient(config) c.Client = client // validate set non-default format f := "json" c.SetFormat("format " + f) if c.Format != f { t.Fatalf("Format is %s but should be %s", c.Format, f) } }
func NewInfluxClient(surl, db string) (*MyInfluxClient, error) { url, err := url.Parse(surl) if err != nil { log.Printf("Error parsing url: %s", err) return nil, err } cfg := client.NewConfig() cfg.URL = *url client, err := client.NewClient(cfg) if err != nil { log.Printf("Error connecting to influxdb: %s", err) return nil, err } return &MyInfluxClient{client, db}, nil }
func TestSetAuth(t *testing.T) { t.Parallel() c := cli.New(CLIENT_VERSION) config := client.NewConfig() client, _ := client.NewClient(config) c.Client = client u := "userx" p := "pwdy" c.SetAuth("auth " + u + " " + p) // validate CLI configuration if c.Username != u { t.Fatalf("Username is %s but should be %s", c.Username, u) } if c.Password != p { t.Fatalf("Password is %s but should be %s", c.Password, p) } }
func main() { defer seelog.Flush() url, err := url.Parse("http://localhost:8086") if err != nil { panic(err) } cfg := client.NewConfig() cfg.URL = *url client, err := client.NewClient(cfg) if err != nil { panic(err) } t := time.Now() var i int = 1 var size int = 1e5 data := make([]string, 0, size) data2 := make([]string, 0, size) data3 := make([]string, 0, size) data4 := make([]string, 0, size) for ; i < size; i++ { t = t.Add(time.Second) data = append(data, fmt.Sprintf("power_price_go3 value=%f %d", rand.Float64(), t.UnixNano())) data2 = append(data2, fmt.Sprintf("power_price_go4 value=%f %d", rand.Float64(), t.UnixNano())) data3 = append(data3, fmt.Sprintf("power_price_go5 value=%f %d", rand.Float64(), t.UnixNano())) data4 = append(data4, fmt.Sprintf("power_price_go6 value=%f %d", rand.Float64(), t.UnixNano())) } start := time.Now() seelog.Info("Start writing data") var wg sync.WaitGroup wg.Add(4) go write_in_batches(data, client, 10000, "marketdata", &wg) go write_in_batches(data2, client, 10000, "marketdata", &wg) go write_in_batches(data3, client, 10000, "marketdata", &wg) go write_in_batches(data4, client, 10000, "marketdata", &wg) wg.Wait() seelog.Infof("Took %s to write 4 times %d points", time.Since(start), len(data)) }
func TestSetPrecision(t *testing.T) { t.Parallel() c := cli.New(CLIENT_VERSION) config := client.NewConfig() client, _ := client.NewClient(config) c.Client = client // validate set non-default precision p := "ns" c.SetPrecision("precision " + p) if c.Precision != p { t.Fatalf("Precision is %s but should be %s", c.Precision, p) } // validate set default precision which equals empty string p = "rfc3339" c.SetPrecision("precision " + p) if c.Precision != "" { t.Fatalf("Precision is %s but should be empty", c.Precision) } }
// Import processes the specified file in the Config and writes the data to the databases in chunks specified by batchSize func (i *Importer) Import() error { // Create a client and try to connect config := client.NewConfig() config.URL = i.config.URL config.Username = i.config.Username config.Password = i.config.Password config.UserAgent = fmt.Sprintf("influxDB importer/%s", i.config.Version) cl, err := client.NewClient(config) if err != nil { return fmt.Errorf("could not create client %s", err) } i.client = cl if _, _, e := i.client.Ping(); e != nil { return fmt.Errorf("failed to connect to %s\n", i.client.Addr()) } // Validate args if i.config.Path == "" { return fmt.Errorf("file argument required") } defer func() { if i.totalInserts > 0 { log.Printf("Processed %d commands\n", i.totalCommands) log.Printf("Processed %d inserts\n", i.totalInserts) log.Printf("Failed %d inserts\n", i.failedInserts) } }() // Open the file f, err := os.Open(i.config.Path) if err != nil { return err } defer f.Close() var r io.Reader // If gzipped, wrap in a gzip reader if i.config.Compressed { gr, err := gzip.NewReader(f) if err != nil { return err } defer gr.Close() // Set the reader to the gzip reader r = gr } else { // Standard text file so our reader can just be the file r = f } // Get our reader scanner := bufio.NewScanner(r) // Process the DDL i.processDDL(scanner) // Set up our throttle channel. Since there is effectively no other activity at this point // the smaller resolution gets us much closer to the requested PPS i.throttle = time.NewTicker(time.Microsecond) defer i.throttle.Stop() // Prime the last write i.lastWrite = time.Now() // Process the DML i.processDML(scanner) // Check if we had any errors scanning the file if err := scanner.Err(); err != nil { return fmt.Errorf("reading standard input: %s", err) } return nil }
// Import processes the specified file in the Config and writes the data to the databases in chunks specified by batchSize func (i *Importer) Import() error { // Create a client and try to connect config := client.NewConfig() config.URL = i.config.URL config.Username = i.config.Username config.Password = i.config.Password config.UserAgent = fmt.Sprintf("influxDB importer/%s", i.config.Version) cl, err := client.NewClient(config) if err != nil { return fmt.Errorf("could not create client %s", err) } i.client = cl if _, _, e := i.client.Ping(); e != nil { return fmt.Errorf("failed to connect to %s\n", i.client.Addr()) } // Validate args if i.config.Path == "" { return fmt.Errorf("file argument required") } defer func() { if i.totalInserts > 0 { log.Printf("Processed %d commands\n", i.totalCommands) log.Printf("Processed %d inserts\n", i.totalInserts) log.Printf("Failed %d inserts\n", i.failedInserts) } }() // Open the file f, err := os.Open(i.config.Path) if err != nil { return err } defer f.Close() var r io.Reader // If gzipped, wrap in a gzip reader if i.config.Compressed { gr, err := gzip.NewReader(f) if err != nil { return err } defer gr.Close() // Set the reader to the gzip reader r = gr } else { // Standard text file so our reader can just be the file r = f } // Get our reader scanner := bufio.NewScanner(r) // Process the scanner i.processDDL(scanner) i.processDML(scanner) // Check if we had any errors scanning the file if err := scanner.Err(); err != nil { return fmt.Errorf("reading standard input: %s", err) } return nil }