小程序原生组件—提升你的小程序体验
870
2023-08-04
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
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);
}
style="height:100px; width:auto; vertical-align: bottom; margin-top: 20px;">
Lightning-fast cluster computing
data-target="#navbar-collapse-1">
Toggle navigation
Libraries
Documentation
Community
(Oct 14, 2015)
(Oct 02, 2015)
(Sep 09, 2015)
(Sep 07, 2015)
Download Spark
Built-in Libraries:
Apache Spark™ is a fast and general engine for large-scale data processing.
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.
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)
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 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.
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
page.
There are many ways to reach the comYqumjmunity:
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
come from 16 organizations.
If you'd like to participate in Spark, or contribute to the libraries on top of it, learn
contribute.
Learning Spark is easy whether you come from a Java or Python background:
Spark Summit 2014 contained free training videos and exercises.
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://baidu.com/")
webFile.foreach(print)
webFile.close()
读取中文资源站点,出现编码混乱问题如下:(解决办法自行解决,本文不是重点)
Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~