百度智能云

All Product Document

          Time-Spatial Database

          Write Operation

          Write Single-Field Data Points

          Users can refer to the following code to write the single-field data points:

          Note: When the metric, field, tags and timestamp written are all the same, the value written later overrides the value written first.

          String METRIC = "wind";                                            // metric
          String TAG_KEY = "city";                                           // tag name
          String TAG_VALUE = "ShangHai";                                     // tag value
          String FIELD = "direction";                                        // field
           
          // create data points
          List<Datapoint> datapoints = Arrays.asList(new Datapoint()
                          .withMetric(METRIC)                                  // set Metric
                          .withField(FIELD)                                    // set data point field. It is optional, if not enter, use the default domain name value
                          .addTag(TAG_KEY, TAG_VALUE)                          // set Tag
                          .addDoubleValue(System.currentTimeMillis(), 0.1));   // add a data point
           
           
          tsdbClient.writeDatapoints(datapoints);

          A Datapoint object can add multiple data points at the same time, which use the same metric and tag and the same field. Multiple data with the same metric and tag can be put into the same Datapoint object to reduce payload.

          Datapoint datapoint = new Datapoint()
                        .withMetric(METRIC)                                      // set Metric
                        .withField(FIELD)                                        // set data point field. It is optional, if not enter, use the default domain name value
                        .addTag(TAG_KEY, TAG_VALUE)                              // set Tag
                        .addDoubleValue(System.currentTimeMillis(), 0.1)         // add a data point
                        .addDoubleValue(System.currentTimeMillis() + 1, 0.2);    // add another data point

          DataPoint objects can add data points of type double, long, and String. For the same field, if the value of one data type is written, the same field is not allowed to write to other data types.

          // add Double type data points
          Datapoint datapoint1 = new Datapoint()
                        .withMetric("wind")                                     // set Metric
                        .withField(FIELD)                                       // set data point field. It is optional, if not enter, use the default domain name value
                        .addTag(TAG_KEY, TAG_VALUE)                             // set Tag
                        .addDoubleValue(System.currentTimeMillis(), 0.1);       // add Double type data points
           
          // add Long type data points
          Datapoint datapoint2 = new Datapoint()
                        .withMetric("temperature")                              // set Metric
                        .addTag(TAG_KEY, TAG_VALUE)                             // set Tag
                        .addLongValue(System.currentTimeMillis(), 10L);         // add Long type data points
           
          // add String type data points
          Datapoint datapoint3 = new Datapoint()
                        .withMetric("humidity")                                 // set Metric
                        .addTag(TAG_KEY, TAG_VALUE)                             // set Tag
                        .addStringValue(System.currentTimeMillis(), "string");  // add String type data points

          Write Multiple-Field Data Points

          Different fields do not need to be written at the same time. It can be considered that different fields are independent. However, if you want to find out with one statement during the query, you need to ensure that metric, all tags and timestamps are consistent.

          Users can refer to the following code to write the multiple-field data points:

          String METRIC = "wind";                                              // metric
          String TAG_KEY = "city";                                             // tag name
          String TAG_VALUE = "ShangHai";                                       // tag value
          String FIELD_1 = "direction";                                        // FIELD 1
          String FIELD_2 = "speed";                                            // FIELD 2
          long TIME = System.currentTimeMillis();                              // time
          
          // add data points to FIELD_1
          Datapoint datapoint1 = new Datapoint()
                        .withMetric(METRIC)                                     // set Metric
                        .withField(FIELD_1)                                     // set FIELD 1
                        .addTag(TAG_KEY, TAG_VALUE)                             // set Tag
                        .addDoubleValue(TIME, 0.1);                             // write Double type data points in appointed time
          
          // add data points to FIELD_2
          Datapoint datapoint2 = new Datapoint()
                        .withMetric(METRIC)                                     // set Metric same with FIELD 1
                        .withField(FIELD_2)                                     // set FIELD 2      
                        .addTag(TAG_KEY, TAG_VALUE)                             // set Tag same with FIELD 1         
                        .addLongValue(TIME, 10L);                               // add Long type data points, and the time is same with FIELD_1
          tsdbClient.writeDatapoints(Arrays.asList(datapoint1, datapoint2));
          Previous
          TsdbClient Create a TsdbClient
          Next
          Query Operation