tag:blogger.com,1999:blog-76266674743158915032024-03-18T02:48:56.831-07:00JavaMakeUse: Java | Big Data | Scala | Hive | Spark | Hadoop | HBase | Solr | Spring | HibernateThis site is all about Java, Big Data, Hadoop, Spark, HBase, Hive, Solr, Big Data technologies, Spring Core,Spring MVC, Spring Web Service, Hibernate, Rest, SOA Web Service, MySQL, Security,Data Structure, JVM, XML, WSDL, Interview question plus tutorials. And also found very tricky and interesting puzzle asked in popular interview discussions, complete sample projects using different technologies such as Spring project, Spring+Hibernate projects, Spring+Hibernate+Web Service projectsjavamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.comBlogger185125tag:blogger.com,1999:blog-7626667474315891503.post-58966578151283013382016-12-31T08:14:00.001-08:002016-12-31T08:14:40.523-08:00Java program to find common elements between two arrays in java<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>
This is again a very useful and frequently used in project and also very popular in <a href="http://www.javamakeuse.com/2015/09/core-java-written-test-questions-and-answer.html" target="_blank">core Java written interview question</a>, not only in core Java but yes finding common elements between array is in any language always on demand. <br />
We know that, array is something in any programming language which is really a powerful and day by day used in project. I think without array no project will complete, almost every project has array..<br />
<br />
Lets compare two array to find the common elements, of course you can find out common elements with multiple ways and few of them are here listed in below program.<br />
We have two array something like,<br />
int [] array1 = [1,2,3,4,5,6];<br />
int [] array2 = [8,1,3,9,4,5]; <br />
Expected output: 1,3,4,5<br />
<!-- adsense --><br />
<pre class="brush:java">package com.javamakeuse.poc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* @author javamakeuse
*
*/
public class ArrayCompare {
public static void compareArray(Integer[] array1, Integer[] array2) {
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array2.length; j++) {
if (array1[i] == array2[j]) {
System.out.println(array1[i]);
}
}
}
}
public static void compareArray2(Integer[] array1, Integer[] array2) {
List<Integer> list1 = Arrays.asList(array1);
List<Integer> list2 = Arrays.asList(array2);
List<Integer> list3 = new ArrayList<>(list1);
list3.retainAll(list2);
System.out.println("list3 " + list3);
}
public static void main(String[] args) {
Integer[] array1 = { 1, 2, 3, 4, 5, 6 };
Integer[] array2 = { 8, 1, 3, 9, 4, 5 };
compareArray(array1, array2);
compareArray2(array1, array2);
}
}
</pre>
<b>OUTPUT</b>:<br />
1<br />
3<br />
4<br />
5<br />
list3 [1, 3, 4, 5]<br />
<br /></div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-84938122313068018662016-12-31T05:53:00.002-08:002016-12-31T05:53:59.453-08:00Java program to check even or odd number<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>Very basic Java coding practice or Java homework questions. While learning Java, we started with converting basic calculation to using Java program. like creating calculator using Java code, printing sum of digits and all. And sometimes this type of questions also asked in <a href="http://www.javamakeuse.com/2015/09/core-java-written-test-questions-and-answer.html" target="_blank">core Java written interview questions</a>, although this question is not as demanded as <a href="http://www.javamakeuse.com/2015/05/java-program-to-balanced-parentheses.html" target="_blank">how to balanced the parentheses</a> using Java program.<br />
<!-- adsense --><br />
<b><span style="font-size: large;">How to check a number is even or odd?</span></b><br />
<pre class="brush:java">package com.javamakeuse.poc;
import java.util.Scanner;
/**
*
* @author javamakeuse
*
*/
public class EvenOrOdd {
public static void evenOrOdd() {
System.out.println("Enter an Integer number:");
try (Scanner in = new Scanner(System.in)) {
int num = in.nextInt();
// if input number is divisible by 2
if (num % 2 == 0) {
System.out.println("Entered number is even");
} else {
System.out.println("Entered number is odd");
}
}
}
public static void main(String[] args) {
evenOrOdd();
}
}
</pre></div>javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-6972528353184049572016-12-31T05:32:00.001-08:002016-12-31T05:32:06.623-08:00Java program to find duplicate elements in an array<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>
Ok, sometimes while working, we face this problem to find the duplicate elements from an array, and also sometime interviewer just write on paper and asking you to write a program or code logic to find the duplicate elements from a given array and expecting some better solution from you or at-least expecting more than one solutions to discuss about the pros and cons of those.<br />
Of-course you can find duplicate elements from an array with number of different approach, but finding the better one is of-course a tedious task, to find better one you need good understating of time and space complexity and the parameters to calculate the time and space. Like we have seen a solution of balancing parentheses with o(n) time at <a href="http://www.javamakeuse.com/2015/05/java-program-to-balanced-parentheses.html" target="_blank">here</a>.<br />
<br />
You can find duplicate elements from an array at-least via two different ways but not only the limited and those are.<br />
<ul style="text-align: left;">
<li>Brute force method(O(n^2)</li>
<li>Set technique(O(n))</li>
</ul>
<b><span style="font-size: large;">Brute force:</span></b><br />
Brute force method also know as exhaustive search and easy to implement, it compares each element from an array to all other elements and this has the O(n^2) complexity.<br />
<!-- adsense --><br />
<b><span style="font-size: large;">Set technique: </span></b><br />
We know that the hashing technique is very fast, and we have Set api in Collection framework so one can easily utilized any implementation of Set to achieve this task, because set does not allow duplicate element in it. And it has O(n) complexity.<br />
<br />
Now lets' implement this<br />
<pre class="brush:java">package com.javamakeuse.poc;
import java.util.HashSet;
import java.util.Set;
/**
*
* @author javamakeuse
*
*/
public class ArrayExample {
static public void findDuplicate(String[] strArray) {
for (int i = 0; i < strArray.length; i++) {
for (int j = i + 1; j < strArray.length; j++) {
if (strArray[i].equals(strArray[j])) {
System.out.println("found duplicate element at index [" + j + "] " + strArray[i]);
}
}
}
}
public static void findDuplicate2(String[] strArray) {
Set<String> strSet = new HashSet<>();
for (String elemnt : strArray) {
if (strSet.add(elemnt) == false) {
System.out.println("found duplicate elemnt " + elemnt);
}
}
}
public static void main(String[] args) {
String[] strArray = new String[] { "one", "two", "three", "one" };
findDuplicate(strArray);
findDuplicate2(strArray);
}
}
</pre>
</div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-62335152731486381212016-12-31T04:06:00.000-08:002016-12-31T04:06:01.766-08:00Java program to check string palindrome<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>Nowadays in any interview beat experienced or fresher you will face at least 2-3 coding interview question as part of first round of <a href="http://www.javamakeuse.com/2015/09/core-java-written-test-questions-and-answer.html" target="_blank">technical written interview</a>. And to checking string is palindrome or not is one of the popular coding interview question like <a href="http://www.javamakeuse.com/2015/05/java-program-to-balanced-parentheses.html" target="_blank">balancing a parentheses</a> or <a href="http://www.javamakeuse.com/2015/10/java-program-to-find-factorial-of-number.html" target="_blank">finding factorial of given number</a>. So i would suggest everyone to do practice on such coding questions.<br />
Although, I have separately posted a complete set-up of Java interview questions <a href="http://www.javamakeuse.com/2015/11/java-j2ee-interview-questions-for.html" target="_blank">here</a> and in this post going to show you a simple Java program to check if string is palindrome or not. <br />
<!-- adsense --><br />
<b><span style="font-size: large;">Code to check if string is palindrome or not</span></b><br />
<pre class="brush:java">package com.javamakeuse.poc;
/**
*
* @author javamakeuse
*
*/
public class Palindrome {
public static boolean isPalindrome(String s) {
int n = s.length();
for (int i = 0; i < (n / 2); ++i) {
if (s.charAt(i) != s.charAt(n - i - 1)) {
return false;
}
}
return true;
}
public static boolean isPalindrome2(String str) {
if (str.equals(new StringBuilder(str).reverse().toString())) {
return true;
}
return false;
}
public static void main(String[] args) {
System.out.println(isPalindrome("696"));
System.out.println(isPalindrome("java"));
System.out.println(isPalindrome2("POP"));
}
}
</pre><b>OUTPUT</b>:<br />
true<br />
false<br />
true<br />
<br />
</div>javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-51989579164220796442016-12-31T03:41:00.000-08:002016-12-31T03:41:01.517-08:00Java program to print vowels in a string<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>
Very common java coding interview question for freshers. You will face this question while appearing in campus interview, this type of questions in first round of technical written interview. This question is also known as homework question for some of you. In my previous post, i had shared how to check if a string contains unique characters or not at <a href="http://www.javamakeuse.com/2016/12/java-program-to-check-string-has-all.html" target="_blank">here</a>. And also here is the list of <a href="http://www.javamakeuse.com/2015/09/core-java-written-test-questions-and-answer.html" target="_blank">written interview questions</a>. Nowadays only theory is not enough for even experienced candidates almost every organizations has come up with their set of predefined written questions to check the logical ability and coding standards of candidates. So be ready with some basic practical questions..<br />
<br />
<b><span style="font-size: large;">Printing vowels</span></b><br />
<pre class="brush:java">package com.javamakeuse.poc;
/**
*
* @author javamakeuse
*
*/
public class Vowel {
public static void vowel(String str) {
String lowerCase = str.toLowerCase();
for (int i = 0; i < lowerCase.length(); i++) {
if ((lowerCase.charAt(i) == 'a') || (lowerCase.charAt(i) == 'u') || (lowerCase.charAt(i) == 'o')
|| (lowerCase.charAt(i) == 'i') || (lowerCase.charAt(i) == 'e')) {
System.out.println(str.charAt(i));
}
}
}
public static void main(String[] args) {
vowel("The quick brown fox jumps over the lazy dog");
}
}
</pre>
<!-- adsense --><br />
<b>OUTPUT</b>:<br />
e<br />
u<br />
i<br />
o<br />
o<br />
u<br />
o<br />
e<br />
e<br />
a<br />
o<br />
<br /></div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-47396627978672302502016-12-31T03:06:00.003-08:002016-12-31T03:06:45.969-08:00Java program to check string has all unique character<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>
This question is very popular in the first round of technical interview for the freshers and also it's kind of homework activity. The coding practice is necessary to be logical on the logical questions like how to balanced parentheses and <a href="http://www.javamakeuse.com/2015/07/java-program-find-second-largest-number-from-an-array.html" target="_blank">find the second largest element from an array</a>. My suggestion for freshers to do always practice on such question like <a href="http://www.javamakeuse.com/2016/12/best-fibonacci-program-using-java.html" target="_blank">calculating Fibonacci series</a>, <a href="http://www.javamakeuse.com/2015/10/java-program-to-find-factorial-of-number.html" target="_blank">finding factorial of given number</a> and all.<br />
<br />
In this post, going to show you some simple approach to check if string contains duplicate characters. <br />
<!-- adsense --><br />
<pre class="brush:java">package com.javamakeuse.poc;
import java.util.HashSet;
/**
*
* @author javamakeuse
*
*/
public class UniqueChars {
public static boolean isUnique(String str) {
String lowerCase = str.toLowerCase();
HashSet<Character> charSet = new HashSet<Character>(lowerCase.length());
for (int i = 0; i < lowerCase.length(); i++) {
if (!charSet.add(lowerCase.charAt(i))) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isUnique("javamakeuse"));
}
}
</pre>
<b>OUTPUT</b>:<br />
false</div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-11192242803747590432016-12-30T23:07:00.000-08:002016-12-30T23:07:30.224-08:00Java program to reverse a string<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>
One of the popular interview question, and most of the time interviewer asked this question while first round of technical interview specially for freshers. Interviewer ask you to check your logical ability, once you provide the solution for this question, interview start asking you to provide some other way to reversing a string and start discussing why this approach is good and why other not and so on..<br />
Although, I had already shared a list of written Java interview questions <a href="http://www.javamakeuse.com/2015/09/core-java-written-test-questions-and-answer.html" target="_blank">here</a> and in this article going to show you how to reverse a string with multiple options.<br />
<br />
<b><span style="font-size: large;">Reverse a String </span></b><br />
Example,<br />
Input: gnirts a esrever ot woh<br />
Expected Output: how to reverse a string<br />
<br />
Input: esuekamavaj<br />
Expected Output: javamakeuse<br />
<!-- adsense --><br />
<pre class="brush:java">package com.javamakeuse.poc;
/**
*
* @author javamakeuse
*
*/
public class ReverseString {
// using built in function
public static String reverseString(String str) {
return new StringBuilder(str).reverse().toString();
}
// using character
public static String reverseString2(String str) {
String result = "";
for (int i = str.length() - 1; i >= 0; i--) {
result = result + str.charAt(i);
}
return result;
}
// using recursion
public static String reverseString3(String str) {
if (str.length() == 0)
return "";
return str.charAt(str.length() - 1) + reverseString3(str.substring(0, str.length() - 1));
}
public static void main(String[] args) {
System.out.println(reverseString("esuekamavaj"));
System.out.println(reverseString2("esuekamavaj"));
System.out.println(reverseString2("esuekamavaj"));
}
}
</pre>
<b>OUTPUT</b>:<br />
how to reverse a string<br />
javamakeuse<br />
javamakeuse<br />
<br /></div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-70637263493968355862016-12-30T06:33:00.002-08:002016-12-30T06:33:59.135-08:00Java program to add two numbers<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>
Very basic java coding practice or java homework questions. While learning Java, we started with converting basic calculation to using Java program. like creating calculator using Java code, printing sum of digits and all. And sometimes this type of questions also asked in <a href="http://www.javamakeuse.com/2015/09/core-java-written-test-questions-and-answer.html" target="_blank">core Java written interview questions</a>, although this question is not as demanded as <a href="http://www.javamakeuse.com/2015/05/java-program-to-balanced-parentheses.html" target="_blank">how to balanced the parentheses</a> using Java program.<br />
<br />
<pre class="brush:java">package com.javamakeuse.poc;
import java.util.Scanner;
/**
*
* @author javamakeuse
*
*/
public class Number {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter first number ");
int a = scanner.nextInt();
System.out.println("Enter second number");
int b = scanner.nextInt();
int c = a + b;
System.out.printf("sum of %d and %d is %d", a, b, c);
}
}
}
</pre>
<!-- adsense --><br />
<b>OUTPUT</b>:<br />
Enter first number <br />
4<br />
Enter second number<br />
4<br />
sum of 4 and 4 is 8</div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-79937322105041336942016-12-30T06:22:00.003-08:002016-12-30T06:22:41.782-08:00Java program to find area of circle<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>
While learning Java, we started with converting basic calculation to using Java program. like creating calculator using Java code, adding two numbers and all. And sometimes this type of questions also asked in <a href="http://www.javamakeuse.com/2015/09/core-java-written-test-questions-and-answer.html" target="_blank">core Java written interview questions</a>, although this question is not as demanded as <a href="http://www.javamakeuse.com/2015/05/java-program-to-balanced-parentheses.html" target="_blank">how to balanced the parentheses</a> using Java program.<br />
<br />
<b><span style="font-size: large;">Calculating Area of Circle</span></b><br />
Formula to calculate area of circle: area = PI * radius * radius<br />
<br />
<pre class="brush:java">package com.javamakeuse.poc;
/**
*
* @author javamakeuse
*
*/
public class Circle {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
// area = PI * radius * radius
public double printArea() {
return Math.PI * radius * radius;
}
public static void main(String[] args) {
Circle circle = new Circle();
circle.setRadius(12);
System.out.println("Area of circle is => " + circle.printArea());
}
}
</pre>
<!-- adsense --><br />
<b>OUTPUT</b>:<br />
Area of circle is => 452.3893421169302<br />
<br /></div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-54882066345668620932016-12-30T06:10:00.002-08:002016-12-30T06:10:49.650-08:00Java program to find sum of digits<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>
Very basic coding practice and core Java written interview questions, sometimes faculty gives this question as homework to their students to check the basic understanding Java syntax and basic calculation logic similar to <a href="http://www.javamakeuse.com/2015/10/java-program-to-find-factorial-of-number.html" target="_blank">finding a factorial of a given number</a> and <a href="http://www.javamakeuse.com/2016/12/best-fibonacci-program-using-java.html" target="_blank">printing Fibonacci series</a>.<br />
In this post, going to show very simple program to calculate the sum of given digits or say sum of given array elements.<br />
<br />
<!-- adsense --><br />
<pre class="brush:java">package com.javamakeuse.poc;
/**
*
* @author javamakeuse
*
*/
public class SumOfDigits {
// printing sum of digits from string
public static void printSumOfDigits(String digits) {
int sum = 0;
for (String digit : digits.split(",")) {
sum = sum + Integer.parseInt(digit);
}
System.out.println("Sum of digits (" + digits + ") is = " + sum);
}
// printing sum of array elements
public static void printSumOfArrayEle(int[] numArray) {
int sum = 0;
for (int num : numArray) {
sum = sum + num;
}
System.out.println("Sum of array element is = " + sum);
}
public static void main(String[] args) {
// calling method.
printSumOfDigits("1,2,3");
printSumOfArrayEle(new int[] { 1, 2, 3 });
}
}
</pre>
<br />
<b>OUTPUT</b>:<br />
Sum of digits (1,2,3) is = 6<br />
Sum of array element is = 6<br />
<br /></div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-23312730154783438612016-12-30T05:58:00.001-08:002016-12-30T05:58:10.031-08:00Java program to convert binary to decimal<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>
Java coding practice and sometimes asked in <a href="http://www.javamakeuse.com/2015/09/core-java-written-test-questions-and-answer.html" target="_blank">core Java written interview question</a>. For freshers it's important to do practice on such coding exercise like how to balanced parentheses and <a href="http://www.javamakeuse.com/2015/10/java-program-to-find-factorial-of-number.html" target="_blank">finding factorial of a given number</a>.<br />
In this article, i'm going to show two simple way to convert binary values into decimal numbers, in previous post, I had shared the just op positive how to convert decimal to binary <a href="http://www.javamakeuse.com/2016/12/java-program-to-convert-decimal-to.html" target="_blank">here</a>.<br />
<!-- adsense --><br />
<pre class="brush:java">package com.javamakeuse.poc;
/**
*
* @author javamakeuse
*
*/
public class BinaryToDecimal {
// converting binary to decimal via parseInt method
public static void binaryToDecimal(String binary) {
System.out.println(Integer.parseInt(binary, 2));
}
// converting binary to decimal via custom implementation
public static void binaryToDecimal2(String binary) {
double j = 0;
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '1') {
j = j + Math.pow(2, binary.length() - 1 - i);
}
}
System.out.println((int) j);
}
public static void main(String[] args) {
binaryToDecimal("10001100");
binaryToDecimal2("10001100");
}
}
</pre>
<b>OUTPUT</b>:<br />
140<br />
140<br />
<br /></div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-72050440304396963092016-12-30T05:41:00.002-08:002016-12-30T05:41:58.358-08:00Java program to convert decimal to binary<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s320/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" width="320" /></a></div>
Java programming interview question, specially for freshers, to check the candidates logical analysis and basic knowledge of coding. Some of the popular coding interview questions like <a href="http://www.javamakeuse.com/2015/05/java-program-to-balanced-parentheses.html" target="_blank">how to balanced parentheses</a> and <a href="http://www.javamakeuse.com/2015/07/java-program-find-second-largest-number-from-an-array.html" target="_blank">finding second largest numbers from an array</a>. In this post, I'm going to show you a simple Java code to convert decimal to binary.<br />
<br />
<b><span style="font-size: large;">Converting decimal to binary in Java</span></b><br />
<ul style="text-align: left;">
<li>Using built in function called as toBinaryString() and</li>
<li>Your own calculation to compute binary.</li>
</ul>
<!-- adsense --><br />
<pre class="brush:java">package com.javamakeuse.poc;
/**
*
* @author javamakeuse
*
*/
public class DecimalToBinary {
// converting decimal to binary using toBinaryString()
public static void convertDecimalToBinary(int num) {
System.out.println("Binary form of " + num + " is = " + Integer.toBinaryString(num));
}
// converting decimal to binary via calculating reminder values
public static void convertDecimalToBinary2(int num) {
if (num == 0) {
System.out.println(0);
} else {
String binaryString = "";
int n = num;
while (n > 0) {
int reminder = n % 2;
binaryString = reminder + binaryString;
n = n / 2;
}
System.out.println("Binary form of " + num + " is = " + binaryString);
}
}
public static void main(String[] args) {
// calling methods
convertDecimalToBinary(140);
convertDecimalToBinary2(140);
}
}
</pre>
<br />
<b>OUTPUT:</b><br />
Binary form of 140 is = 10001100<br />
Binary form of 140 is = 10001100<br />
<br /></div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-44821392273702586202016-12-29T09:51:00.000-08:002016-12-29T09:55:12.862-08:00Java program to print prime numbers<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPD68S9LWXDexYWpM53MeLgVc3f3HeBlDtHTxhTTeiKFkaZZVyABU1s95qD8140Lm5lZxgH234oOzjW5rV0l-nsbS6QLNHWCAIsdlFf5z4pscBvOKeFwS-RUFrP4ESLdN3ylwm4TwiDq4/s1600/interview.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPD68S9LWXDexYWpM53MeLgVc3f3HeBlDtHTxhTTeiKFkaZZVyABU1s95qD8140Lm5lZxgH234oOzjW5rV0l-nsbS6QLNHWCAIsdlFf5z4pscBvOKeFwS-RUFrP4ESLdN3ylwm4TwiDq4/s1600/interview.png" /></a></div>Java programming interview question, specially for freshers, to check the candidates logical analysis and basic knowledge of coding. Some of the popular coding interview questions like <a href="http://www.javamakeuse.com/2015/05/java-program-to-balanced-parentheses.html" target="_blank">how to balanced parentheses</a> and <a href="http://www.javamakeuse.com/2015/07/java-program-find-second-largest-number-from-an-array.html" target="_blank">finding second largest numbers from an array</a>. In this post, I'm going to show you a simple Java code to print prime numbers.<br />
<br />
<b><span style="font-size: large;">What is Prime number?</span></b><br />
A prime number is a natural number greater than 1, that has no any positive divisors other than 1 and itself. For example 2,3,5,7,11,13,17,19,23,29,31....<br />
<br />
<pre class="brush:java">package com.javamakeuse.poc;
public class PrimeNumbers {
// printing prime numbers till given number
public static void printPrimeNumbers(int number) {
int count;
for (int i = 1; i <= number; i++) {
count = 0;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
count++;
break;
}
}
if (count == 0) {
System.out.println(i + " is a Prime number");
}
}
}
public static void main(String[] args) {
printPrimeNumbers(50);
}
}
</pre><!-- adsense --><br />
<b>Output:</b><br />
1 is a Prime number<br />
2 is a Prime number<br />
3 is a Prime number<br />
5 is a Prime number<br />
7 is a Prime number<br />
11 is a Prime number<br />
13 is a Prime number<br />
17 is a Prime number<br />
19 is a Prime number<br />
23 is a Prime number<br />
29 is a Prime number<br />
31 is a Prime number<br />
37 is a Prime number<br />
41 is a Prime number<br />
43 is a Prime number<br />
47 is a Prime number<br />
<br />
</div>javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-24972754369582265082016-12-28T10:19:00.002-08:002016-12-28T10:19:54.919-08:00Best Fibonacci Program Using Java<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPD68S9LWXDexYWpM53MeLgVc3f3HeBlDtHTxhTTeiKFkaZZVyABU1s95qD8140Lm5lZxgH234oOzjW5rV0l-nsbS6QLNHWCAIsdlFf5z4pscBvOKeFwS-RUFrP4ESLdN3ylwm4TwiDq4/s1600/interview.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPD68S9LWXDexYWpM53MeLgVc3f3HeBlDtHTxhTTeiKFkaZZVyABU1s95qD8140Lm5lZxgH234oOzjW5rV0l-nsbS6QLNHWCAIsdlFf5z4pscBvOKeFwS-RUFrP4ESLdN3ylwm4TwiDq4/s1600/interview.png" /></a></div>
One of the popular Java coding interview question, this question is very common and almost all the companies ask this type of question specially in campus selection, this type of question is used to check the candidates logical analysis capacity and basic understanding of programming. In earlier post, I had shared a list of <a href="http://www.javamakeuse.com/2015/09/core-java-written-test-questions-and-answer.html" target="_blank">Java written interview question</a> and in this article going to show you different way to write Fibonacci program.<br />
<br />
<b><span style="font-size: large;">What is Fibonacci Series?</span></b><br />
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones."copied from the <a href="https://en.wikipedia.org/wiki/Fibonacci_number" target="_blank">wiki</a><br />
Example, 0,1,1,2,3,5,8,13,21,34,55,89...<br />
<br />
<pre class="brush:java">package com.javamakeuse.poc;
import java.math.BigInteger;
public class Fibonacci {
public static void main(String[] args) {
printFibonacci(10);
}
public static void printFibonacci(int n) {
BigInteger a = BigInteger.ZERO;
BigInteger b = BigInteger.ONE;
BigInteger c = a.add(b);
System.out.println(a);
System.out.println(b);
for (int i = 0; i < n; i++) {
c = a.add(b);
System.out.println(c);
a = b;
b = c;
}
}
}
</pre>
<!-- adsense --><br />
<b>Output:</b><br />
0<br />
1<br />
1<br />
2<br />
3<br />
5<br />
8<br />
13<br />
21<br />
34<br />
55<br />
89<br />
<br /></div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-8400818163678371532016-12-10T10:17:00.000-08:002016-12-10T10:17:04.497-08:00Hive Tutorial | Hive DDL Commands<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQZrfz0gxzaBd2deCfefCmxWN9S5Ev_gnRjDDXF_0mwAvAs6Ol47zn-Sq5NGYur8WduhkkGjYZ2AHKL4QrpGhJFC_T33x45S2eLesCZCc6u3I-46_QRqM47Gmp7DZs6-FZtCTnOXrTuU/s1600/hive_logo_medium.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQZrfz0gxzaBd2deCfefCmxWN9S5Ev_gnRjDDXF_0mwAvAs6Ol47zn-Sq5NGYur8WduhkkGjYZ2AHKL4QrpGhJFC_T33x45S2eLesCZCc6u3I-46_QRqM47Gmp7DZs6-FZtCTnOXrTuU/s1600/hive_logo_medium.jpg" /></a></div>
Apache Hive is data warehouse tool to process structure dataset and it's based on Hadoop infrastructure. The syntax is very much similar to MySql database, Hive mainly used for data presentation phase unlike Pig, <a href="http://www.javamakeuse.com/2016/02/difference-between-apache-pig-and-apache-hive.html" target="_blank">here</a> is very nice article explained on the differences between Apache Hive and Apache Pig. Hive was developed at Facebook, but now it's a part of Apache project and known as one of the popular Apache project among other <a href="http://www.javamakeuse.com/2016/01/top-20-big-data-technologies.html" target="_blank">big data technologies</a>. <br />
<br />
In this tutorial, We will see Hive DDL Commands with simple example,hive ddl is a part of Hive Query Language and the ddl commands use in hive are very much similar to other databases like mysql.<br />
If you are very new to hive, then you have to prepared your hive environment and here is a <a href="http://www.javamakeuse.com/2016/02/apache-hive-installation-in-ubuntu-hive.html" target="_blank">step by step guide to setup hive environment</a>.<br />
<br />
<u><b><span style="font-size: large;">HiveQL: Data Definition Language</span></b></u><br />
<ol style="text-align: left;">
<li>CREATE database database_name;<br />
<pre class="brush:java">create database ddl_commands;
</pre>
</li>
<li>CREATE schema schema_name;<br />
<pre class="brush:java">create schema ddl_commands;
</pre>
</li>
<li>CREATE table<br />
<pre class="brush:java">create table t(t_name string);
</pre>
</li>
<li>CREATE view<br />
<pre class="brush:java">create view t_view as select * from t;
</pre>
</li>
<li>CREATE function<br />
<pre class="brush:java">CREATE function my_fun AS MainClass USING JAR myjar;
</pre>
</li>
<li>CREATE index<br />
<pre class="brush:java">CREATE index index_t on table t2(t_name) AS
'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler'
WITH DEFERRED REBUILD;
</pre>
</li>
<li>DESCRIBE database<br />
<pre class="brush:java">DESCRIBE database ddl_commands;
</pre>
</li>
<li>DESCRIBE schema<br />
<pre class="brush:java">DESCRIBE schema ddl_commands;
</pre>
</li>
<li> DESCRIBE table<br />
<pre class="brush:java">DESCRIBE t1;
</pre>
</li>
<li>DESCRIBE view_name<br />
<pre class="brush:java">DESCRIBE t_view;
</pre>
</li>
<li>TRUNCATE table<br />
<pre class="brush:java">TRUNCATE t;
</pre>
</li>
<li>ALTER database<br />
<pre class="brush:java">ALTER database ddl_commands SET
DBPROPERTIES ('creator' = 'javamakeuse');
</pre>
</li>
<li>ALTER schema<br />
<pre class="brush:java">ALTER schema ddl_commands SET
DBPROPERTIES ('creator' = 'javamakeuse');
</pre>
</li>
<li>ALTER table<br />
<pre class="brush:java">ALTER table t SET TBLPROPERTIES ("comment"="table for learning");
</pre>
</li>
<li>ALTER view<br />
<pre class="brush:java">ALTER view t_view as select t_name from t;
</pre>
</li>
<li>SHOW databases<br />
<pre class="brush:java">SHOW databases;
</pre>
</li>
<li>SHOW schemas<br />
<pre class="brush:java">SHOW schemas;
</pre>
</li>
<li>SHOW tables<br />
<pre class="brush:java">SHOW tables;
</pre>
</li>
<li>SHOW TBLPROPERTIES<br />
<pre class="brush:java">SHOW TBLPROPERTIES t;
</pre>
</li>
<li>SHOW PARTITIONS<br />
<pre class="brush:java">SHOW PARTITIONS t;
</pre>
</li>
<li>SHOW functions<br />
<pre class="brush:java">SHOW functions;
</pre>
</li>
<li>SHOW indexs<br />
<pre class="brush:java">show index on t;
</pre>
</li>
<li>SHOW columns<br />
<pre class="brush:java">show columns from t;
</pre>
</li>
<li>SHOW create table<br />
<pre class="brush:java">show CREATE TABLE t;
</pre>
</li>
<li>MSCK repair table<br />
<pre class="brush:java">MSCK REPAIR TABLE t;
</pre>
</li>
</ol>
<!-- adsense --><br />
<u><b>DROP COMMANDS:</b></u><br />
<ol style="text-align: left;">
<li>DROP index<br />
<pre class="brush:java">drop index index_t on t;
</pre>
</li>
<li>DROP view<br />
<pre class="brush:java">drop view t_view;
</pre>
</li>
<li>DROP table<br />
<pre class="brush:java">drop table t;
</pre>
</li>
<li>DROP database<br />
<pre class="brush:java">drop database ddl_commands;
</pre>
</li>
<li>DROP schema<br />
<pre class="brush:java">drop schema ddl_commands;
</pre>
</li>
</ol>
<br />
That's it all about Hive DDL commands.</div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-85149535302150465542016-12-04T10:24:00.000-08:002016-12-04T10:24:22.640-08:00Hive Tutorial | Hive Data Types Example<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQZrfz0gxzaBd2deCfefCmxWN9S5Ev_gnRjDDXF_0mwAvAs6Ol47zn-Sq5NGYur8WduhkkGjYZ2AHKL4QrpGhJFC_T33x45S2eLesCZCc6u3I-46_QRqM47Gmp7DZs6-FZtCTnOXrTuU/s1600/hive_logo_medium.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQZrfz0gxzaBd2deCfefCmxWN9S5Ev_gnRjDDXF_0mwAvAs6Ol47zn-Sq5NGYur8WduhkkGjYZ2AHKL4QrpGhJFC_T33x45S2eLesCZCc6u3I-46_QRqM47Gmp7DZs6-FZtCTnOXrTuU/s1600/hive_logo_medium.jpg" /></a></div>
Apache Hive is a very demanding data warehouse in the market, and it's based on <a href="http://www.javamakeuse.com/2016/08/hadoop-tutorial-what-is-hadoop.html" target="_blank">Hadoop</a> infrastructure. Although all big data technologies are on demand but Hive is on highly demanded as compared to other big data technology. Apache Hive is data warehouse and sql like tool to process structure datasets. It was developed at Facebook and mainly used for data presentation phase. Hive syntax is very much similar to mysql database.<br />
In this tutorial, I am going to list the data types supported by Apache Hive, and those are listed below with very simple example. It categories Hive data types into five different sub categories and those are-<br />
<u><span style="font-size: large;"><b>Hive Data Types:</b></span></u><br />
<ul style="text-align: left;">
<li>Numeric Types</li>
<li>Date and Time Types</li>
<li>String Types</li>
<li>Misc Types</li>
<li>Complex Types</li>
</ul>
<span style="font-size: large;"><b><u>Numeric Types:</u></b></span><br />
<ul style="text-align: left;">
<li>TINYINT : 1 byte signed integer.</li>
<li>SMALLINT : 2 byte signed integer.</li>
<li>INT : 4 byte signed integer.</li>
<li>BIGINT : 8 byte signed integer.</li>
<li>FLOAT : 4 byte single precision floating point number.</li>
<li>DOUBLE :8 byte double precision floating point number.</li>
<li>DECIMAL : user definable precision and scale.</li>
</ul>
<br />
<span style="font-size: large;"><u><b>Date and Time:</b></u></span><br />
<ul style="text-align: left;">
<li>DATE </li>
<li>TIMESTAMP</li>
</ul>
<!-- adsense --><br />
<span style="font-size: large;"><u><b>String Types:</b></u></span><br />
<ul style="text-align: left;">
<li>CHAR (create table t (alphabets char(2));)</li>
<li>VARCHAR</li>
<li>STRING</li>
</ul>
<br />
<span style="font-size: large;"><u><b>Misc Types:</b></u></span><br />
<ul style="text-align: left;">
<li>BOOLEAN</li>
<li>BINARY</li>
</ul>
<br />
And below are the data types which makes Hive totally different from other traditional databases, known as <a href="http://www.javamakeuse.com/2016/11/hive-tutorial-hive-collection-data.html" target="_blank">complex data types</a> in hive.<br />
<span style="font-size: large;"><b><u>Complex Types:</u></b></span><br />
<ul style="text-align: left;">
<li><a href="http://www.javamakeuse.com/2016/11/hive-tutorial-array-in-hive-example.html" target="_blank">array</a> : Indexed based collection of similar type.</li>
<li><a href="http://www.javamakeuse.com/2016/11/hive-tutorial-struct-data-type-in-hive.html" target="_blank">maps</a> : Object(object contains different types of fields)</li>
<li><a href="http://www.javamakeuse.com/2016/11/hive-tutorial-hive-collection-data.html" target="_blank">structs</a> : Collection of Key-Value pair.</li>
<li>union : UNIONTYPE is a collection of heterogeneous data types. </li>
</ul>
<br />
<u><b><span style="font-size: large;">Example:</span></b></u><br />
<pre class="brush:xml">create table hive_data_type_info
(id int,name string,
types array<string>,
detail_info map<string,string>,
committers struct<count:int,dt:date,name:string>,
other_info uniontype<float,boolean,string>)
row format delimited
fields terminated by '\001'
collection items terminated by '\002'
map keys terminated by ':'
stored as textfile;
</pre>
<br />
That's it, to start with word count in hive you may <a href="http://www.javamakeuse.com/2016/10/word-count-program-using-hive-query.html">like</a> this and for the installation, <a href="http://www.javamakeuse.com/2016/02/apache-hive-installation-in-ubuntu-hive.html">hive installation guide</a>.</div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-3555293925694210162016-11-19T00:10:00.000-08:002016-12-06T09:05:13.528-08:00Hive Tutorial | Hive Collection data types example<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQZrfz0gxzaBd2deCfefCmxWN9S5Ev_gnRjDDXF_0mwAvAs6Ol47zn-Sq5NGYur8WduhkkGjYZ2AHKL4QrpGhJFC_T33x45S2eLesCZCc6u3I-46_QRqM47Gmp7DZs6-FZtCTnOXrTuU/s1600/hive_logo_medium.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQZrfz0gxzaBd2deCfefCmxWN9S5Ev_gnRjDDXF_0mwAvAs6Ol47zn-Sq5NGYur8WduhkkGjYZ2AHKL4QrpGhJFC_T33x45S2eLesCZCc6u3I-46_QRqM47Gmp7DZs6-FZtCTnOXrTuU/s1600/hive_logo_medium.jpg" /></a></div>
Nowadays <a href="http://www.javamakeuse.com/2015/12/what-is-big-data.html" target="_blank">Big data</a> is trending in the market, and that's why to analyse those dataset too many technologies are in the market every year or you can say on every six months a new big data technology is introduced and California city is the hub of start-up company where most of the technologies are getting matured. Apache Hive is one of the popular big data technology used in the market. <br />
<br />
Anyway in this tutorial post, I am going to list you the complex data types supported in Apache Hive, with the example of Map Collection data type. If you are new to Apache Hive, than you need to set-up your Hive environment first before starting and <a href="http://www.javamakeuse.com/2016/02/apache-hive-installation-in-ubuntu-hive.html" target="_blank">here</a> is a step by step guide to set-up your Hive environment.<br />
<br />
<u><b><span style="font-size: x-large;">What is Apache Hive?</span></b></u><br />
Apache Hive is a data warehouse tool to process and analyse structure dataset, with schema on read feature based on Hadoop infrastructure. Hive was developed at Facebook and mainly used for data presentation phase. You can start with <a href="http://www.javamakeuse.com/search/label/Apache%20Hive" target="_blank">word count tutorial in hive</a>.<br />
<br />
<u><b><span style="font-size: x-large;">Collection types in Hive</span></b></u><br />
Hive support four different collection types and those are listed below-<br />
<ul style="text-align: left;">
<li><a href="http://www.javamakeuse.com/2016/11/hive-tutorial-array-in-hive-example.html" target="_blank">Array</a>: Indexed based collection of similar type.</li>
<li><a href="http://www.javamakeuse.com/2016/11/hive-tutorial-struct-data-type-in-hive.html" target="_blank">Struct</a>: Object(object contains different types of fields)</li>
<li>Map: Collection of Key-Value pair.</li>
<li>uniontype: is a collection of heterogeneous data types </li>
</ul>
We have already seen <a href="http://www.javamakeuse.com/2016/11/hive-tutorial-array-in-hive-example.html" target="_blank">Array</a> and <a href="http://www.javamakeuse.com/2016/11/hive-tutorial-struct-data-type-in-hive.html" target="_blank">Struct</a> data types in hive and now time see Map in hive here is a complete <a href="http://www.javamakeuse.com/2016/12/hive-tutorial-hive-data-types-example.html" target="_blank">hive data type tutorial</a>.<br />
<!-- adsense --><br />
<i><u><b><span style="font-size: large;">Map in Hive:</span></b></u></i> <br />
A collection of key value pair, when you need elements to be accessed by key, than use key-value pair type map. And values are accessed using key, for example mapColumn.[key]<br />
<br />
Let's try with sample of cricket players data creating table and loading sample data than trying to fetch some of the details using key of map.<br />
<b>Sample Data set:</b><br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQR9-vXg5hFd8kCGnBeXZ50CBk6etS0MD6COvGnseGqDNsfQ0enrT2nWFF6A9KMUTgIxIWeEbxx7GVU5E0XBReX5df4l79oC17qDniEgtNQyGpZaqtuUBdlrKT9t8CmpUKT2GtfNOyZW4/s1600/map_data.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQR9-vXg5hFd8kCGnBeXZ50CBk6etS0MD6COvGnseGqDNsfQ0enrT2nWFF6A9KMUTgIxIWeEbxx7GVU5E0XBReX5df4l79oC17qDniEgtNQyGpZaqtuUBdlrKT9t8CmpUKT2GtfNOyZW4/s1600/map_data.png" /></a></div>
<br />
<u><b><span style="font-size: large;">Create table to hold data in map.</span></b></u><br />
<pre class="brush:xml">create table cricket_players_info(id int,team string,country string,
player string,match_info map<string,string>)
row format delimited
fields terminated by '\001'
collection items terminated by '\002'
map keys terminated by ':'
stored as textfile;
</pre>
<u><b><span style="font-size: large;">Describe command to verify table creation:</span></b></u><br />
<pre class="brush:xml">hive> desc cricket_players_info;
OK
id int
team string
country string
player string
match_info map<string,string>
Time taken: 0.078 seconds, Fetched: 5 row(s)
</pre>
<br />
<u><b><span style="font-size: large;">Load data into cricket_players_info table:</span></b></u><br />
putting data on hdfs<br />
<pre class="brush:xml">hadoop fs -put cricket_players_data /
</pre>
loading dataset into table<br />
<pre class="brush:xml">hive> load data inpath '/cricket_players_data' into table cricket_players_info;
</pre>
<u><b><span style="font-size: large;">Select Query:</span></b></u><br />
<pre class="brush:xml">hive> select * from cricket_players_info;
</pre>
Output:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNSCscS_yw9xb1SqFhfhGghfQfHF6_MkdG6FeSaXHGZkdS_q3gMeiF_NPgfkT5XxArnYVLkwSV9yanhy22AgA9RHjM_WwpeW2TflSITfPz-6D14h7Yuys_3FdZgnM-9DQmVRINKunApI0/s1600/moutput.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNSCscS_yw9xb1SqFhfhGghfQfHF6_MkdG6FeSaXHGZkdS_q3gMeiF_NPgfkT5XxArnYVLkwSV9yanhy22AgA9RHjM_WwpeW2TflSITfPz-6D14h7Yuys_3FdZgnM-9DQmVRINKunApI0/s1600/moutput.png" /></a></div>
<br />
<u><b><span style="font-size: large;">Lets access some element from map:</span></b></u><br />
<pre class="brush:java">select player, country, match_info["test_match"] as total_test,
match_info["odi_match"] as total_odi from cricket_players_info;
</pre>
<b>Output:</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxZtYNBEG2aJZpuSGw1xuGi_aSV0ffHesqXJ2DHenZvIRh4vpVmZ3ToCG1_K3hIr3IOJR6ZohFU6_44hC9MneUpqmRHKIntFy7We5YmCt8SRh3jsWMwGULB8Z8z4e4am4kQ7U1KTwkQZQ/s1600/moutput2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxZtYNBEG2aJZpuSGw1xuGi_aSV0ffHesqXJ2DHenZvIRh4vpVmZ3ToCG1_K3hIr3IOJR6ZohFU6_44hC9MneUpqmRHKIntFy7We5YmCt8SRh3jsWMwGULB8Z8z4e4am4kQ7U1KTwkQZQ/s1600/moutput2.png" /></a></div>
<br />
That's it about Complex type in hive.<br />
<br /></div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-23177042379196639782016-11-16T10:01:00.000-08:002016-12-06T09:08:09.870-08:00Hive Tutorial | Struct data type in Hive Example <div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQZrfz0gxzaBd2deCfefCmxWN9S5Ev_gnRjDDXF_0mwAvAs6Ol47zn-Sq5NGYur8WduhkkGjYZ2AHKL4QrpGhJFC_T33x45S2eLesCZCc6u3I-46_QRqM47Gmp7DZs6-FZtCTnOXrTuU/s1600/hive_logo_medium.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQZrfz0gxzaBd2deCfefCmxWN9S5Ev_gnRjDDXF_0mwAvAs6Ol47zn-Sq5NGYur8WduhkkGjYZ2AHKL4QrpGhJFC_T33x45S2eLesCZCc6u3I-46_QRqM47Gmp7DZs6-FZtCTnOXrTuU/s1600/hive_logo_medium.jpg" /></a></div>
Apache Hive is a popular data warehouse, based on <a href="http://www.javamakeuse.com/2016/08/hadoop-tutorial-what-is-hadoop.html" target="_blank">Hadoop</a> infrastructure and very demanding for data analytic. Nowadays Hive is almost used in every data analytic job. It's very much similar to any sql-oriented rdbms syntax but the objective of Hive is totally different than, traditional RDBMS. Hive is very popular for batch processing.<br />
<br />
In this article, I am going to show you an example of one of the collection data type in hive known as struct, although we have already seen a complete hive data type tutorial <a href="http://www.javamakeuse.com/2016/12/hive-tutorial-hive-data-types-example.html" target="_blank">here</a>. Hive's collection data type support four different type and those are-<br />
<i><b>Collection data type in Hive:</b></i><br />
<ul style="text-align: left;">
<li><a href="http://www.javamakeuse.com/2016/11/hive-tutorial-array-in-hive-example.html" target="_blank">Array</a>: Indexed based collection of similar type.</li>
<li>Struct: Object(object contains different types of fields)</li>
<li><a href="http://www.javamakeuse.com/2016/11/hive-tutorial-struct-data-type-in-hive.html" target="_blank">Map</a>: Collection of Key-Value pair.</li>
<li>uniontype: is a collection of heterogeneous data types.
</li>
</ul>
In our previous post, we have already seen <a href="http://www.javamakeuse.com/2016/11/hive-tutorial-array-in-hive-example.html" target="_blank">Array Collection type in Hive</a>, now lets explore the Struct type in this article.<br />
<br />
<u><b><span style="font-size: large;">Struct data type in Hive:</span></b></u><br />
It's very much similar to Java object or exactly same as struct in C language. It contains different types of fields unlike array(array contains similar type) and fields can be accessed via .(dot) notation like product.id<br />
<br />
<b><u>Sample Cricket Player Dataset:</u></b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6pspjOgEMY4T4KX9PAto6KsJpem7DuXuRAkSWEQejAdMFGZ74RwB4yj2ER97HxEXJQ-ObzEgjY52zgxP1e17sgjd6F5aIERxqA57AVloaC79Q1brIMoQPkOqvj_LDqT9cBQzrmhji69Q/s1600/cricket_players.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6pspjOgEMY4T4KX9PAto6KsJpem7DuXuRAkSWEQejAdMFGZ74RwB4yj2ER97HxEXJQ-ObzEgjY52zgxP1e17sgjd6F5aIERxqA57AVloaC79Q1brIMoQPkOqvj_LDqT9cBQzrmhji69Q/s1600/cricket_players.png" /></a></div>
<br />
<b><span style="font-size: large;"><u>Lets create table to hold struct type:</u></span></b><br />
<pre class="brush:xml">create table cricket_players(id int,team string,country string,player string,
match_details struct<total_test:int,total_odi:int,debut_dt:string>)
row format delimited
fields terminated by '\001'
collection items terminated by '\002'
stored as textfile;
</pre>
<br />
<u><b><span style="font-size: large;">Describe command to verify table creation:</span></b></u><br />
<pre class="brush:java">hive> desc cricket_players;
OK
id int
team string
country string
player string
match_details struct<total_test:int,total_odi:int,debut_dt:string>
Time taken: 0.083 seconds, Fetched: 5 row(s)
</pre>
<!-- adsense --><br />
<b><u><span style="font-size: large;">Load data into cricket_players table:</span></u></b><br />
putting data on hdfs<br />
<pre class="brush:java">hadoop fs -put cricket_players /
</pre>
loading dataset<br />
<pre class="brush:java">hive> load data inpath '/cricket_players' into table cricket_players;
</pre>
<u><b>Select Query:</b></u><br />
<pre class="brush:java">hive> select * from cricket_players;
</pre>
<b>Output:</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipGfxOvNV7syf-slfdMyGpZ-O3Wg28aLu6GxWCyzbPEdMD0Ws05YTn8BFTiebMnBHYx0D-3cFLeDhrqPT6-STCQK3GzAovRklyvTbKABzCWMK5iR38eWqteCqy8qyyKjJsvu1vKTd3JOI/s1600/cp.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipGfxOvNV7syf-slfdMyGpZ-O3Wg28aLu6GxWCyzbPEdMD0Ws05YTn8BFTiebMnBHYx0D-3cFLeDhrqPT6-STCQK3GzAovRklyvTbKABzCWMK5iR38eWqteCqy8qyyKjJsvu1vKTd3JOI/s1600/cp.png" /></a></div>
<br />
<u><b><span style="font-size: large;">Let's access some elements from struct type:</span></b></u><br />
<pre class="brush:java">hive> select team,country,player,match_details.total_test as total_test,match_details.total_odi as total_odi from cricket_players;
</pre>
Output:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUCqAXahOHJdki1uK1MgDQs8jFhW4OgbuNCa4Q6hqNIzu3QNXgoMsE-kTVPU__ioGHG6t7IuQ4C6bWBAcblkJ3m5Hojg9no3HaHe9ubF9e2OrGS9P8DC5Z_OV04wYYAgkDiAIge2pwAUk/s1600/cpo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUCqAXahOHJdki1uK1MgDQs8jFhW4OgbuNCa4Q6hqNIzu3QNXgoMsE-kTVPU__ioGHG6t7IuQ4C6bWBAcblkJ3m5Hojg9no3HaHe9ubF9e2OrGS9P8DC5Z_OV04wYYAgkDiAIge2pwAUk/s1600/cpo.png" /></a></div>
<br />
That's it in a simpler way to use struct in hive.<br />
<br /></div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-20091575308807512302016-11-10T07:24:00.000-08:002016-12-06T09:10:35.042-08:00Hive Tutorial | Collection data type in Hive | Array Example in Hive<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQZrfz0gxzaBd2deCfefCmxWN9S5Ev_gnRjDDXF_0mwAvAs6Ol47zn-Sq5NGYur8WduhkkGjYZ2AHKL4QrpGhJFC_T33x45S2eLesCZCc6u3I-46_QRqM47Gmp7DZs6-FZtCTnOXrTuU/s1600/hive_logo_medium.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZQZrfz0gxzaBd2deCfefCmxWN9S5Ev_gnRjDDXF_0mwAvAs6Ol47zn-Sq5NGYur8WduhkkGjYZ2AHKL4QrpGhJFC_T33x45S2eLesCZCc6u3I-46_QRqM47Gmp7DZs6-FZtCTnOXrTuU/s1600/hive_logo_medium.jpg" /></a></div>
Apache Hive is a popular data warehouse, based on Hadoop infrastructure and one of the demanding technology among the <a href="http://www.javamakeuse.com/2016/01/top-20-big-data-technologies.html" target="_blank">big data technologies</a>. Nowadays almost all the analytic companies are using Hive to perform analytics on big data, because of the simplicity of hive and also hive is very much sql oriented. Hive enables easy way to summarize your <a href="http://www.javamakeuse.com/2015/12/what-is-big-data.html" target="_blank">big data</a>, perform ad-hoc queries, and analysis of big data sets. <br />
If you are new to Big data technology and want to build your carrier in big data technology, than you should must know <a href="http://www.javamakeuse.com/2016/08/hadoop-tutorial-what-is-hadoop.html" target="_blank">What is Hadoop</a> and Hive. Anyway, for installation of hive you may visit my previous <a href="http://www.javamakeuse.com/2016/02/apache-hive-installation-in-ubuntu-hive.html" target="_blank">step by step guide to install Apache hive</a>. In this, tutorial, I'm going to show you an example of one of the commonly used collection data type in hive, that is Array in Hive.<br />
<i><b>Collection data type:</b></i><br />
Four different Collection data type supported in hive-<br />
<ul style="text-align: left;">
<li><a href="http://www.javamakeuse.com/2016/11/hive-tutorial-array-in-hive-example.html" target="_blank">Array</a>: Indexed based collection of similar type.</li>
<li><a href="http://www.javamakeuse.com/2016/11/hive-tutorial-struct-data-type-in-hive.html" target="_blank">Struct</a>: Object(object contains different types of fields)</li>
<li>Map: Collection of Key-Value pair.</li>
<li>uniontype: is a collection of heterogeneous data types.
</li>
</ul>
<u><i><b><span style="font-size: large;">Array in Hive:</span></b></i></u><br />
Indexed based ordered collection, index start with zero, to access element of array you need to pass the index position of element to be access.<br />
Example,<br />
array("Apple","Banana","Mango","Papaya")<br />
Now 3rd element/Mango can be access by using,<br />
array[2]<br />
<!-- adsense --><br />
<u><b>Lets create table with array type:</b></u><br />
<pre class="brush:xml">create table cricket_team(id int,name string,country string,
players array<string>) row format delimited
fields terminated by '\001'
collection items terminated by '\002'
stored as textfile;
</pre>
<br />
<u><b>Describe command to verify table creation:</b></u><br />
<pre class="brush:xml">hive> describe cricket_team;
OK
id int
name string
country string
players array<string>
Time taken: 0.161 seconds, Fetched: 4 row(s)
</pre>
<br />
<u><b>Load data into the cricket_team:</b></u><br />
putting dataset on hdfs:<br />
<pre class="brush:java">hadoop fs -put cricket_data /
</pre>
loading dataset<br />
<pre class="brush:java">hive> load data inpath '/cricket_data' into table cricket_team;
</pre>
<u><b>Select Query:</b></u><br />
<pre class="brush:java">hive> select * from cricket_team;
</pre>
<b>Output:</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgavFAHx_0dZOwwJPbUyytF31zx5HnhfV04X_uCKiUapNMDwAe4XWcOAWOQozcwWSLxzD-tPSnGTisnFE5PIQEm4PMQGVKX_WpSOBU4anpBhzm-7iPjqJc20i6S2FhYSj3EImAsh5LEBIU/s1600/select_array.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgavFAHx_0dZOwwJPbUyytF31zx5HnhfV04X_uCKiUapNMDwAe4XWcOAWOQozcwWSLxzD-tPSnGTisnFE5PIQEm4PMQGVKX_WpSOBU4anpBhzm-7iPjqJc20i6S2FhYSj3EImAsh5LEBIU/s1600/select_array.png" /></a></div>
<br />
<u><b><span style="font-size: large;">Access element from players array type:</span></b></u><br />
<pre class="brush:java">hive> select country,players[0] from cricket_team;
</pre>
<br />
<b><span style="font-size: large;">OUTPUT:</span></b><br />
<div class="separator" style="clear: both; text-align: center;">
<b><span style="font-size: large;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxLDP6ogk4XL9SprDUPX9TS5L6x4FF7rO1eiIc3lGYw-UPmUFrt-PyG7geu2M2rPEkEwqAqFDkrMGVrd6w-UwpK77_5XG7ETWJ1dmC7QX5C2u_ORKX76fwnD1bYZMI9zZU278_nT6UCy8/s1600/select_array2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxLDP6ogk4XL9SprDUPX9TS5L6x4FF7rO1eiIc3lGYw-UPmUFrt-PyG7geu2M2rPEkEwqAqFDkrMGVrd6w-UwpK77_5XG7ETWJ1dmC7QX5C2u_ORKX76fwnD1bYZMI9zZU278_nT6UCy8/s1600/select_array2.png" /></a></span></b></div>
<br />
<br /></div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-81565584992687833092016-11-05T07:14:00.000-07:002016-11-05T07:14:09.440-07:00Scala Tutorial | Scala Variables Example<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhduUhDZyvJmPHZz4MXUB8WPm7q2Xq2PF5hn2oxE0T3LeVjZLjCr8tOL7ul-q4_kKE-BIDTtMU-GQxSvTixhlej_0Jnx1O67iPLHFBEwdhpt1RDLIw1rTiYZ8Klr3L0RjKjVJl62TOTxo4/s1600/scala.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhduUhDZyvJmPHZz4MXUB8WPm7q2Xq2PF5hn2oxE0T3LeVjZLjCr8tOL7ul-q4_kKE-BIDTtMU-GQxSvTixhlej_0Jnx1O67iPLHFBEwdhpt1RDLIw1rTiYZ8Klr3L0RjKjVJl62TOTxo4/s200/scala.png" width="135" /></a></div>
The Scala stands for "Scalable Language" is a pure object oriented with fully supported functional programming language. Scala is concise and very much comparable with Java programming language.<br />
The origin of Scala is mix of many languages like Java, C#, C++, Haskell etc.<br />
Scala runs on JVM(Java Virtual Machine), Scala source code compiled into byte code similar to Java, you can easily mix Java code inside Scala or you can use Java libraries inside you Scala project. Although Scala is very much similar to Java but still it has some differences which makes it more demanding in a very short period of time unlike other languages. Here is detailed differences between Scala and Java <a href="http://www.javamakeuse.com/2016/10/scala-tutorial-scala-vs-java.html" target="_blank">Scala Vs Java</a><br />
<br />
In this tutorial, I am going to show you What is Scala Variables and different types of Variables in Scala like var, val, lazy with example.<br />
In Scala variables comes in three different variety-<br />
<ul style="text-align: left;">
<li>var</li>
<li>val</li>
<li>lazy val</li>
</ul>
<span style="font-size: x-large;"><b><span style="font-size: large;">var</span></b></span><br />
A variable with <b>mutable state</b> or you can say mutable variable defines using <b>var</b> keywords, which can be modified.<br />
<b><span style="font-size: large;">Syntax:</span></b><br />
<pre class="brush:java">package com.javamakeuse.tutorial
object VariableExample {
def main(args: Array[String]): Unit = {
// defining state variable
var state = "Open"
// another way to defining variable
var anotherWay: String = "Closed"
// ;semicolon in scala is optional
println("Semicolon is optional " + state)
// printing variable values on console
println("With semicolon; " + anotherWay);
}
}
</pre>
<b>Output:</b><br />
Semicolon is optional Open<br />
With semicolon; Closed<br />
<br />
<b>Note:</b> Semicolon(;) is optional in Scala, for Scala set-up, visit <a href="http://www.javamakeuse.com/2016/07/scala-tutorial-scala-installation.html" target="_blank">step by step guide to set-up scala</a>.<br />
<!-- adsense --><br />
<b><span style="font-size: x-large;">val</span></b><br />
A variable with <a href="http://www.javamakeuse.com/2015/04/example-of-immutable-classes-in-java.html" target="_blank">immutable state</a>, that means you cannot modifies the value once defines with <b>val</b> keywords. val defines a fixed value, which cannot be modifies.<br />
<b><span style="font-size: large;">Syntax:</span></b><br />
<pre class="brush:java">package com.javamakeuse.tutorial
object VariableExample {
def main(args: Array[String]): Unit = {
// defining state variable
val pi = 3.1415
//reassignment to val will gives compile time error.
//pi =12;
// another way to defining variable
var anotherWay: Double = 3.14
// ;semicolon in scala is optional
println("Semicolon is optional " + pi)
// printing variable values on console
println("With semicolon; " + anotherWay);
}
}
</pre>
<b>Output:</b><br />
Semicolon is optional 3.1415<br />
With semicolon; 3.14<br />
<br />
<b>Note:</b> reassignment of val variable will gives you compile time error, for overview of Scala read <a href="http://www.javamakeuse.com/2016/07/scala-tutorial-scala-overview.html" target="_blank">what is Scala</a>.<br />
<br />
<b><span style="font-size: x-large;">lazy val</span></b><br />
A variable which values are calculated on demand, only when they accessed for the first time, it's similar to val with pre lazy keyword. For more on lazy in scala, here is a separate post on <a href="http://www.javamakeuse.com/2016/11/scala-tutorial-scala-lazy-evaluation-example.html" target="_blank">lazy evaluation in scala</a>.<br />
<span style="font-size: large;"><b>Syntax:</b></span><br />
<pre class="brush:java">package com.javamakeuse.tutorial
object VariableExample {
def main(args: Array[String]): Unit = {
// defining state variable
lazy val pi = 3.1415
//reassignment to val will gives compile time error.
//pi =12;
// ;semicolon in scala is optional
println("Lazily evaluated " + pi)
}
}
</pre>
<b>Output:</b><br />
Lazily evaluated 3.1415<br />
<br />
Note: lazy keyword cannot be used with var keyword. For hello world in scala visit <a href="http://www.javamakeuse.com/2016/11/scala-tutorial-hello-world-in-scala.html" target="_blank">Scala tutorial</a><br />
<br />
<h4>
Scope of Variables</h4>
Three different scope exist for variables-<br />
<ul style="text-align: left;">
<li>Field variable</li>
<li>Local variable</li>
<li>Method parameter</li>
</ul>
<b>Field variables:</b> It can be defines by any of the above variables, and they belong to an Object and can be accessed by any method or anywhere depending on the accessibility used.<br />
<br />
<b> Local variable:</b> It can be defines inside method only and it can be accessed only inside the method it defined. It can be any of the above variable like var, val, lazy val<br />
<br />
<b>Method parameter: </b>variables which values are passed as parameter of the method while calling, and it can be only accessible from inside the method, and only <b>val</b> can be defines as a method parameter.<br />
<br />
<b>Note: </b>There are no static variable in Scala unlike Java, because in Scala everything is Object.</div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-4936415342509059072016-11-05T06:19:00.000-07:002016-11-07T09:25:47.673-08:00Scala Tutorial | Scala Lazy Evaluation Example<div dir="ltr" style="text-align: left;" trbidi="on"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhduUhDZyvJmPHZz4MXUB8WPm7q2Xq2PF5hn2oxE0T3LeVjZLjCr8tOL7ul-q4_kKE-BIDTtMU-GQxSvTixhlej_0Jnx1O67iPLHFBEwdhpt1RDLIw1rTiYZ8Klr3L0RjKjVJl62TOTxo4/s1600/scala.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhduUhDZyvJmPHZz4MXUB8WPm7q2Xq2PF5hn2oxE0T3LeVjZLjCr8tOL7ul-q4_kKE-BIDTtMU-GQxSvTixhlej_0Jnx1O67iPLHFBEwdhpt1RDLIw1rTiYZ8Klr3L0RjKjVJl62TOTxo4/s200/scala.png" width="135" /></a></div>The Scala stands for "Scalable Language" is a pure object oriented with fully supported functional programming language. Scala is concise and very much comparable with Java programming language.<br />
The origin of Scala is mix of many languages like Java, C#, C++, Haskell etc.<br />
Scala runs on JVM(Java Virtual Machine), Scala source code compiled into byte code similar to Java, you can easily mix Java code inside Scala or you can use Java libraries inside your Scala project. Although Scala is very much similar to Java but still it has some differences which makes it more demanding in a very short period of time unlike other languages. Here is detailed differences between Scala and Java <a href="http://www.javamakeuse.com/2016/10/scala-tutorial-scala-vs-java.html" target="_blank">Scala Vs Java</a><br />
<!-- adsense --><br />
We have already seen how many types of variables are available in Scala and when to use them with step by step guide at <a href="http://www.javamakeuse.com/2016/11/scala-tutorial-scala-variables-example.html" target="_blank">here</a>. And, In this article, I am going to show you a simple example of lazy evaluation in Scala.<br />
<b><u><span style="font-size: large;">Lazy evaluation Scala</span></u></b><br />
lazy val are calculated on demand when they accessed for the first time.<br />
Example,<br />
<pre class="brush:java">scala> lazy val num=10
num: Int = <lazy>
</pre>Here it's printing lazy value instead of num value 10, because of lazy val, lazy val calculated only when they accessed, but if you remove the lazy keyword, than, it will print the num value as 10 without even printing them, below is example of non-lazy.<br />
<pre class="brush:java">scala> val mynum=10
mynum: Int = 10
</pre></div>javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-77920909830783208682016-11-02T07:52:00.000-07:002016-11-02T07:52:34.585-07:00Scala Tutorial | Hello World in Scala using Eclipse<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhduUhDZyvJmPHZz4MXUB8WPm7q2Xq2PF5hn2oxE0T3LeVjZLjCr8tOL7ul-q4_kKE-BIDTtMU-GQxSvTixhlej_0Jnx1O67iPLHFBEwdhpt1RDLIw1rTiYZ8Klr3L0RjKjVJl62TOTxo4/s1600/scala.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhduUhDZyvJmPHZz4MXUB8WPm7q2Xq2PF5hn2oxE0T3LeVjZLjCr8tOL7ul-q4_kKE-BIDTtMU-GQxSvTixhlej_0Jnx1O67iPLHFBEwdhpt1RDLIw1rTiYZ8Klr3L0RjKjVJl62TOTxo4/s200/scala.png" width="135" /></a></div>
The Scala stands for "Scalable language", Scala is pure object oriented programming language as well as fully supported functional programming. Scala is concise and very much compatible with Java programming language. The syntax of Scala is very much similar to Java, one can easily mix both Scala and Java code in one place. It's a high level language with strongly statically typed, the execution model of Scala is derived from Java.<br />
For more on Scala overview you may read <a href="http://www.javamakeuse.com/2016/07/scala-tutorial-scala-overview.html" target="_blank">this</a> and for Scala installation you may read <a href="http://www.javamakeuse.com/2016/07/scala-tutorial-scala-installation.html" target="_blank">Scala installation guide</a>. Although Scala is very much similar to Java, but still it has various differences that's why nowadays Scala is on demanding, for detailed differences between Scala and Java read <a href="http://www.javamakeuse.com/2016/10/scala-tutorial-scala-vs-java.html" target="_blank">Scala vs Java</a> separate post.<!-- adsense --><br />
In this tutorial, I am going to show you an example of Hello World program in Scala using eclipse IDE.<br />
<span style="font-size: large;"><b>Step 1: Open IDE,Go to File->New->Scala Project</b></span><br />
It will ask for project name, provide the project name and click on Finish button.<br />
<br />
<b><span style="font-size: large;">Step 2: Right click on newly created project go to New->Scala Object</span></b><br />
It will ask for name, provide the name, below is the Create New File wizard.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglQJtn-qfJ-t-D3zIt8u3d4EuexSglSqtA_YPOEjhKDuvBQ6J-hNhD7o6JNa4dx_vDdc5cjVTI0nDUzKprMGKbgrPdGe9SqEPYesHIt_0mpkuqQjYX5dhf2vAvYCY3UwCGumzfEjTsHoc/s1600/hello-world.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglQJtn-qfJ-t-D3zIt8u3d4EuexSglSqtA_YPOEjhKDuvBQ6J-hNhD7o6JNa4dx_vDdc5cjVTI0nDUzKprMGKbgrPdGe9SqEPYesHIt_0mpkuqQjYX5dhf2vAvYCY3UwCGumzfEjTsHoc/s1600/hello-world.png" /></a></div>
Once done, it will open an editor to write your code.<br />
<br />
<b><span style="font-size: large;">Step 3: Hello world code.</span></b><br />
<pre class="brush:java">package com.javamakeuse.tutorial
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
</pre>
<br />
<b><span style="font-size: large;">Step 4: Run it.</span></b><br />
Output : Hello, world!<br />
<br />
That's it.</div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-83313741891150595562016-11-01T10:46:00.000-07:002016-11-01T10:46:35.483-07:00Java instanceof Operator Example<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3vniPQiW2doKkcEfjxsBiP65c-CPiWstxGk55zNDYn64axaxfi8Bm0AD8tqSvoiSTUzgINaSbKHBltnz97vH_kJfENVg8JyscsUfRSbnG-Y7bOySXVioeKR6GdizKqOWRp1qawDqeMRY/s1600/Java%252BMake%252BUse%252Bwith%252Burl%252Blogo.png" /></a></div>
The instanceof operator also known as type comparison operator, and it's very useful to compare the type of object. It compares an object to a specified type. One can use instanceof operator to test if an object is an instance of class,sub-class or interface, just like checking of <a href="http://www.javamakeuse.com/2014/11/what-is-serialization-in-java-with.html" target="_blank">serializable</a> interface after implementation of serializable.<br />
In this post, I am going to show you few examples of how to use instanceof operator in your code.<br />
<b>Example 1:</b><br />
We know that, String class by default implement the Serializable interface,lets check this.<br />
<pre class="brush:java">package com.javamakeuse.poc.bd;
import java.io.Serializable;
public class InstanceOfOp {
public static void main(String[] args) {
String str = "implemented serializable";
if (str instanceof Serializable) {
System.out.println("String by default implement Serializable");
}
}
}
</pre>
<b>Output:</b><br />
String by default implement Serializable<br />
<!-- adsense --><br />
<b>Example 2:</b><br />
<pre class="brush:java">package com.javamakeuse.poc.bd;
public class InstanceOfOp {
public static void main(String[] args) {
Parent parent = new Parent();
Parent child = new Child();
System.out.println("parent is parent => " + (parent instanceof Parent));
System.out.println("child is child => " + (child instanceof Parent));
}
}
class Parent {
}
class Child extends Parent {
}
</pre>
<b>Output:</b><br />
parent is parent => true<br />
child is child => true<br />
<br />
<b>Example 3:</b><br />
null is not an instance of anything, so comparison with null, it should return false.<br />
<pre class="brush:java">package com.javamakeuse.poc.bd;
public class InstanceOfOp {
public static void main(String[] args) {
String str = null;
System.out.println(str instanceof String);
}
}
</pre>
<b>Output:</b><br />
false</div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0tag:blogger.com,1999:blog-7626667474315891503.post-89131489886978514962016-10-20T11:24:00.000-07:002016-10-20T11:24:07.156-07:00Word count program using hive query<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhx-bZ7nT9F4rz7tI5R7dbA2PqgnEyyBvdc9EF3Uk0mtESMOUdEqqHa_vdFV_vIr77yMZWBcy0ypz001fXsCRRAxsSGDOIZhd4sHK5g1LGB43p8NZeA89G6fFb9xvDI4LZZ2VaGir7CL8s/s1600/hive_logo_medium.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhx-bZ7nT9F4rz7tI5R7dbA2PqgnEyyBvdc9EF3Uk0mtESMOUdEqqHa_vdFV_vIr77yMZWBcy0ypz001fXsCRRAxsSGDOIZhd4sHK5g1LGB43p8NZeA89G6fFb9xvDI4LZZ2VaGir7CL8s/s1600/hive_logo_medium.jpg" /></a></div>
We have already done the setup of hive in our local machine with the help of my previous article, <a href="http://www.javamakeuse.com/2016/02/apache-hive-installation-in-ubuntu-hive.html">hive installation</a> and now time to start with the hello world implementation of <a href="http://www.javamakeuse.com/2016/08/hadoop-tutorial-what-is-hadoop.html">hadoop</a> in hive, which is also known as word count in hive :)<br />
In this post, I am going to show you an example of word count program using hive, although we have already done the same using map reduce program here at <a href="http://www.javamakeuse.com/2016/01/hadoop-mapreduce-wordcount-tutorial.html" target="_blank">word count in map reduce tutorial</a>.<br />
<br />
<u><i>Word count program using hive</i></u><br />
<br />
<b><span style="font-size: large;">Input sample data:</span></b><br />
This is my first hive tutorial, which is known as hello world program in big data , big data technologies are now on demand.<br />
<br />
<b><span style="font-size: large;">Expected Output:</span></b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTD3nEOsNn5HbgGGRJO41yQaIz5mwVEJa9VEb0VLnJSTxivzzdap35stRazw1muamQ-Y9tWS7qrh_5TntJTjB_EG_QQThyphenhyphensjoOG7R379Kz4dciPq3uGYXHK3m5sS-fkVedmmhnd9kWV5k/s1600/hive_wc.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTD3nEOsNn5HbgGGRJO41yQaIz5mwVEJa9VEb0VLnJSTxivzzdap35stRazw1muamQ-Y9tWS7qrh_5TntJTjB_EG_QQThyphenhyphensjoOG7R379Kz4dciPq3uGYXHK3m5sS-fkVedmmhnd9kWV5k/s1600/hive_wc.png" /></a></div>
<br />
To achieve the above one, I am going to give you a very short and simple query unlike many lines of code while doing with map reduce :)<br />
<br />
Instead of directly executing the complete query, I'm breaking them in a step by step, so that it will help easy to read as well as easy understand, of course at the end you will find a complete query to full fill your needs.<br />
<br />
<b><span style="font-size: large;">Step 1. Create a table in hive</span></b><br />
Create a table in hive to insert the above input sample data from the file to hive table.<br />
Syntax:<br />
<pre class="brush:xml">hive> create table feedback(comments string);
</pre>
<!-- adsense --><br />
<b><span style="font-size: large;">Step 2. Load data from the sample file</span></b><br />
Syntax:<br />
<pre class="brush:xml">hive> load data local inpath '/home/subodh/hadoop_data/comments.txt' into table feedback;
</pre>
Ok, data is inserted into the hive table, now time to analyse how we can count..<br />
<br />
<b><span style="font-size: large;">Step 3. Convert comments into an array</span></b><br />
Now time to convert comments of feedback table into an array of string.<br />
Syntax:<br />
<pre class="brush:xml">hive> select split(comments,' ') from feedback;
</pre>
The above split udf will return the below output<br />
["This","is","my","first","hive","tutorial,","which","is","known","as","hello","world","program","in","big","data",",","big","data","technologies","are","now","on","demand."]<br />
<br />
<b><span style="font-size: large;">Step 4. Use table generation udf</span></b><br />
Now time to return multiple row from the above array of string, and for that we have built in table generation UDTF explode function.<br />
Syntax:<br />
<pre class="brush:xml">hive> select explode( split(comments,' ')) from feedback;
</pre>
The output of the above explode with split function is<br />
<pre class="brush:xml">This
is
my
first
hive
tutorial,
which
is
known
as
hello
world
program
in
big
data
,
big
data
technologies
are
now
on
demand.
</pre>
<br />
<b><span style="font-size: large;">Step 5. Final step</span></b><br />
Ok,Ok too late, let's put it all together and see the result<br />
Syntax:<br />
<pre class="brush:xml">hive> select word,count(*) from (select explode( split(comments,' ')) as word from feedback)tmp group by word;
</pre>
Or alternative one<br />
<pre class="brush:xml">hive> SELECT word, COUNT(*) FROM feedback LATERAL VIEW explode(split(comments, ' ')) tmp as word GROUP BY word;
</pre>
And below is the final output.<br />
<pre class="brush:xml">, 1
This 1
are 1
as 1
big 2
data 2
demand. 1
first 1
hello 1
hive 1
in 1
is 2
known 1
my 1
now 1
on 1
program 1
technologies 1
tutorial, 1
which 1
world 1
</pre>
<br />
That's it. Don't forget to provide your valuable feedback.</div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com1tag:blogger.com,1999:blog-7626667474315891503.post-9092868071178883072016-10-18T09:05:00.000-07:002016-10-18T09:05:45.251-07:00Scala Tutorial | Scala Vs Java<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhduUhDZyvJmPHZz4MXUB8WPm7q2Xq2PF5hn2oxE0T3LeVjZLjCr8tOL7ul-q4_kKE-BIDTtMU-GQxSvTixhlej_0Jnx1O67iPLHFBEwdhpt1RDLIw1rTiYZ8Klr3L0RjKjVJl62TOTxo4/s1600/scala.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhduUhDZyvJmPHZz4MXUB8WPm7q2Xq2PF5hn2oxE0T3LeVjZLjCr8tOL7ul-q4_kKE-BIDTtMU-GQxSvTixhlej_0Jnx1O67iPLHFBEwdhpt1RDLIw1rTiYZ8Klr3L0RjKjVJl62TOTxo4/s200/scala.png" width="135" /></a></div>
Scala is getting much and more popular, and become one of the favourite alternative of Java that's the reason why Scala grown exponentially. Scala syntax is very much similar to Java, and the best is that Scala runs on JVM(Java Virtual Machine), that means Scala is fully compatible with Java syntax, one can easily mix both code Scala and Java on same class.We have already seen an overview of Scala in my <a href="http://www.javamakeuse.com/2016/07/scala-tutorial-scala-overview.html" target="_blank">previous article</a>.<br />
After compiling the Scala code, it will generate the <b>.class</b> file similar to Java, which makes it write once run anywhere compatible, even you will get all the objects/method of <b>java.lang </b>package by default into Scala code. Although Scala is very much similar to Java, but still has some major differences, which caused the invention of Scala, and those are listed below.<br />
<b><span style="font-size: x-large;">Differences between Scala and Java</span></b><br />
<i><u>1. Scala is pure object oriented with fully support of functional programming</u></i><br />
<br />
The first and major difference between Scala and Java is that, Scala is pure object oriented, that means everything in Scala is an Object including numbers or functions. Whereas Java is not pure Object oriented programming language because of primitive types like int, bool, double etc. Where you can not call functions on these, but in Scala nothing like primitive everything is object, so you can easily call any method on Int, Double etc.<br />
And also Scala is fully supported of functional programming, but Java is not.<br />
<br />
<u><i>2. Less amount of code in Scala as compared to Java</i></u><br />
<br />
The simplicity of Java is much better as compared to Scala, but while coding Scala will reduce the number of lines to write by fully utilizing the type inference, as compared to Java.<br />
Quick example, to add fruits names into a list.<br />
<b>In Java:</b><br />
<pre class="brush:java">List<String> list = new ArrayList<String>();
list.add("Apple");
list.add("Mango");
list.add("Banana");
list.add("Papaya");
</pre>
<b>In Scala:</b><br />
<pre class="brush:java">val list = List("Apple", "Mango", "Banana","Papaya")
</pre>
Just a single line , really awesome isn't it?<br />
<br />
<u><i>3. Scala support operator overloading, Java does not</i></u><br />
<!-- adsense --><br />
Scala supports operator overloading whereas Java doesn’t.<br />
<br />
<u><i>4. Scala needs learning curve, as compared to Java.</i></u><br />
<br />
Scala is not that much simple as Java, one of the big reason behind the popularity of Java is simplicity. Java is more simple, but Scala is not, Scala needs thorough learning curve, because Scala is more complex as compared to Java, Scala has too many nested support. Although syntax of Scala is very much similar to Java, but the representation of Scala code is not that much simple.<br />
<b>Note</b>: of course this point is vary depending upon the user to user :) but it's from my personal experience.<br />
<br />
<i><u>5. Scala has built in lazy evaluation, but Java does not</u></i><br />
<br />
Scala supports lazy evaluation with the help of lazy keyword, you can evaluate values lazily. This will very much helpful while dealing with values that are not required during execution and also the computation cost is very high, for that it's much much better to get the computation on demand, for more on <a href="http://www.javamakeuse.com/2016/10/scala-tutorial-scala-lazy-evaluation.html" target="_blank">lazy evaluation in Scala</a>.<br />
<br />
Below is the syntax of lazy evaluation:<br />
<br />
<b>Without lazy evaluation:</b><br />
scala> val x = 10<br />
x: Int = 10<br />
<br />
<b>With lazy evaluation:</b><br />
scala> lazy val y = 15<br />
y: Int = <lazy><br />
<br />
here (lazy val) is not executed,unless you accessed it for the first time. <a href="http://www.javamakeuse.com/2016/07/scala-tutorial-scala-installation.html" target="_blank">Here</a> is a tutorial to set-up Scala in your lappy to start with.<br />
<br />
That's it, if you know any other differences, please do let me know in comments, so that I can add into the list and it'll really help someone too.</div>
javamakeusehttp://www.blogger.com/profile/08911269821730937343noreply@blogger.com0