You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@edgent.apache.org by ho...@apache.org on 2016/04/15 19:01:50 UTC
[2/5] incubator-quarks-website git commit: [QUARKS-141] Update
ambiguous 'mileage' references for Applying Different Processing Against a
Single Stream recipe
[QUARKS-141] Update ambiguous 'mileage' references for Applying Different Processing Against a Single Stream recipe
Project: http://git-wip-us.apache.org/repos/asf/incubator-quarks-website/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-quarks-website/commit/7acf93f6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-quarks-website/tree/7acf93f6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-quarks-website/diff/7acf93f6
Branch: refs/heads/master
Commit: 7acf93f628196e0426f1adc065b89407a34df23d
Parents: 9f33abf
Author: Queenie Ma <qu...@gmail.com>
Authored: Tue Apr 12 11:40:24 2016 -0700
Committer: Queenie Ma <qu...@gmail.com>
Committed: Thu Apr 14 09:04:21 2016 -0700
----------------------------------------------------------------------
...ecipe_different_processing_against_stream.md | 94 ++++++++++----------
1 file changed, 47 insertions(+), 47 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-quarks-website/blob/7acf93f6/site/recipes/recipe_different_processing_against_stream.md
----------------------------------------------------------------------
diff --git a/site/recipes/recipe_different_processing_against_stream.md b/site/recipes/recipe_different_processing_against_stream.md
index 8e2084e..76d502c 100644
--- a/site/recipes/recipe_different_processing_against_stream.md
+++ b/site/recipes/recipe_different_processing_against_stream.md
@@ -4,13 +4,13 @@ title: Applying different processing against a single stream
In the previous [recipe](recipe_value_out_of_range), we learned how to filter a stream to obtain the interesting sensor readings and ignore the mundane data. Typically, a user scenario is more involved, where data is processed using different stream operations. Consider the following scenario, for example.
-Suppose a package delivery company would like to monitor the gas mileage of their delivery trucks using embedded sensors. They would like to apply different analytics to the sensor data that can be used to make more informed business decisions. For instance, if a truck is reporting consistently poor mileage readings, the company might want to consider replacing that truck to save on gas costs. Perhaps the company also wants to convert the sensor readings to JSON format in order to easily display the data on a web page. It may also be interested in determining the expected gallons of gas used based on the current mileage.
+Suppose a package delivery company would like to monitor the gas mileage of their delivery trucks using embedded sensors. They would like to apply different analytics to the sensor data that can be used to make more informed business decisions. For instance, if a truck is reporting consistently poor gas mileage readings, the company might want to consider replacing that truck to save on gas costs. Perhaps the company also wants to convert the sensor readings to JSON format in order to easily display the data on a web page. It may also be interested in determining the expected gallons of gas used based on the current gas mileage.
-In this instance, we can take the stream of mileage sensor readings and apply multiple types of processing against it so that we end up with streams that serve different purposes.
+In this instance, we can take the stream of gas mileage sensor readings and apply multiple types of processing against it so that we end up with streams that serve different purposes.
## Setting up the application
-We assume that the environment has been set up following the steps outlined in the [Getting started guide](../docs/quarks-getting-started). Let's begin by creating a `DirectProvider` and `Topology`. We choose a `DevelopmentProvider` so that we can view the topology graph using the console URL (refer to the [Application console](../docs/console) page for a more detailed explanation of this provider). The gas mileage bounds, initial mileage value, and the number of miles in a typical delivery route have also been defined.
+We assume that the environment has been set up following the steps outlined in the [Getting started guide](../docs/quarks-getting-started). Let's begin by creating a `DirectProvider` and `Topology`. We choose a `DevelopmentProvider` so that we can view the topology graph using the console URL (refer to the [Application console](../docs/console) page for a more detailed explanation of this provider). The gas mileage bounds, initial gas mileage value, and the number of miles in a typical delivery route have also been defined.
```java
import java.text.DecimalFormat;
@@ -28,14 +28,14 @@ We assume that the environment has been set up following the steps outlined in t
public class ApplyDifferentProcessingAgainstStream {
/**
- * Gas mileage value bounds
+ * Gas mileage (in miles per gallon, or mpg) value bounds
*/
- static double MILEAGE_LOW = 7.0;
- static double MILEAGE_HIGH = 14.0;
+ static double MPG_LOW = 7.0;
+ static double MPG_HIGH = 14.0;
/**
* Initial gas mileage sensor value
*/
- static double INITIAL_MILEAGE = 10.5;
+ static double INITIAL_MPG = 10.5;
/**
* Hypothetical value for the number of miles in a typical delivery route
*/
@@ -47,54 +47,54 @@ We assume that the environment has been set up following the steps outlined in t
System.out.println(dp.getServices().getService(HttpServer.class).getConsoleUrl());
- Topology top = dp.newTopology("MileageSensor");
+ Topology top = dp.newTopology("GasMileageSensor");
// The rest of the code pieces belong here
}
}
```
-## Generating mileage sensor readings
+## Generating gas mileage sensor readings
-The next step is to simulate a stream of gas mileage readings using [`SimpleSimulatedSensor`](https://github.com/apache/incubator-quarks/blob/master/samples/utils/src/main/java/quarks/samples/utils/sensor/SimpleSimulatedSensor.java). We set the initial mileage and delta factor in the first two arguments. The last argument ensures that the sensor reading falls in an acceptable range (between 7.0 mpg and 14.0 mpg). In our `main()`, we use the `poll()` method to generate a flow of tuples (readings), where each tuple arrives every second.
+The next step is to simulate a stream of gas mileage readings using [`SimpleSimulatedSensor`](https://github.com/apache/incubator-quarks/blob/master/samples/utils/src/main/java/quarks/samples/utils/sensor/SimpleSimulatedSensor.java). We set the initial gas mileage and delta factor in the first two arguments. The last argument ensures that the sensor reading falls in an acceptable range (between 7.0 mpg and 14.0 mpg). In our `main()`, we use the `poll()` method to generate a flow of tuples (readings), where each tuple arrives every second.
```java
- // Generate a stream of mileage sensor readings
- SimpleSimulatedSensor mileageSensor = new SimpleSimulatedSensor(INITIAL_MILEAGE,
- 0.4, Ranges.closed(MILEAGE_LOW, MILEAGE_HIGH));
- TStream<Double> mileage = top.poll(mileageSensor, 1, TimeUnit.SECONDS);
+ // Generate a stream of gas mileage sensor readings
+ SimpleSimulatedSensor mpgSensor = new SimpleSimulatedSensor(INITIAL_MPG,
+ 0.4, Ranges.closed(MPG_LOW, MPG_HIGH));
+ TStream<Double> mpgReadings = top.poll(mpgSensor, 1, TimeUnit.SECONDS);
```
## Applying different processing to the stream
-The company can now perform analytics on the `mileage` stream and feed it to different functions.
+The company can now perform analytics on the `mpgReadings` stream and feed it to different functions.
-First, we can filter out mileage values that are considered poor and tag the resulting stream for easier viewing in the console.
+First, we can filter out gas mileage values that are considered poor and tag the resulting stream for easier viewing in the console.
```java
// Filter out the poor gas mileage readings
- TStream<Double> poorMileage = mileage
- .filter(mpg -> mpg <= 9).tag("filtered");
+ TStream<Double> poorMpg = mpgReadings
+ .filter(mpg -> mpg <= 9.0).tag("filtered");
```
If the company also wants the readings to be in JSON, we can easily create a new stream and convert from type `Double` to `JsonObject`.
```java
// Map Double to JsonObject
- TStream<JsonObject> json = mileage
+ TStream<JsonObject> json = mpgReadings
.map(mpg -> {
JsonObject jObj = new JsonObject();
- jObj.addProperty("mileage", mpg);
+ jObj.addProperty("gasMileage", mpg);
return jObj;
}).tag("mapped");
```
-In addition, we can calculate the estimated gallons of gas used based on the current mileage using `modify`.
+In addition, we can calculate the estimated gallons of gas used based on the current gas mileage using `modify`.
```java
- // Modify mileage stream to obtain a stream containing the estimated gallons of gas used
+ // Modify gas mileage stream to obtain a stream containing the estimated gallons of gas used
DecimalFormat df = new DecimalFormat("#.#");
- TStream<Double> gallonsUsed = mileage
+ TStream<Double> gallonsUsed = mpgReadings
.modify(mpg -> Double.valueOf(df.format(ROUTE_MILES / mpg))).tag("modified");
```
@@ -104,7 +104,7 @@ With each of these resulting streams, the company can perform further analytics,
```java
// Terminate the streams
- poorMileage.sink(mpg -> System.out.println("Poor mileage! " + mpg + " mpg"));
+ poorMpg.sink(mpg -> System.out.println("Poor gas mileage! " + mpg + " mpg"));
json.sink(mpg -> System.out.println("JSON: " + mpg));
gallonsUsed.sink(gas -> System.out.println("Gallons of gas: " + gas + "\n"));
```
@@ -116,18 +116,18 @@ We end our application by submitting the `Topology`.
When the final application is run, the output looks something like the following:
```
- JSON: {"mileage":9.5}
+ JSON: {"gasMileage":9.5}
Gallons of gas: 8.4
- JSON: {"mileage":9.2}
+ JSON: {"gasMileage":9.2}
Gallons of gas: 8.7
- Poor mileage! 9.0 mpg
- JSON: {"mileage":9.0}
+ Poor gas mileage! 9.0 mpg
+ JSON: {"gasMileage":9.0}
Gallons of gas: 8.9
- Poor mileage! 8.8 mpg
- JSON: {"mileage":8.8}
+ Poor gas mileage! 8.8 mpg
+ JSON: {"gasMileage":8.8}
Gallons of gas: 9.1
```
@@ -153,19 +153,19 @@ Let's see what the topology graph looks like. We can view it using the console U
import quarks.topology.TStream;
import quarks.topology.Topology;
- /**
+ /**
* Fan out stream and perform different analytics on the resulting streams.
*/
public class ApplyDifferentProcessingAgainstStream {
/**
- * Gas mileage value bounds
+ * Gas mileage (in miles per gallon, or mpg) value bounds
*/
- static double MILEAGE_LOW = 7.0;
- static double MILEAGE_HIGH = 14.0;
+ static double MPG_LOW = 7.0;
+ static double MPG_HIGH = 14.0;
/**
* Initial gas mileage sensor value
*/
- static double INITIAL_MILEAGE = 10.5;
+ static double INITIAL_MPG = 10.5;
/**
* Hypothetical value for the number of miles in a typical delivery route
*/
@@ -173,7 +173,7 @@ Let's see what the topology graph looks like. We can view it using the console U
/**
* Polls a simulated delivery truck sensor to periodically obtain
- * mileage readings (in miles/gallon). Feed the stream of sensor
+ * gas mileage readings (in miles/gallon). Feed the stream of sensor
* readings to different functions (filter, map, and modify).
*/
public static void main(String[] args) throws Exception {
@@ -182,32 +182,32 @@ Let's see what the topology graph looks like. We can view it using the console U
System.out.println(dp.getServices().getService(HttpServer.class).getConsoleUrl());
- Topology top = dp.newTopology("MileageSensor");
+ Topology top = dp.newTopology("GasMileageSensor");
- // Generate a stream of mileage sensor readings
- SimpleSimulatedSensor mileageSensor = new SimpleSimulatedSensor(INITIAL_MILEAGE,
- 0.4, Ranges.closed(MILEAGE_LOW, MILEAGE_HIGH));
- TStream<Double> mileage = top.poll(mileageSensor, 1, TimeUnit.SECONDS);
+ // Generate a stream of gas mileage sensor readings
+ SimpleSimulatedSensor mpgSensor = new SimpleSimulatedSensor(INITIAL_MPG,
+ 0.4, Ranges.closed(MPG_LOW, MPG_HIGH));
+ TStream<Double> mpgReadings = top.poll(mpgSensor, 1, TimeUnit.SECONDS);
// Filter out the poor gas mileage readings
- TStream<Double> poorMileage = mileage
+ TStream<Double> poorMpg = mpgReadings
.filter(mpg -> mpg <= 9.0).tag("filtered");
// Map Double to JsonObject
- TStream<JsonObject> json = mileage
+ TStream<JsonObject> json = mpgReadings
.map(mpg -> {
JsonObject jObj = new JsonObject();
- jObj.addProperty("mileage", mpg);
+ jObj.addProperty("gasMileage", mpg);
return jObj;
}).tag("mapped");
- // Modify mileage stream to obtain a stream containing the estimated gallons of gas used
+ // Modify gas mileage stream to obtain a stream containing the estimated gallons of gas used
DecimalFormat df = new DecimalFormat("#.#");
- TStream<Double> gallonsUsed = mileage
+ TStream<Double> gallonsUsed = mpgReadings
.modify(mpg -> Double.valueOf(df.format(ROUTE_MILES / mpg))).tag("modified");
// Terminate the streams
- poorMileage.sink(mpg -> System.out.println("Poor mileage! " + mpg + " mpg"));
+ poorMpg.sink(mpg -> System.out.println("Poor gas mileage! " + mpg + " mpg"));
json.sink(mpg -> System.out.println("JSON: " + mpg));
gallonsUsed.sink(gas -> System.out.println("Gallons of gas: " + gas + "\n"));