189 8069 5689

Scala的文件读取、写入、控制台操作的方法示例

本篇内容介绍了“Scala的文件读取、写入、控制台操作的方法示例”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联建站专业为企业提供宾川网站建设、宾川做网站、宾川网站设计、宾川网站制作等企业网站建设、网页设计与制作、宾川企业网站模板建站服务,十载宾川做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

Scala文件读取

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

文件读取示例代码:

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

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

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

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

      Lightning-fast cluster computing  

 

Latest News
   

Archive

 Built-in Libraries:  

   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(lambdaline: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.

 Spark SQL  Spark Streaming  MLlib (machine learning)  GraphX

Runs Everywhere

 Spark runs on Hadoop, Mesos, standalone, or in the cloud. It can access perse 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:

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 Spark

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://cdxtjz.cn/article/ggccih.html

联系我们

您好HELLO!
感谢您来到成都网站建设公司,若您有合作意向,请您为我们留言或使用以下方式联系我们, 我们将尽快给你回复,并为您提供真诚的设计服务,谢谢。
  • 电话:028- 86922220 18980695689
  • 商务合作邮箱:631063699@qq.com
  • 合作QQ: 532337155
  • 成都网站设计地址:成都市青羊区锣锅巷31号五金站写字楼6楼

小谭建站工作室

成都小谭网站建设公司拥有多年以上互联网从业经验的团队,始终保持务实的风格,以"帮助客户成功"为已任,专注于提供对客户有价值的服务。 我们已为众企业及上市公司提供专业的网站建设服务。我们不只是一家网站建设的网络公司;我们对营销、技术、管理都有自己独特见解,小谭建站采取“创意+综合+营销”一体化的方式为您提供更专业的服务!

小谭观点

相对传统的成都网站建设公司而言,小谭是互联网中的网站品牌策划,我们精于企业品牌与互联网相结合的整体战略服务。
我们始终认为,网站必须注入企业基因,真正使网站成为企业vi的一部分,让整个网站品牌策划体系变的深入而持久。