
We define a pipeline that reads from a Map.Entry holding our (X, Y) coordinates. writeTo(MyUtils.buildTopicSink(Pi1Job.class, "pi")) aggregate(AggregateOperations.averagingDouble(string -> Double.parseDouble(string))) apply(PythonTransforms.mapUsingPython(getPythonServiceConfig("pi1"))) map(entry -> entry.getKey() + "," + entry.getValue())

readFrom(Sources.mapJournal("points", JournalInitialPosition.START_FROM_CURRENT)).withIngestionTimestamps() However, we need eviction on the target map so we don’t run out of storage space. This generates an unlimited number of points, which is what we need for our estimation calculation to become more accurate. We use the map as the input source for our pipeline. So a Tuple2 is a map entry, where X is the entry’s key and Y is the entry’s value. There is also Tuple3 for a trio of data items. Tuple2 tuple2 = Tuple2.tuple2(random.nextDouble(), random.nextDouble()) Ī Tuple2 is a Jet convenience class to hold a data pair. Here’s the Python code: for point in points: Conveniently, but not coincidentally, the radius is 1. To calculate whether the generated (X, Y) coordinate is within the circle, all we need do is add X squared to Y squared and see if these two values added together are less than or equal to the radius squared. We generate random points within the square where X varies randomly between -1 inclusive and +1 inclusive, and where Y varies randomly between -1 inclusive and +1 inclusive. Our circle has a radius of 1.0 and is centered on (0, 0). The more random points we generate, the better the approximation becomes. The proportion of points inside compared to outside multiplied by 4 gives us an approximation to Pi.

If we randomly generate points inside the square (the green crosses), some will be inside the circle and some won’t. Picture a circle that exactly fits inside a square so it touches at the edges, as per the diagram.

There are various ways, but here we’re going to use the “ Monte Carlo method.” Random points on a square that contains a circle It has apparently an infinite number of decimal places. Pi is the ratio of a circle’s radius to its circumference. To have some fun, let’s use Jet to drive multiple Python workers to calculate Pi with increasing accuracy. Today is PI DAY! Obviously, Pi has rather more than 2 decimal places.
