所有文档

          规则引擎 Rule Engine

          创建-删除-修改规则

          创建规则

          请参考以下代码创建规则:

          // 准备相关的参数
          String name = "myRuleName";								// 规则名称
          String description = "my rule description";				// 规则描述
          String mqttEndpoint = "myendpoint";						// 消息来源的物接入endpoint地址
          
          // 选择哪些字段,可以是*,字段名,嵌套字段名
          String select = "name, temperature, info.location";
          
          // 消息来源于哪个mqtt topic
          String fromMqttTopic = "topic/mysensor01";
          String whereCondition = "temperature > 90";				// 约束条件
          
          // 消息转发至哪个mqtt topic或TSDB数据库
          String mqttDestinationTopic = "topic/mysensor01/warn";
          String tsdbDestinationUrl = "<put your tsdb url here>";
          
          // 准备CreateRuleRequest,并且填好数据
          CreateRuleRequest request = new CreateRuleRequest();
          request.setName(name);
          request.setDescription(description);
          request.setEndpoint(mqttEndpoint);
          request.setSelect(select);
          request.setFrom(fromMqttTopic);
          request.setWhere(whereCondition);
          
          // 准备好规则引擎的目的地,一个MQTT,一个TSDB
          List<Destination> destinations = new ArrayList<Destination>();
          Destination mqttDestination = new Destination();
          mqttDestination.setKind(DestinationKind.MQTT);
          mqttDestination.setValue(mqttDestinationTopic);
          destinations.add(mqttDestination);
          
          Destination tsdbDestination = new Destination();
          tsdbDestination.setKind(DestinationKind.TSDB);
          tsdbDestination.setValue(tsdbDestinationUrl);
          destinations.add(tsdbDestination);
          
          request.setDestinations(destinations);
          
          // 调用创建规则接口, 返回创建好的规则
          Rule rule = client.createRule(request);
          
          // 打印新规则的id
          System.out.println("新规则的id=" + rule.getUuid());
          
          // 打印目的地(destination)的id
          for (Destination d: rule.getDestinations()) {
          	System.out.println(d.getUuid());
          }

          删除规则

          请参考以下代码删除规则:

          // 创建client
          RuleEngineClient client = new RuleEngineClient(accessKey, secretKey);
          
          // 要删除的规则的id
          String ruleId = "<place your rule id here>";
          DeleteRulesRequest request = new DeleteRulesRequest();
          
          // 删除是个批量操作,可以一次删除多个规则,所以ruleIds是一个列表
          List<String> ruleIds = new ArrayList<String>();
          ruleIds.add(ruleId);
          request.setRules(ruleIds);
          
          client.deleteRule(request);

          修改规则

          修改规则信息

          请参考以下代码修改规则:

          // 创建client
          RuleEngineClient client = new RuleEngineClient(accessKey, secretKey);
          
          UpdateRuleRequest request = new UpdateRuleRequest();
          request.setUuid("rule id that need to be updated");		// 被修改的规则ID
          request.setDescription("new description");				// 修改规则描述
          request.setFrom("new mqtt topic");						// 修改消息来源topic
          request.setSelect("*");									// 修改消息选取规则
          request.setWhere("temperature > 95");					// 修改约束条件
          
          client.updateRule(request);

          为规则增加一个目的地

          请参考以下代码为规则增加一个目的地:

          // 创建client
          RuleEngineClient client = new RuleEngineClient(accessKey, secretKey);
          
          Destination destination = new Destination();
          
          // 指定目的地加到哪个rule中去
          destination.setRuleUuid("the rule id here");
          
          // 目的地为百度托管KAFKA目的地
          destination.setKind(DestinationKind.KAFKA);
          String kafkaTopic = "your baidu kafka topic";
          destination.setValue("kafkaTopic");
          client.createDestination(destination);

          从规则删除一个目的地

          请参考以下代码为规则删除一个目的地:

          // 创建client
          RuleEngineClient client = new RuleEngineClient(accessKey, secretKey);
          
          // 需要删除的额目的地ID
          String destinationId = "your destination id";
          client.deleteDestination(destinationId);
          上一篇
          创建RuleEngineClient
          下一篇
          查看已经创建的规则