Spark基础操作
Posted energy1010
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spark基础操作相关的知识,希望对你有一定的参考价值。
Spark官网: http://spark.apache.org/
Spark是适用于大规模数据分析处理的框架, 通过DAG流程图的调度计算,适合处理批量Batch和流式Streaming数据, 相比于Hadoop速度提升100倍左右。
Spark提供四大框架 Spark SQL , SparkStreaming, MLib, GraphX,同时可以在Hadoop, Apache Mesos, Kubernets, Standalong, 或者云
目前,最新版本是 2.4.0 (Nov 02, 2018 )
Spark基础操作对象是RDD,其中RDDs提供两种类型的转换操作, 所有的转换操作transformations是懒操作,直到需要计算结果的时候才会进行转换操作
常用的转化操作
这里比较有用的是pipe 这个函数可以使用shell脚本处理RDD
Actions 操作函数
Action | Meaning |
---|---|
reduce(func) | Aggregate the elements of the dataset using a function func (which takes two arguments and returns one). The function should be commutative and associative so that it can be computed correctly in parallel. |
collect() | Return all the elements of the dataset as an array at the driver program. This is usually useful after a filter or other operation that returns a sufficiently small subset of the data. |
count() | Return the number of elements in the dataset. |
first() | Return the first element of the dataset (similar to take(1)). |
take(n) | Return an array with the first n elements of the dataset. |
takeSample(withReplacement, num, [seed]) | Return an array with a random sample of num elements of the dataset, with or without replacement, optionally pre-specifying a random number generator seed. |
takeOrdered(n, [ordering]) | Return the first n elements of the RDD using either their natural order or a custom comparator. |
saveAsTextFile(path) | Write the elements of the dataset as a text file (or set of text files) in a given directory in the local filesystem, HDFS or any other Hadoop-supported file system. Spark will call toString on each element to convert it to a line of text in the file. |
saveAsSequenceFile(path) (Java and Scala) |
Write the elements of the dataset as a Hadoop SequenceFile in a given path in the local filesystem, HDFS or any other Hadoop-supported file system. This is available on RDDs of key-value pairs that implement Hadoop‘s Writable interface. In Scala, it is also available on types that are implicitly convertible to Writable (Spark includes conversions for basic types like Int, Double, String, etc). |
saveAsObjectFile(path) (Java and Scala) |
Write the elements of the dataset in a simple format using Java serialization, which can then be loaded using
SparkContext.objectFile() . |
countByKey() | Only available on RDDs of type (K, V). Returns a hashmap of (K, Int) pairs with the count of each key. |
foreach(func) | Run a function func on each element of the dataset. This is usually done for side effects such as updating an Accumulator or interacting with external storage systems.
Note: modifying variables other than Accumulators outside of the foreach() may result in undefined behavior. See Understanding closures for more details. |
以上是关于Spark基础操作的主要内容,如果未能解决你的问题,请参考以下文章