百度智能云

All Product Document

          Time-Spatial Database

          Query Operation

          Get the Metric

          The following code can get the metric list:

          // get the Metric
          GetMetricsResponse response = tsdbClient.getMetrics();
           
          // print the result
          for(String metric : response.getMetrics()) {
              System.out.println(metric);
          }

          Get the Field

          The following code can get the Field list:

          String METRIC = "metric";
          
          // get the Field
          GetFieldsResponse response = tsdbClient.getFields(METRIC);
          
          // print the result
          for(Map.Entry<String, FieldInfo> entry : response.getFields().entrySet()) {
              System.out.println(entry.getKey() + ":");
              System.out.println("\t" + entry.getValue().getType());
          }

          Get the Tag

          The following code can get the Tag list:

          String METRIC = "cpu_idle";                                   // set the metric of tag to get
           
          // get the tag
          GetTagsResponse response = tsdbClient.getTags(METRIC);
           
          // print the result
          for(Map.Entry<String, List<String>> entry : response.getTags().entrySet()) {
              System.out.println(entry.getKey() + ":");
              for(String tagValue : entry.getValue()) {
                  System.out.println("\t" + tagValue);
              }
          }

          Query Data Points

          The following code can query data points:

          String metric = "wind";
          String field = "direction";
          
          // build query objects
          List<Query> queries = Arrays.asList(new Query()               // create Query Objects
                  .withMetric(metric)                                   // set the metric
                  .withField(field)                                     // set the domain name of the query, and query the default domain if not set
                  .withFilters(new Filters()                            // create Filters Objects
                          .withRelativeStart("2 hours ago"))            // set the relative start time, here the setup is 2 hours ago
                  .withLimit(100)                                       // set number limits of the return data points
                  .addAggregator(new Aggregator()                       // create Aggregator Objects
                          .withName(TsdbConstants.AGGREGATOR_NAME_SUM)  // set the aggregator to Sum
                          .withSampling("1 second")));                  // set the sampling
          
          // query data
          QueryDatapointsResponse response = tsdbClient.queryDatapoints(queries);
          
          // print the result
          for (Result result : response.getResults()) {
              System.out.println("Result:");
              for (Group group : result.getGroups()) {
                  System.out.println("\tGroup:");
          
                  for (GroupInfo groupInfo : group.getGroupInfos()) {
                      System.out.println("\t\tGroupInfo:");
                      System.out.println("\t\t\tName:" + groupInfo.getName());
                  }
                  System.out.println("\t\tTimeAndValue:");
                  try {
                      for (Group.TimeAndValue timeAndValue : group.getTimeAndValueList()) {
                          if (timeAndValue.isDouble()) {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getDoubleValue() + "]");
                          } else if (timeAndValue.isLong()) {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getLongValue() + "]");
                          } else {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getStringValue() + "]");
                          }
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }

          Query Data Points in Multiple Fields

          The following code can query data points:

          tring metric = "wind";
          String field1 = "direction";
          String field2 = "speed";
          String tag = "city";
          
          // build query objects
          List<String> fields = Arrays.asList(field1, field2);
          List<Query> queries = Arrays.asList(new Query()             // create Query Objects
                  .withMetric(metric)                                 // set the metric
                  .withFields(fields)                                 // set domain name list of the query, and query the default domain if not set, which conflict with field
                  .withTags(Arrays.asList(tag))                       // set the tag list of the query, and do not query if not set,
                  .withFilters(new Filters()                          // create Filters Objects
                          .withRelativeStart("2 hours ago")           // set the relative start time, here the setup is 2 hours ago
                          .withRelativeEnd("1 second ago"))           // set the relative end time, and terminate till the current time by default if not set
                  .withLimit(100));
          
          // query data, the order of the return results is same with the filed to query
          QueryDatapointsResponse response = tsdbClient.queryDatapoints(queries);
          
          // print the result
          for (Result result : response.getResults()) {
              System.out.println("Result:");
              for (Group group : result.getGroups()) {
                  System.out.println("\tGroup:");
          
                  for (GroupInfo groupInfo : group.getGroupInfos()) {
                      System.out.println("\t\tGroupInfo:");
                      System.out.println("\t\t\tName:" + groupInfo.getName());
                  }
                  System.out.println("\t\tTimeAndValues:");
                  try {
                      for (Group.TimeAndValue timeAndValue : group.getTimeAndValueList()) {
                          System.out.print("\t\t\t[" + timeAndValue.getTime());
                          for (int index = 0; index < fields.size(); index++) {
                              System.out.print(", ");
                              if (!timeAndValue.isNull(index)) {
                                  if (timeAndValue.isDouble(index)) {
                                      System.out.print(timeAndValue.getDoubleValue(index));
                                  } else if (timeAndValue.isLong(index)) {
                                      System.out.print(timeAndValue.getLongValue(index));
                                  } else {
                                      System.out.print(timeAndValue.getStringValue(index));
          
                                  }
                              } else {
                                  System.out.print("null");
                              }
                          }
                          System.out.println("]");
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }

          Query Data Points and Corresponding Tags

          The following code can query data points and at the same time return the corresponding tags information:

          String metric = "wind";
          String field = "direction";
          String tagKey1 = "city";
          String tagKey2 = "province";                                           // build query objects
          List<Query> queries = Arrays.asList(new Query()                        // create query objects
                  .withMetric(metric)                                            // set the metric
                  .withField(field)                                              // set domain name of the query, and quey the default domain if not set
                  .withTags(Arrays.asList(tagKey1, tagKey2))                     // set the tag key list to return at the same time
                  .withFilters(new Filters()                                     // create Filters Objects
                          .withRelativeStart("2 hours ago"))                     // set the relative start time
                  .withLimit(100));                                              // set number limits of the return data points
          
          
          // query data
          QueryDatapointsResponse response = tsdbClient.queryDatapoints(queries);
          
          // print the result
          for (Result result : response.getResults()) {
              System.out.println("Result:");
              for (Group group : result.getGroups()) {
                  System.out.println("\tGroup:");
          
                  for (GroupInfo groupInfo : group.getGroupInfos()) {
                      System.out.println("\t\tGroupInfo:");
                      System.out.println("\t\t\tName:" + groupInfo.getName());
                  }
                  System.out.println("\t\tTimeAndValue:");
                  try {
                      for (Group.TimeAndValue timeAndValue : group.getTimeAndValueList()) {
                          // format for printing [TIME,FIELD_VALUE,TAG_1_VALUE,TAG_2_VALUE]
                          if (timeAndValue.isDouble()) {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getDoubleValue(0) + "," + timeAndValue.getStringValue(1) + "," + timeAndValue.getStringValue(2) + "]");
                          } else if (timeAndValue.isLong()) {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getLongValue(0) + "," + timeAndValue.getStringValue(1) + "," + timeAndValue.getStringValue(2) + "]");
                          } else {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getStringValue(0) + "," + timeAndValue.getStringValue(1) + "," + timeAndValue.getStringValue(2) + "]");
                          }
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }

          Query Data Points Using Interpolation

          The following code can interpolate the data points:

          String metric = "wind";
          String field = "direction";
          
          // build query objects
          List<Query> queries = Arrays.asList(new Query()                          // create Query Objects
                  .withMetric(metric)                                              // set the metric
                  .withField(field)                                                // set the domain name of the query, and quey the default domain if not set
                  .withFilters(new Filters()                                       // create Filters Objects
                          .withRelativeStart("2 hours ago")                        // set the relative start time
                          .withRelativeEnd("1 second ago"))                        // set the relative end time, and terminate till the current time by default if not set
                  .withFill(new Fill()
                          .withType(TsdbConstants.FILL_TYPE_LINEAR)                // set the interpolate type, here the setup is Linear
                          .withInterval("5 minutes")                               // set the interpolate interval
                          .withMaxWriteInterval("10 minutes")));                   // set the Max write interval
          
          // query data
          QueryDatapointsResponse response = tsdbClient.queryDatapoints(queries);
          
          // print the result
          for (Result result : response.getResults()) {
              System.out.println("Result:");
              for (Group group : result.getGroups()) {
                  System.out.println("\tGroup:");
          
                  for (GroupInfo groupInfo : group.getGroupInfos()) {
                      System.out.println("\t\tGroupInfo:");
                      System.out.println("\t\t\tName:" + groupInfo.getName());
                  }
                  System.out.println("\t\tTimeAndValue:");
                  try {
                      for (Group.TimeAndValue timeAndValue : group.getTimeAndValueList()) {
                          if (timeAndValue.isDouble()) {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getDoubleValue() + "]");
                          } else if (timeAndValue.isLong()) {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getLongValue() + "]");
                          } else {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getStringValue() + "]");
                          }
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }

          Original Data Points of the Paging Query

          TSDB supports two ways of paging query, one is marker + limit, the other is offset + limit. The marker + limit method can determine the starting position of the query at the index level through marker, so the query speed is faster and the query cost is less. The offset + limit method requires the query to be run before the location of the result set offset can be found, so the query cost is relatively high. When the offset value is too large, the query return time may become long.

          The following code is the paged query raw data point for marker + limit:

          String metric = "wind";
          String field = "direction";
          
          // build query objects
          Query query = new Query()                                                // create Query Objects
                  .withMetric(metric)                                              // set the metric
                  .withField(field)                                                // set domain name list of the query, and query the default domain if not set
                  .withFilters(new Filters()                                       // create Filters Objects
                          .withRelativeStart("1 week ago")                         // set the relative start time, here the setup is 1 week ago
                          .withRelativeEnd("1 second ago"));                       // set the relative end time, and terminate till the current time by default if not set
          
          while (true) {
              // query data
              QueryDatapointsResponse response = tsdbClient.queryDatapoints(Arrays.asList(query));
              // print the result
              Result result = response.getResults().get(0);
              System.out.println("Result:");
              for (Group group : result.getGroups()) {
                  System.out.println("\tGroup:");
                  for (GroupInfo groupInfo : group.getGroupInfos()) {
                      System.out.println("\t\tGroupInfo:");
                      System.out.println("\t\t\tName:" + groupInfo.getName());
                  }
                  System.out.println("\t\tTimeAndValue:");
                  try {
                      for (Group.TimeAndValue timeAndValue : group.getTimeAndValueList()) {
                          if (timeAndValue.isDouble()) {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getDoubleValue() + "]");
                          } else if (timeAndValue.isLong()) {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getLongValue() + "]");
                          } else {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getStringValue() + "]");
                          }
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              // if there is no next page data, exit the loop
              if (!result.isTruncated()) {
                  break;
              }
              // set the marker for the next page data
              query.setMarker(result.getNextMarker());
          }

          The following is the original data points of paging query of offset + limit:

          String metric = "wind";
          String field = "direction";
          int pageSize = 10;
          Query query = new Query()                                                // create Query Objects
                  .withMetric(metric)                                              // set the metric
                  .withField(field)                                                // set domain name of the query, and query the default domain if not set, which conflict with field
                  .withFilters(new Filters()                                       // create Filters Objects
                          .withRelativeStart("1 week ago")                         // set the relative start time, here the setup is 1 week ago
                          .withRelativeEnd("1 second ago"))                        // set the relative end time, and terminate till the current time by default if not set
                  .withLimit(pageSize)                                             // set the size of each page as 10
                  .withOffset(0);                                                  // query the data of page 1
          QueryDatapointsResponse response = tsdbClient.queryDatapoints(Arrays.asList(query));
          
          // query the data of page 2
          query.setOffset(pageSize * 1);
          response = tsdbClient.queryDatapoints(Arrays.asList(query));
          
          // query the data of page 3
          query.setOffset(pageSize * 2);
          response = tsdbClient.queryDatapoints(Arrays.asList(query));

          Use Label Filtering When Querying Data

          The code example is as follows:

          String metric = "wind";
          String field = "direction";
          
          // build query objects
          List<Query> queries = Arrays.asList(new Query()                          // create Query Objects
                  .withMetric(metric)                                              // set the metric
                  .withField(field)                                                // set domain name of the query, and query the default domain if not set, which conflict with field
                  .withFilters(new Filters()                                       // create Filters Objects
                          .withRelativeStart("2 hours ago")                        // set the relative start time, here the setup is 2 hours ago
                          .withRelativeEnd("1 second ago")                         // set the relative end time, and terminate till the current time by default if not set
                          .addTagFilter(new TagFilter()                            // create TagFilter Objects
                                  .withTag("tag1")                                 // set the tagKey to filter
                                  .addNotIn("value2"))));                          // set the tagValue that is not allowed to appear. Set the tagValue can appear, or set the like match.
          // query data
          QueryDatapointsResponse response = tsdbClient.queryDatapoints(queries);
          
          // print the result
          for (Result result : response.getResults()) {
              System.out.println("Result:");
              for (Group group : result.getGroups()) {
                  System.out.println("\tGroup:");
                  System.out.println("\t\tTimeAndValue:");
                  try {
                      for (Group.TimeAndValue timeAndValue : group.getTimeAndValueList()) {
                          if (timeAndValue.isDouble()) {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getDoubleValue() + "]");
                          } else if (timeAndValue.isLong()) {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getLongValue() + "]");
                          } else {
                              System.out.println("\t\t\t[" + timeAndValue.getTime() + "," + timeAndValue.getStringValue() + "]");
                          }
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }

          Example of Setting Filters Objects

          // use absolute time
          Filters filters1 = new Filters()
                  .withAbsoluteStart(System.currentTimeMillis() - 5000)
                  .withAbsoluteEnd(System.currentTimeMillis() - 1000);
           
          // add Tags: one by one
          Filters filters2 = new Filters()
                  .withRelativeStart("5 minutes ago")
                  .withRelativeEnd("2 minutes ago")
                  .addTag("tagKey1", "tagValue1", "tagValue2")
                  .addTag("tagKey2", "tagValue1");
           
          // add Tags: add via map
          Map<String, List<String>> tags = new HashMap<String, List<String>>();      // create tags
          tags.put("tagKey1", Arrays.asList("tagValue1", "tagValue2"));              // add the tag
          tags.put("tagKey2", Arrays.asList("tagValue1"));                           // add the tag
          Filters filters3 = new Filters()
                  .withRelativeStart("5 minutes ago")
                  .withRelativeEnd("2 minutes ago")
                  .withTags(tags);

          Example of Setting Filtered by Value

          // Filter the Long type data, and the supported comparators are >,<,>=,<=,= and !=
          Filters filters1 = new Filters()
                  .withAbsoluteStart(System.currentTimeMillis() - 5000)
                  .withAbsoluteEnd(System.currentTimeMillis() - 1000)
                  .withValue(ValueFilter.createValueFilter(ValueFilter.LESS_OR_EQUAL, 100L));    // the filter condition is "<= 100"
           
          // Filter the Double type data, and the supported comparators are >,<,>=,<=,= and !=
          Filters filters1 = new Filters()
                  .withAbsoluteStart(System.currentTimeMillis() - 5000)
                  .withAbsoluteEnd(System.currentTimeMillis() - 1000)
                  .withValue(ValueFilter.createValueFilter(ValueFilter.GREATER, 99.9));          // the filter condition is "> 99.9"
           
          // Filter the String type data, and the supported comparators are >,<,>=,<=,= and !=
          Filters filters1 = new Filters()
                  .withAbsoluteStart(System.currentTimeMillis() - 5000)
                  .withAbsoluteEnd(System.currentTimeMillis() - 1000)
                  .withValue(ValueFilter.createValueFilter(ValueFilter.EQUAL, "stringvalue"));   // the filter condition is "= 'stringvalue'"
           
          // compare the data with the value of tagKey1 for filtering, and the supported comparators are >,<,>=,<=,= and !=
          Filters filters1 = new Filters()
                  .withAbsoluteStart(System.currentTimeMillis() - 5000)
                  .withAbsoluteEnd(System.currentTimeMillis() - 1000)
                  .withValue(ValueFilter.createValueFilterOfTag(ValueFilter.EQUAL, "tagKey1"));   // the filter condition is "= tagKey1"
          
          // filter the united value used by all multi-field data, and the supported comparators are >,<,>=,<=,= and !=
          Filters filters1 = new Filters()
                  .withAbsoluteStart(System.currentTimeMillis() - 5000)
                  .withAbsoluteEnd(System.currentTimeMillis() - 1000)
                  .withValue(ValueFilter.createValueFilter(ValueFilter.LESS_OR_EQUAL, 100L));    // the filter condition is "<= 100"
           
          // filter the different values used by all multi-field data, and the supported comparators are >,<,>=,<=,= and !=
          FieldFilter fieldFilter1 = new FieldFilter("field1", ValueFilter.createValueFilter("<", 100L));
          FieldFilter fieldFilter2 = new FieldFilter("field2", ValueFilter.createValueFilter(">", 200L));
          Filters filters1 = new Filters()
                  .withAbsoluteStart(System.currentTimeMillis() - 5000)
                  .withAbsoluteEnd(System.currentTimeMillis() - 1000)
                  .withFields(Arrays.asList(fieldFilter1, fieldFilter2));                       // the filter condition is "field1< 100 && field2 > 200"
                  

          Query Involved Objects

          Query involved objects, including: Query object, Filters object, GroupBy object, and Aggregator object. For a detailed description of the object, see [Query data points] (TSDB/API Reference/Data API Description. Md # Query data points) in API Reference.

          SQL Query Interface

          In the SDK of version 0.10. 32, sql query is supported. Examples are as follows:

          String METRIC = "wind";
          String FIELD = "direction";
           
          String sql = "select " + FIELD + " from " + METRIC;
           
          // query data
          GetRowWithSqlResponse response = tsdbClient.getRowsWithSql(sql); 
            
          // print the headers
          for (Column column : response.getColumns()) {                            
              System.out.print(column.getName() + "\t");
          }
          System.out.println();
          // print the data rows
          for (List<Object> row : response.getRows()) {
              for (Object item : row) {
                  System.out.print(item.toString() + "\t");
              }
              System.out.println();
          }
          Previous
          Write Operation
          Next
          URLGenerate the Pre-Signed URL of Query Data Points