Featured post

The Ultimate Cheat Sheet On Hadoop

Top 20 frequently asked questions to test your Hadoop knowledge given in the below Hadoop cheat sheet. Try finding your own answers and match the answers given here.




Question #1 

You have written a MapReduce job that will process 500 million input records and generate 500 million key-value pairs. The data is not uniformly distributed. Your MapReduce job will create a significant amount of intermediate data that it needs to transfer between mappers and reducers which is a potential bottleneck. A custom implementation of which of the following interfaces is most likely to reduce the amount of intermediate data transferred across the network?



A. Writable
B. WritableComparable
C. InputFormat
D. OutputFormat
E. Combiner
F. Partitioner
Ans: e




Question #2 

Where is Hive metastore stored by default ?


A. In HDFS
B. In client machine in the form of a flat file.
C. In client machine in a derby database
D. In lib directory of HADOOP_HOME, and requires HADOOP_CLASSPATH to be modified.
Ans: c




Question…

How to Use Python Try and Except Logic Correctly

In Python, using the Try and Except logic, you can avoid exceptions. Error-free programs save a lot of time. So it reduces defects in the production.  
...


How to Use Python Try and Except Logic Correctly


In Python what to do if specific errors occur is handled by using TRY and EXCEPT.  For this, you need to write some logic to handle this. If the programmer did not do this, then the default is for Python to print an error message and stops execution. 


So the responsibility of a programmer is upfront he/she has to find errors and to handle them correctly. This is possible if you use the TRY and EXCEPT.


Python Syntax for Try and Except.


try:
      c = a/b
except:
      c = 1000000

How 'Try' works here. The above syntax says that the Try ends with ':'. That means, in Python, that is the block of Try.
  • The programmer's logic comes in the Try block.
  • The Except: is another block. That means in this block programmer can specify some value. And that value populates when any error happens.

So far so good.


Try and Except Examples.

Example: 1

Below is the example to give the expected error in except.

try:
      c = a/b
except ZeroDivisionError:
      c = 1000000


You can specify error name in Except: When the error is other than this the 'C' will be assigned as '1000000'.


Example: 2

Below is the example to give more than one expected error in except.

try:
      c = a/b
except (ValueError, ZeroDivisionError):
      c = 1000000


Also, there can be many except statements associated with a single try:

Example: 3


Below is the example to give more than one except.
try:
      c = a/b
except ValueError:
    c = 0
except ZeroDivisionError:
      c = 1000000


And, as was mentioned, a variable can hold the value of the error to be caught:

Example: 4

Below is the example to assign an expected error to a variable.

k = ZeroDivisionError
try:
      c = a/b
except k:
      c = 1000000


If we left out the exception name, any exception that occurs will be caught and the exception code will be executed:


try:
      c = a/b
except:
      c = 0


Related Posts

Comments

Popular posts from this blog

AWS Vs Azure Load Balancers Top Insights

Hadoop File System Basic Commands

4 Important Skills You Need for Data Scientists

Hyperledger Fabric: 20 Real Interview Questions