Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Calculation of Pi value of Beam Super practical examples

Shulou Source: shulou.com Published: 2022-06-03 03:15:07 09月20日 Update

Beam Pi value calculation

Beam has not been open source for a long time, almost 2 months. The current version is version 0.5.0. The official source code provides 4 examples. Unfortunately, these four cases are just four different implementations of WordCount. As an author who entered the hall of big data from Spark, I, who have used SparkPi many times, how can I put up with example without Pi? If you have this case, it is very convenient to test whether in the development tool or in the cluster. So there is the following. The author's writing style and technology are limited. If there are any inadequacies, I also hope that my friends will give me more suggestions. Let us come on .

Let's first talk about the implementation principle of Pi. We use the method of probability and statistics to achieve. First imagine drawing a circle with a unit as its radius and then drawing a circumscribed square of the circle. Suppose the bottom of a cup is filled with the square and the square inner tangent circle. Do n experiments, throw a stone into the cup and divide the number of times it falls into the circle by whether the total number of times is Pi*r*r/2r*2r or Pi/4.

Therefore, Pi is four times the probability.

The following is an experiment with the code. For reference only.

You can copy the code

Package org.tongfang.beam.examples

Import java.util.ArrayList

Import java.util.List

Import org.apache.beam.runners.spark.SparkRunner

Import org.apache.beam.sdk.Pipeline

Import org.apache.beam.sdk.options.PipelineOptions

Import org.apache.beam.sdk.options.PipelineOptionsFactory

Import org.apache.beam.sdk.transforms.*

Public class BeamPi {

Public static void main (String [] args) {

/ / Custom implementation of Beam Pi

/ / first step: create an options

/ / through this object, you can choose which computing framework to use for calculation, and set the name of the application.

PipelineOptions options = PipelineOptionsFactory.create ()

/ / set the job (application) name

Options.setJobName ("Beam Pi")

/ / set runner to Spark

Options.setRunner (SparkRunner.class)

/ / create pipe p

Pipeline p = Pipeline.create (options)

/ / the number of 100000000 randomized trials, if resources

/ / enough to carry out more experiments, according to big data's theory.

/ / in theory, countless experiments can be carried out (as long as the computing resources are constantly scaled out).

List list = new ArrayList ()

For (int I = 0 position I)

Tags: Experiment code probability number of times that is data square square iteration that is name cup case version theory author resources first come application different Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Apple Shulou Tech Info MariaDB MySQL