快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Scala中如何实现文件读取、写入、控制台操作

这篇文章主要介绍了Scala中如何实现文件读取、写入、控制台操作,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

创新互联公司是一家专业提供友谊企业网站建设,专注与网站建设、成都做网站、H5场景定制、小程序制作等业务。10年已为友谊众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。

Scala文件读取

E盘根目录下scalaIO.txt文件内容如下:

Scala中如何实现文件读取、写入、控制台操作

文件读取示例代码:

 //文件读取
 val file=Source.fromFile("E:\\scalaIO.txt")
 for(line <- file.getLines)
 {
  println(line)
 }
 file.close

Scala中如何实现文件读取、写入、控制台操作

说明1:file=Source.fromFile(“E:\scalaIO.txt”),其中Source中的fromFile()方法源自 import scala.io.Source源码包,源码如下图:

Scala中如何实现文件读取、写入、控制台操作

file.getLines(),返回的是一个迭代器-Iterator;源码如下:(scala.io)

Scala中如何实现文件读取、写入、控制台操作

Scala 网络资源读取

 //网络资源读取
 val webFile=Source.fromURL("http://spark.apache.org")
 webFile.foreach(print)
 webFile.close()

fromURL()方法源码如下:

 /** same as fromURL(new URL(s))
 */
 def fromURL(s: String)(implicit codec: Codec): BufferedSource =
 fromURL(new URL(s))(codec)

读取的网络资源资源内容如下:




 
 
 

 
  Apache Spark™ - Lightning-Fast Cluster Computing

 




 


 
 
 

 
 
 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-32518208-2']);
 _gaq.push(['_trackPageview']);
 (function() {
 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();

 
 function trackOutboundLink(link, category, action) {
 try {
  _gaq.push(['_trackEvent', category , action]);
 } catch(err){}

 setTimeout(function() {
  document.location.href = link.href;
 }, 100);
 }
 

 
 













 
  
  
   Lightning-fast cluster computing
  
 

        Toggle navigation             
        
  • Download
  •          Libraries           
  • SQL and DataFrames
  •    
  • Spark Streaming
  •    
  • MLlib (machine learning)
  •    
  • GraphX (graph)
  •        
  • Third-Party Packages
  •                Documentation           
  • Latest Release (Spark 1.5.1)
  •    
  • Other Resources
  •         
  • Examples
  •          Community           
  • Mailing Lists
  •    
  • Events and Meetups
  •    
  • Project History
  •    
  • Powered By
  •    
  • Project Committers
  •    
  • Issue Tracker
  •         
  • FAQ
  •    
            
    Latest News
          
  • Submission is open for Spark Summit East 2016    (Oct 14, 2015)
  •    
  • Spark 1.5.1 released    (Oct 02, 2015)
  •    
  • Spark 1.5.0 released    (Sep 09, 2015)
  •    
  • Spark Summit Europe agenda posted    (Sep 07, 2015)
  •      Archive

             Download Spark         Built-in Libraries:   

         
  • SQL and DataFrames
  •   
  • Spark Streaming
  •   
  • MLlib (machine learning)
  •   
  • GraphX (graph)
  •      Third-Party Packages          Apache Spark™ is a fast and general engine for large-scale data processing.    

    Speed

        Run programs up to 100x faster than   Hadoop MapReduce in memory, or 10x faster on disk.  

     

      Spark has an advanced DAG execution engine that supports cyclic data flow and   in-memory computing.  

               Logistic regression in Hadoop and Spark        

    Ease of Use

        Write applications quickly in Java, Scala, Python, R.  

     

      Spark offers over 80 high-level operators that make it easy to build parallel apps.   And you can use it interactively   from the Scala, Python and R shells.  

               text_file = spark.textFile("hdfs://...")       text_file.flatMap(lambda line: line.split())       .map(lambda word: (word, 1))       .reduceByKey(lambda a, b: a+b)      Word count in Spark's Python API            

    Generality

        Combine SQL, streaming, and complex analytics.  

     

      Spark powers a stack of libraries including   SQL and DataFrames, MLlib for machine learning,   GraphX, and Spark Streaming.   You can combine these libraries seamlessly in the same application.  

                               

    Runs Everywhere

        Spark runs on Hadoop, Mesos, standalone, or in the cloud. It can access diverse data sources including HDFS, Cassandra, HBase, and S3.  

     

      You can run Spark using its standalone cluster mode, on EC2, on Hadoop YARN, or on Apache Mesos.   Access data in HDFS, Cassandra, HBase,   Hive, Tachyon, and any Hadoop data source.  

                 

    Community

     

      Spark is used at a wide range of organizations to process large datasets.   You can find example use cases at the Spark Summit   conference, or on the   Powered By   page.  

     

      There are many ways to reach the community:  

        
  • Use the mailing lists to ask questions.
  •   
  • In-person events include the Bay Area Spark meetup and   Spark Summit.
  •   
  • We use JIRA for issue tracking.
  •        

    Contributors

     

      Apache Spark is built by a wide set of developers from over 200 companies.   Since 2009, more than 800 developers have contributed to Spark!  

     

      The project's   committers   come from 16 organizations.  

     

      If you'd like to participate in Spark, or contribute to the libraries on top of it, learn   how to   contribute.  

         

    Getting Started

     

    Learning Spark is easy whether you come from a Java or Python background:

        
  • Download the latest release — you can run Spark locally on your laptop.
  •   
  • Read the quick start guide.
  •   
  •   Spark Summit 2014 contained free training videos and exercises.   
  •   
  • Learn how to deploy Spark on a cluster.
  •        Download Spark    
     Apache Spark, Spark, Apache, and the Spark logo are trademarks of  The Apache Software Foundation. Process finished with exit code 0
     //网络资源读取
     val webFile=Source.fromURL("http://www.baidu.com/")
     webFile.foreach(print)
     webFile.close()

    读取中文资源站点,出现编码混乱问题如下:(解决办法自行解决,本文不是重点)

    Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1

    感谢你能够认真阅读完这篇文章,希望小编分享的“Scala中如何实现文件读取、写入、控制台操作”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


    分享名称:Scala中如何实现文件读取、写入、控制台操作
    网页URL:http://6mz.cn/article/gpcdhg.html

    免费获取网站建设与品牌策划方案报价

    *主要业务范围包括:高端网站建设, 集团网站建设(网站建设网站制作)找网站建设公司就上快上网。
    提交需求

      联系我们

      028-86922220
    • 手机:13518219792
    • 地址:成都市太升南路288号锦天国际A幢1002号
    • 24小时服务热线:400-028-6601

      网站建设服务

    • 网页设计
    • 网站制作
    • 网站开发

      网站推广服务

    • 营销网站建设
    • 百度快速排名
    • 整站网站推广

      网站运维服务

    • 基础维护
    • 网站改版
    • 网站维护

      FOLLOW US

    • 微信二维码

      微信二维码

    Copyright © 2022 成都快上网科技有限公司 成都网站建设公司-选网站建设公司快上网!国内专业的网站制作公司!
    All Rights Reserved 版权所有 蜀ICP备19037934号-11