473,545 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to call a defined function with map()?

3 New Member
Hello,

I am trying to call a defined function with the map(). I understand that that the map function appl the same procedure to every item in an iterable data structure. But the pre-defined is not being called.

I have a defined function, like this:

Expand|Select|Wrap|Line Numbers
  1. def process (records):
  2.    fields = records.map(lambda x: x.split('ID:')[0].split('\n'))
  3.    return fields;
  4.  
Here is the map() and the defined function being called:

Expand|Select|Wrap|Line Numbers
  1. files = sc.wholeTextFiles('file:///data/*/*')
  2. records = files.map(lambda x: x[1])
  3. results = records.map(lambda x: process(x))
  4.  
There are 4,560 files loaded up by wholeTextFiles. Perhaps, I am missing something basic. Can you help?
Feb 26 '18 #1
4 1452
dwblas
626 Recognized Expert Contributor
List comprehension may work better
Expand|Select|Wrap|Line Numbers
  1. fields=[x.split('ID:')[0].split('\n') for x in records] 
but since you don't open and read the files (don't know what wholeTextFiles does) you won't have any data for list comprehension/map.
Feb 26 '18 #2
byr1288
3 New Member
Hi dwblas, thanks for your reply. WholeTextFiles = Read a directory of text files from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI. https://spark.apache.org/docs/2.0.2/...rkContext.html

It returns the file name and the contents of the file, a pairRDD.

I did a count on “records” and there are 4,650 files with data, so there is something there in “records”.

I am new to Python. If I had a RDD, how would I call a predefined function with the map(), so I can iterate through each data set in the RDD? This does not work: results = records.map(lam bda x: process(x))
Feb 27 '18 #3
dwblas
626 Recognized Expert Contributor
What happened when you tried the code I posted. This list comprehension replaces
Expand|Select|Wrap|Line Numbers
  1. records = files.map(lambda x: x[1])
so hopefully it will now work but I am shooting in he dark as don't know what files and records contains.
Expand|Select|Wrap|Line Numbers
  1. records=[x[1] for x in files] 
Feb 27 '18 #4
byr1288
3 New Member
Hi Dwblas, I was able to figure this out. I was calling a defined function that has a map() with the map(). I experienced that this call errors. I rewrote my defined function that does not include the map().

Expand|Select|Wrap|Line Numbers
  1. files = sc.wholeTextFiles ('file:///data/*/*')
  2. records = files.map(lambda x: x[1])
  3. results = records.map(lambda x: process_records(x))
  4.  
Mar 1 '18 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2554
by: Steve D | last post by:
I've looked all over but can't find a solid answer. I've got a function that runs from a View and when the function runs the first time it is calculating a Temperature for a group of Formulas. What I want to do is calculate this temperature in the UDF on the first pass and store it somewhere (table, global variable etc.). On every...
7
2337
by: Andrzej | last post by:
Is it possible to call a function which name is given by a string? Let assume that I created a program which call some functions for example void f1(void), void f2(void), void f3(void). After some time, I added new function void f4(void).
19
3808
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
6
30011
by: silverburgh.meryl | last post by:
Hi, I have a function called 'test' defined in A.py. How can I call that function test in my another file B.py? Thank you.
10
1537
by: MLH | last post by:
I would like to call a function that "returned" several values - all of which are relevant to the needs of a procedure on a form. I do understand that FN's return a single value. I'm wondering, is this a good place to employ a user-defined Type? The FN would reside in a global module. When called, it does return one value considered to be...
11
2847
by: dolphin | last post by:
Hi All! I have a question that how to call a function just using a string. For example There is a .cpp file named a.cpp.There are some functions::fun1() fun2() fun3(). I have another fucntion void funcall( char *pch). if I pass a argument char* p1="fun1" .How do I call the function fun1() using that string "fun1"that I pass.
12
2278
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and switch statements. My first approach was to rely on partial template specialization. Therefore, I have: // .h file template <int vSomeClass;
8
2257
Claus Mygind
by: Claus Mygind | last post by:
I keep getting "MyControl.displayMyWayPoint is not a function" error var Class = { create: function() { return function() { this.initialize.apply(this, arguments); } } }
0
1978
by: mohitgyl | last post by:
Hello, I am trying to call a stored procedure in a user defined function. CREATE FUNCTION test () RETURNS DECIMAL (15,4) LANGUAGE SQL READ SQL DATA EXTERNAL ACTION
0
7486
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7442
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7776
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6001
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5347
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4965
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3456
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
729
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.