Scala's simple computational example, its advantages in data analysis

Source: Internet
Author: User
Tags readfile

The program simply reads the data from the file and calculates it.

Package com.bill.www/** * Created by Bill on 2016/2/3. * Purpose: Simple data calculation using Scala * source file: Interface record number of 20, including timestamp and floating-point data * execution: Scala Readfile.scala "E:\\spark\\data\\i_22_221000000073_l_ 20151016\\i_22_221000000073_l_20151016_223458.dat "* Development environment: WIN10 + IJ IDEA15.0.2 + scala2.11.7 + java1.7 */import scala.c Ollection.mutableimport scala.io.Sourceobject ReadFile {def main (args:array[string]) {println ("Hello--------------- ----------------------------------------")//Read the file and print its contents and length line by row if (Args.length > 0) {for (lines <-SOURCE.F Romfile (args (0)). Getlines) Print (Line.length + "\t--" +line + "\ n")} else Console.err.println ("please    Enter a file or file with directory ") println ("--------------------------------------------------------1st. ") Using Arraybuffer to save a column of data, the object is implemented by some operations to achieve a variety of calculations val nums = mutable.        Arraybuffer[float] () if (Args.length > 0) {for (line <-source.fromfile (args (0)). getlines) {//Progressive processing Split field, type conversion, append to variable array nums + = Line.split ("") (1). Tofloat}} else Console.err.println ("Please enter a file or file with directory") Do some simple calculations and output the calculation results//Common calculate println (nums.sum) println (nums.length) println (Nums.max) printl N (nums.min)//Average println (nums.sum/nums.length) println ("---------------------------------------------------    -----2nd. ") Sort desc nums.sorted.reverse foreach (println) println ("-------------------------------------------------------- End. ")}}

The results of the implementation are as follows:

Hello-------------------------------------------------------21--22:34:58.0000 23.929920--22:34:58.0005 23.92721- -22:34:58.0010 23.927721--22:34:58.0015 23.928420--22:34:58.0020 23.92721--22:34:58.0025 23.925620--22:34:58.0030 23.92720--22:34:58.0035 23.92721--22:34:58.0040 23.926321--22:34:58.0045 23.928421--22:34:58.0050 23.927721-- 22:34:58.0055 23.926321--22:34:58.0060 23.929121--22:34:58.0065 23.925621--22:34:58.0070 23.927721--22:34:58.0075 23.925621--22:34:58.0080 23.924121--22:34:58.0085 23.926320--22:34:58.0090 23.92721--22:34:58.0095 23.9263-------- ------------------------------------------------1st.478.542023.929923.924123.927----------------------------------------- ---------------2nd.23.929923.929123.928423.928423.927723.927723.927723.92723.92723.92723.92723.92723.926323.926323.926323 .926323.925623.925623.925623.9241--------------------------------------------------------End.

Learning Communication in the beginner stage.

Scala's simple computational example, its advantages in data analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.