github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/influxdb/v2/example_test.go (about)

     1  package client
     2  
     3  /*
     4  // Create a new client
     5  func ExampleClient() {
     6  	// NOTE: this assumes you've setup a user and have setup shell env variables,
     7  	// namely INFLUX_USER/INFLUX_PWD. If not just omit Username/Password below.
     8  	_, err := client.NewHTTPClient(client.HTTPConfig{
     9  		Addr:     "http://localhost:8086",
    10  		Username: os.Getenv("INFLUX_USER"),
    11  		Password: os.Getenv("INFLUX_PWD"),
    12  	})
    13  	if err != nil {
    14  		fmt.Println("Error creating InfluxDB Client: ", err.Error())
    15  	}
    16  }
    17  
    18  // Write a point using the UDP client
    19  func ExampleClient_uDP() {
    20  	// Make client
    21  	config := client.UDPConfig{Addr: "localhost:8089"}
    22  	c, err := client.NewUDPClient(config)
    23  	if err != nil {
    24  		fmt.Println("Error: ", err.Error())
    25  	}
    26  	defer c.Close()
    27  
    28  	// Create a new point batch
    29  	bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
    30  		Precision: "s",
    31  	})
    32  
    33  	// Create a point and add to batch
    34  	tags := map[string]string{"cpu": "cpu-total"}
    35  	fields := map[string]interface{}{
    36  		"idle":   10.1,
    37  		"system": 53.3,
    38  		"user":   46.6,
    39  	}
    40  	pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
    41  	if err != nil {
    42  		fmt.Println("Error: ", err.Error())
    43  	}
    44  	bp.AddPoint(pt)
    45  
    46  	// Write the batch
    47  	c.Write(bp)
    48  }
    49  
    50  // Ping the cluster using the HTTP client
    51  func ExampleClient_Ping() {
    52  	// Make client
    53  	c, err := client.NewHTTPClient(client.HTTPConfig{
    54  		Addr: "http://localhost:8086",
    55  	})
    56  	if err != nil {
    57  		fmt.Println("Error creating InfluxDB Client: ", err.Error())
    58  	}
    59  	defer c.Close()
    60  
    61  	_, _, err = c.Ping(0)
    62  	if err != nil {
    63  		fmt.Println("Error pinging InfluxDB Cluster: ", err.Error())
    64  	}
    65  }
    66  
    67  // Write a point using the HTTP client
    68  func ExampleClient_write() {
    69  	// Make client
    70  	c, err := client.NewHTTPClient(client.HTTPConfig{
    71  		Addr: "http://localhost:8086",
    72  	})
    73  	if err != nil {
    74  		fmt.Println("Error creating InfluxDB Client: ", err.Error())
    75  	}
    76  	defer c.Close()
    77  
    78  	// Create a new point batch
    79  	bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
    80  		Database:  "BumbleBeeTuna",
    81  		Precision: "s",
    82  	})
    83  
    84  	// Create a point and add to batch
    85  	tags := map[string]string{"cpu": "cpu-total"}
    86  	fields := map[string]interface{}{
    87  		"idle":   10.1,
    88  		"system": 53.3,
    89  		"user":   46.6,
    90  	}
    91  	pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
    92  	if err != nil {
    93  		fmt.Println("Error: ", err.Error())
    94  	}
    95  	bp.AddPoint(pt)
    96  
    97  	// Write the batch
    98  	c.Write(bp)
    99  }
   100  
   101  // Create a batch and add a point
   102  func ExampleBatchPoints() {
   103  	// Create a new point batch
   104  	bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
   105  		Database:  "BumbleBeeTuna",
   106  		Precision: "s",
   107  	})
   108  
   109  	// Create a point and add to batch
   110  	tags := map[string]string{"cpu": "cpu-total"}
   111  	fields := map[string]interface{}{
   112  		"idle":   10.1,
   113  		"system": 53.3,
   114  		"user":   46.6,
   115  	}
   116  	pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
   117  	if err != nil {
   118  		fmt.Println("Error: ", err.Error())
   119  	}
   120  	bp.AddPoint(pt)
   121  }
   122  
   123  // Using the BatchPoints setter functions
   124  func ExampleBatchPoints_setters() {
   125  	// Create a new point batch
   126  	bp, _ := client.NewBatchPoints(client.BatchPointsConfig{})
   127  	bp.SetDatabase("BumbleBeeTuna")
   128  	bp.SetPrecision("ms")
   129  
   130  	// Create a point and add to batch
   131  	tags := map[string]string{"cpu": "cpu-total"}
   132  	fields := map[string]interface{}{
   133  		"idle":   10.1,
   134  		"system": 53.3,
   135  		"user":   46.6,
   136  	}
   137  	pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
   138  	if err != nil {
   139  		fmt.Println("Error: ", err.Error())
   140  	}
   141  	bp.AddPoint(pt)
   142  }
   143  
   144  // Create a new point with a timestamp
   145  func ExamplePoint() {
   146  	tags := map[string]string{"cpu": "cpu-total"}
   147  	fields := map[string]interface{}{
   148  		"idle":   10.1,
   149  		"system": 53.3,
   150  		"user":   46.6,
   151  	}
   152  	pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
   153  	if err == nil {
   154  		fmt.Println("We created a point: ", pt.String())
   155  	}
   156  }
   157  
   158  // Create a new point without a timestamp
   159  func ExamplePoint_withoutTime() {
   160  	tags := map[string]string{"cpu": "cpu-total"}
   161  	fields := map[string]interface{}{
   162  		"idle":   10.1,
   163  		"system": 53.3,
   164  		"user":   46.6,
   165  	}
   166  	pt, err := client.NewPoint("cpu_usage", tags, fields)
   167  	if err == nil {
   168  		fmt.Println("We created a point w/o time: ", pt.String())
   169  	}
   170  }
   171  
   172  // Write 1000 points
   173  func ExampleClient_write1000() {
   174  	sampleSize := 1000
   175  
   176  	// Make client
   177  	c, err := client.NewHTTPClient(client.HTTPConfig{
   178  		Addr: "http://localhost:8086",
   179  	})
   180  	if err != nil {
   181  		fmt.Println("Error creating InfluxDB Client: ", err.Error())
   182  	}
   183  	defer c.Close()
   184  
   185  	rand.Seed(42)
   186  
   187  	bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
   188  		Database:  "systemstats",
   189  		Precision: "us",
   190  	})
   191  
   192  	for i := 0; i < sampleSize; i++ {
   193  		regions := []string{"us-west1", "us-west2", "us-west3", "us-east1"}
   194  		tags := map[string]string{
   195  			"cpu":    "cpu-total",
   196  			"host":   fmt.Sprintf("host%d", rand.Intn(1000)),
   197  			"region": regions[rand.Intn(len(regions))],
   198  		}
   199  
   200  		idle := rand.Float64() * 100.0
   201  		fields := map[string]interface{}{
   202  			"idle": idle,
   203  			"busy": 100.0 - idle,
   204  		}
   205  
   206  		pt, err := client.NewPoint(
   207  			"cpu_usage",
   208  			tags,
   209  			fields,
   210  			time.Now(),
   211  		)
   212  		if err != nil {
   213  			println("Error:", err.Error())
   214  			continue
   215  		}
   216  		bp.AddPoint(pt)
   217  	}
   218  
   219  	err = c.Write(bp)
   220  	if err != nil {
   221  		fmt.Println("Error: ", err.Error())
   222  	}
   223  }
   224  
   225  // Make a Query
   226  func ExampleClient_query() {
   227  	// Make client
   228  	c, err := client.NewHTTPClient(client.HTTPConfig{
   229  		Addr: "http://localhost:8086",
   230  	})
   231  	if err != nil {
   232  		fmt.Println("Error creating InfluxDB Client: ", err.Error())
   233  	}
   234  	defer c.Close()
   235  
   236  	q := client.NewQuery("SELECT count(value) FROM shapes", "square_holes", "ns")
   237  	if response, err := c.Query(q); err == nil && response.Error() == nil {
   238  		fmt.Println(response.Results)
   239  	}
   240  }
   241  
   242  // Create a Database with a query
   243  func ExampleClient_createDatabase() {
   244  	// Make client
   245  	c, err := client.NewHTTPClient(client.HTTPConfig{
   246  		Addr: "http://localhost:8086",
   247  	})
   248  	if err != nil {
   249  		fmt.Println("Error creating InfluxDB Client: ", err.Error())
   250  	}
   251  	defer c.Close()
   252  
   253  	q := client.NewQuery("CREATE DATABASE telegraf", "", "")
   254  	if response, err := c.Query(q); err == nil && response.Error() == nil {
   255  		fmt.Println(response.Results)
   256  	}
   257  }
   258  */