Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Calculation of Pi value of Beam Super practical examples

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology