<jupyter_start><jupyter_text>* Collect action<jupyter_code>inputList = [1,2,2,3,5,2]
inputRDD = sc.parallelize(inputList)
retrievedValues= inputRDD.collect()
print (inputRDD)
print (retrievedValues)<jupyter_output>ParallelCollectionRDD[0] at parallelize at PythonRDD.scala:195
[1, 2, 2, 3, 5, 2]<jupyter_text>* Count action<jupyter_code>print (inputRDD.count())
print (inputRDD.distinct().count())<jupyter_output>6
4<jupyter_text>* Count by value<jupyter_code>print (inputRDD.countByValue())<jupyter_output>defaultdict(<class 'int'>, {1: 1, 2: 3, 3: 1, 5: 1})<jupyter_text>* Tack action<jupyter_code>print(inputRDD.take(2))
print(inputRDD.take(3))<jupyter_output>[1, 2]
[1, 2, 2]<jupyter_text>* First action -return the first element in RDD<jupyter_code>print(inputRDD.first())<jupyter_output>1<jupyter_text>* Top action<jupyter_code>print(inputRDD.top(3))
print(inputRDD.top(4))<jupyter_output>[5, 3, 2]
[5, 3, 2, 2]<jupyter_text>* TakeOrdered action<jupyter_code>print(inputRDD.takeOrdered(3))
print(inputRDD.takeOrdered(4))
takeOrderedList = ['Paolo','Giovanni','Luca']
takeOrderedRDD = sc.parallelize(takeOrderedList)
print(takeOrderedRDD.takeOrdered(2,lambda s:len(s)))<jupyter_output>['Luca', 'Paolo']<jupyter_text>* Take sample action<jupyter_code>print(inputRDD.takeSample(True,2))<jupyter_output>[2, 2]<jupyter_text>* Reduce action<jupyter_code>print(inputRDD.reduce(lambda e1,e2:e1+e2))
def computeMax(v1,v2):
    if v1>v2:
        return v1
    else:
        return v2
    
print(inputRDD.reduce(computeMax))
print(inputRDD.reduce(lambda e1,e2: max(e1,e2)))
print(inputRDD.reduce(lambda e1,e2: e1 if e1>e2 else e2 ))<jupyter_output>5
5
5<jupyter_text>* Fold action<jupyter_code>foldInput =['This','is','a','test']
foldRDD = sc.parallelize(foldInput)
print(foldRDD.fold('',lambda s1,s2:s1+s2))
print(foldRDD.reduce(lambda s1,s2:s1+s2))<jupyter_output>Thisisatest
Thisisatest<jupyter_text>* Aggregate action<jupyter_code>zeroValue = (0,0)
sumCount=inputRDD.aggregate(zeroValue,\
                           lambda acc,e: (acc[0]+e,acc[1]+1),\
                           lambda p1,p2:(p1[0]+p2[0],p1[1]+p2[1]))
print(sumCount)<jupyter_output>(15, 6)
{'fraction_non_alphanumeric': 0.16419294990723562, 'fraction_numerical': 0.045454545454545456, 'mean_word_length': 7.8765432098765435, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '351908', 'license': 'Apache-2.0', 'n_tokens_mistral': 971, 'n_tokens_neox': 930, 'n_words': 126, 'path': 'class example/07Spark_basic_actions.ipynb', 'repo_name': 'Rexyyj/Big-data-for-Internet-applications', 'size': '2156'}