In Haskell, we have the syntax sugar ' do ' to help express the monad operation. We also have the corresponding syntax sugar ' for ' in Scala. The for expression will be transformed by Scala compiler, with the following simple examples: for { foo yield (A + B)===>Foo.flatmap ((a) + = {= = {+ b })})So we need to implement two methods Flatmap and map.or using the previous state Monad as an example, we add
1. Spark is an open-source cluster computing system based on memory computing, which is designed to make data analysis faster. So the machine running spark should be as large as possible in memory, such as 96G or more.2. All operation of Spark is based on RDD, the operation is divided into 2 major categories: transformation and action.3. Spark provides an interface for interaction, similar to the use of the shell.4. Spark can optimize the iteration workload because the intermediate data is store
Dependency OperatorsInput and output a one-to-one operator, and the result of the RDD partition structure is not changed , mainly map, FlatMap;Input and output one-to-one operators, but the results of the RDD partition structure has changed, such as Union, coalesce;Select the operator of some element from the input, such as filter, distinct, subtract, sample.Wide dependency operatorWide dependencies involve the shuffle class, which produces the stage
, z:string): StringThe following types are produced:A: = = IntB: (Int) BooleanC: (Int) (String, String) stringPolymorphic Method TypesThe Polymorphic method type is internally expressed as [Tps]t,[tps] is a type parameter part [A1;: L1 Example 3.3.2 the following declaration:def Empty[a]: list[a]def union[a The following types are generated:Empty: [A;: Nothing Union: [A;: Nothing Type constructorThe inner representation of a type constructor is similar to a polymorphic method type. [+/-A1;: L1 E
function f:t->u. But only when the action operator is triggered does the F function perform operations on the data in a stage with other functions. V1 input F Conversion output V ' 1.(2) FlatMapConverts each element in the original RDD through the function f to the new element and merges the elements from each collection of the resulting RDD into a single collection. Internally created Flatmappedrdd (this, Sc.clean (f)).Figure 3-5 The small box represents a partition of the RDD, the
from --, and -- will not delete all repeated elements.
13> [1,2,3,4,2] -- [2,3].[1,4,2]
4) lists: flatten/1
This is a flat list function, which has performance overhead. erlang's official documentation also describes:Lists: flatten/1 builds an entirely new list. Therefore, it is expensive, and even more expensive than the ++ (which copies its left argument, but not its right argument ).It copies all nested elements in DeepList to generate a new list, which is costly and is not recommended for l
need to call the corresponding interface and input data, it will be distributed and executed in the distributed system, and maximize the performance. At the end of the program, you must call the stop method to disconnect the environment.
Method textFile reads a text file and creates an RDD set in the Spark environment. This dataset is stored in the lines variable. The flatMap method is different from the map method. The map returns a key-value pair,
= ["ForrestWoo","Swift1"] let str = "My name is ForrestWoo,I am learning Swift" let query = arr.contains(str.containsString) print(query)
Read an object
Let path = NSBundle. mainBundle (). pathForResource ("filter", ofType: "rtf ")
Let lines = try? String (contentsOfFile: path !). Characters. split {$0 = "\ n"}. map (String. init)
For item in lines! {
Print (item)
}
Happy birthday to you
let name = "Forrest" (1...4).forEach{print("Happy Birthday " + ((
http://blog.csdn.net/jdsjlzx/article/details/51685769RxJava (a) create operator usage and source code analysis http://blog.csdn.net/johnny901114/article/details/51524470RxJava (B) map operator Usage Details http://blog.csdn.net/johnny901114/article/details/51531348RxJava (III) the usage of the flatMap operator is described in detail in comparison () and. flatMap () comparison of http://www.jianshu.com/p/6d
}
Scenario Two: Multiple asynchronous requests for continuous invocationThis kind of scene is also very common, we do the use of the user Picture editor, generally there will be three requests need continuous calls:
Request the address of the Avatar upload
Upload Avatar
Update user Information
In the normal code, we need to step by step callback nested down, code lengthy too ugly, and not good maintenance, using Rxjava chain call processing code logic will be ver
application of our custom function interface to each element of the Integerjavardd, adds 1 to each element as follows:
javardd It is important to note that the map operation can return data with different types of rdd, such as the following, which returns a custom user object: publicclassuserimplementsserializable{private Stringuserid;privateintegeramount;public user (Stringuserid,integeramount) { this.userId=userId;this.amount= amount;}//gettersetter....@ Overridepublicstringtostring ()
application of our custom function interface to each element of the Parallelize_rdd, adds 1 to each element as follows:
Map_rdd = Parallelize_rdd.map (lambda x:x + 1) "" "Result: [[2, 3], [4, 4, 5]]" "" print "Map_rdd = {0}". Format (Map_rdd.glo M (). Collect ())It is important to note that the map operation can return data with different types of rdd, such as the following, which returns a String type object:Map_string_rdd = Parallelize_rdd.map (lambda x: "{0}-{1}". Format (x, "test") ""
) { if ("007"). Equals (Man.getid ())) { return man ; } } return NULL ; }The following improvements are: Public Man GetById8 (list Mans) { return mans.stream (). Filter (M--"Oo7". Equals ( M.getid ()). FindFirst (). OrElse (null); }3, continue, get the name called Zhang San (because of the same name) all the bank cards, here does not discuss the actual business significance, only the technology, haha, with for is the case. Public L
Exercise: Flatten the following dictionary and output it to the target dictionary formatSource = {' A ': {' B ': 1, ' C ': 2}, ' d ': {' E ': 3, ' F ': {' G ': 4}}}target = {' a.b ': 1, ' D.F.G ': 4, ' D.E ': 3, ' A.C ': 2}Source = {' A ': {' B ': 1, ' C ': 2}, ' d ': {' E ': 3, ' F ': {' g ': 4}}}target = {}def Flatmap (srcdic, targetkey= '): for K, v I n Srcdic.items (): if Isinstance (V, dict):
information of the House to the screen addhouseinformationtoscreen (houses);}); }}}}}.start (); The way to use Rxjava is this:Observable.from (Getcommunitiesfromserver ()). FlatMap (New func1@OverridePublic observablePager(Community Community) {return observable.from (community.houses);} }). Filter (new Func1 @Override public Boolean call return house.price>= 5000000;} }). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mai
pipeline triggers an abort operation, otherwise the intermediate operation will not perform any processing! It is called "lazy evaluation" when it is processed all at once when terminating the operation.Filtering and slicingUse//筛选年龄MappingExample Application
Map
@Test public void test1() { List
FlatMap
//flatmap @Test public void Test3() { ListSortPractic
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.