Skip to content

Instantly share code, notes, and snippets.

@vajja
Last active August 1, 2018 15:34
Show Gist options
  • Select an option

  • Save vajja/798535f8142aeb524f40 to your computer and use it in GitHub Desktop.

Select an option

Save vajja/798535f8142aeb524f40 to your computer and use it in GitHub Desktop.
Json Parsing, ArrrayList, LinkedList,HashSet, HashMap
import java.util.*;
class AList{
public static void main(String args[]){
/* Array list extends Abstract List implements List interface.
* Array list supports dynamic Arrays that can grow as needed
* Array list can dynamically increase and decrease in size
* It is of variable length
*/
//Array list declaration
ArrayList<String> a1=new ArrayList<>();
//to check the initial size of arraylist
System.out.println("Initial size of a1 "+a1.size());
//adding elements to arraylist
a1.add("Happy");
a1.add("New");
a1.add("Year");
a1.add("2015");
//cheking the size of the array after addding elemnts
System.out.println("Size of after adding: "+a1.size());
//we are adding new element in front of the array
a1.add(0,"wish");
System.out.println("contents of a1: "+a1);
//Removing the element at index 4
a1.remove(4);
//printing the output after deleting the element
System.out.println("contents of a1: "+a1);
ArrayList<Integer> a2= new ArrayList<>();
a2.add(1);
a2.add(2);
a2.add(3);
a2.add(4);
a2.add(5);
System.out.println("a2 contains: "+a2);
//creating a new array to add add all the integers in a2
Integer ap[]= new Integer[a2.size()];
ap=a2.toArray(ap);
int sum=0;
//we are using for each loop to add all the integers in array ap.
for(int i : ap) sum+=i;
System.out.println("sum:"+sum);
a2.add(2,5);
}
}
import java.io.IOException;
import java.util.*;
import org.apache.avro.*;
import org.apache.avro.Schema.Type;
import org.apache.avro.mapred.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
/**
* The classic WordCount example modified to output Avro Pair<CharSequence,
* Integer> records instead of text.
*/
public class AvroWordCount extends Configured implements Tool {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
}
public static class Reduce extends MapReduceBase
implements Reducer<Text, IntWritable,
AvroWrapper<Pair<CharSequence, Integer>>, NullWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<AvroWrapper<Pair<CharSequence, Integer>>, NullWritable> output,
Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(new AvroWrapper<Pair<CharSequence, Integer>>(
new Pair<CharSequence, Integer>(key.toString(), sum)),
NullWritable.get());
}
}
public int run(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: AvroWordCount <input path> <output path>");
return -1;
}
JobConf conf = new JobConf(AvroWordCount.class);
conf.setJobName("wordcount");
// We call setOutputSchema first so we can override the configuration
// parameters it sets
AvroJob.setOutputSchema(conf, Pair.getPairSchema(Schema.create(Type.STRING),
Schema.create(Type.INT)));
conf.setMapperClass(Map.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setMapOutputKeyClass(Text.class);
conf.setMapOutputValueClass(IntWritable.class);
conf.setOutputKeyComparatorClass(Text.Comparator.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
return 0;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new AvroWordCount(), args);
System.exit(res);
}
}
import java.util.Map.Entry;
import java.util.*;
import javax.crypto.Mac;
class HashMapDemo{
public static void main(String args[]){
//string represents key and integer is the value in the hash map
HashMap<String, Integer> hm= new HashMap<String,Integer>();
//In tree Map the data is sorted
//TreeMap<String, Integer> hm= new TreeMap<String,Integer>();
hm.clear();
//To insert the data in Hash Map
hm.put("Happy", 20);
hm.put("New", 9);
hm.put("Year", 18);
hm.put("2015", 27);
//To read the data at the output we read data using entrySet which transfers the data
//into set from which er are readig the data
Set<Entry<String,Integer>> set = hm.entrySet();
for(Entry<String,Integer> me:set){
System.out.println(me.getKey()+":"+me.getValue());
}
System.out.println();
int balance= hm.get("Happy");
hm.put("Happy",balance+10);
//using hm.get(key) we can get particular key value
System.out.println("New value of Happy: "+hm.get("Happy"));
}
}
import java.util.HashSet;
class HashSetDemo{
public static void main(String args[]){
//similar to array list but here the hashset output is shuffled unsorted
HashSet<String> a=new HashSet<>();
//TreeSet is similar to Hash set but gets the data in the sorted order
a.add("O");
a.add("N");
a.add("Y");
a.add("E");
a.add("H");
System.out.println(a);
}
}
package vajja;
import vajja.User;
// the old namespace mentioned in the original tutorial is outdated
//import org.codehaus.jackson.map.ObjectMapper;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import java.io.File;
import java.net.URL;
import java.io.IOException;
public class UserTest {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
//Here we are taking the input from the json file and extracting the data
File jsonFile = new File("user.json");
//This is the command to get data from the url
URL jsonUrl = new URL("https://gist.githubusercontent.com/vajja/bf13481f31209d0b9f9c/raw/aba5d0ec9345b886426ab767376b7c9cb60c251f/user.json");
//Here we are declaring complete data as string and then extracting into normal daat
String jsonStr =
"{\"name\":{\"first\":\"Joe\",\"last\":\"Sixpack\"},\"gender\":\"MALE\",\"verified\":false,\"userImage\":\"Rm9vYmFyIQ==\"}";
User user = null;
//convert java to json and viseversa
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//Here we are reading the json file to user
user = mapper.readValue(jsonFile, User.class);
//user.getName().getFirst() first it reads name and then goes to firstname and gives Joe
System.out.println(user.getName().getFirst());
//this command retrieves the gender from the prints in the output
System.out.println(user.getGender());
//Here the data is taken from the Url data
user = mapper.readValue(jsonUrl, User.class);
//We are retrieving the data similar to the json file
System.out.println(user.getName().getLast());
//Here we are getting the data from the String jsonString declared above and printing the output
user = mapper.readValue(jsonStr, User.class);
System.out.println(user.getGender());
}
}
import java.util.LinkedList;
class LinkedListDemo{
public static void main(String args[]){
//Almost same as Array list but has few extra functions like getFirst(),removeLast and etcs..
LinkedList<String> a= new LinkedList<>();
a.add("A");
a.add("A2");
a.add("E");
a.add("I");
a.add("O");
a.add("U");
a.add("J");
a.add("S");
a.add("Z");
System.out.println("Contents in Linked List: "+a);
a.remove("A2");
a.remove(6);
System.out.println("contents after Deletion: "+a);
System.out.println(a.getFirst());
System.out.println(a.peekLast());
a.removeLast();
a.pollLast();
String val=a.get(2);
a.set(2,val+" vowel" );
System.out.println("a after changed: "+a);
}
}
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class WordCount {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordcount");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
//import org.omg.CORBA.portable.InputStream;
import org.w3c.dom.Document;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class xmlparsing2 {
public static void main(String[] args) throws Exception {
//1.Create a new SAX parser factory with the SAXParserFactory class.
SAXParserFactory parserFactor = SAXParserFactory.newInstance();
//2.Configure the factory.
//3.Create a new SAX parser (SAXParser) object from the factory
SAXParser parser = parserFactor.newSAXParser();
//4.Set the event handlers for the SAX parser.
SAXHandler handler = new SAXHandler();
//URL jsonUrl = new URL("https://gist.githubusercontent.com/vajja/dddd83442abdc5e80240/raw/881a55f577ff4e295d625453263c36dfc27c6ace/user.xml");
URL xmlUrl = new URL("https://gist.githubusercontent.com/vajja/dddd83442abdc5e80240/raw/881a55f577ff4e295d625453263c36dfc27c6ace/user.xml");
InputStream in = xmlUrl.openStream();
//5.Parse the input XML documents
//both works
//parser.parse((new FileInputStream("/Users/rajagopalvajja/Downloads/user.xml")),handler);
parser.parse(in,handler);
//Printing the list of employees obtained from XML
for ( Employee emp : handler.empList){
System.out.println(emp);
}
}
}
/*
* The major event-handling methods are startDocument(), endDocument(), startElement(),
* and endElement() when it recognizes an XML tag. This interface also defines the methods
* characters() and processingInstruction(), which are invoked when the parser encounters
* the text in an XML element or an inline processing instruction
* */
class SAXHandler extends DefaultHandler {
List<Employee> empList = new ArrayList<>();
Employee emp = null;
String content = null;
@Override
//Triggered when the start of tag is found.
/**
* The following code shows the XMLDefaultHandler implementation.
* The startElement() and endElement() methods print the qualified name,
* local name, and namespace URI for each element
*/
public void startElement(String uri, String localName,String qName, Attributes attributes)
throws SAXException {
switch(qName){
//Create a new Employee object when the start tag is found
case "employee":
emp = new Employee();
emp.id = attributes.getValue("id");
break;
}
}
@Override
public void endElement(String uri, String localName,String qName) throws SAXException {
switch(qName){
//Add the employee to list once end tag is found
case "employee":
empList.add(emp);
break;
//For all other end tags the employee has to be updated.
case "firstName":
emp.firstName = content;
break;
case "lastName":
emp.lastName = content;
break;
case "location":
emp.location = content;
break;
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
content = String.copyValueOf(ch, start, length).trim();
}
}
class Employee1 {
String id;
String firstName;
String lastName;
String location;
@Override
public String toString() {
return firstName + " " + lastName + "(" + id + ")" + location;
}
}
import java.io.IOException;
import java.awt.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.lang.ClassLoader;
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.lang.model.element.Element;
import javax.xml.parsers.*;
import org.w3c.dom.Document; //imp
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
class Employee2{
String id;
String firstName;
String lastName;
String location;
public String toString(){
return firstName+" "+lastName+"("+id+")"+location;
}
}
public class xmlParsing3{
public static void main(String args[])throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//retriving data from online source
URL xmlUrl = new URL("https://gist.githubusercontent.com/vajja/dddd83442abdc5e80240/raw/881a55f577ff4e295d625453263c36dfc27c6ace/user.xml");
InputStream in = xmlUrl.openStream();
Document document = builder.parse(in);
ArrayList<Employee2> empList = new ArrayList<>();
NodeList nodeList = document.getDocumentElement().getChildNodes();
System.out.println("NodeList length is: "+nodeList.getLength());
//we are going through each node and extracting data
for(int i=0;i<nodeList.getLength();i++){
//First we are getting employee
Node node = nodeList.item(i);
if(node instanceof Element){
Employee2 emp = new Employee2();
emp.id=node.getAttributes().getNamedItem("id").getNodeValue();
NodeList childNodes = node.getChildNodes();
//identfying the child nodes of employee
for(int j=0;j<childNodes.getLength();j++){
Node cNode =childNodes.item(j);
if(cNode instanceof Element){
String content = cNode.getLastChild().getTextContent().trim();
switch(cNode.getNodeName()){
case "firstName":
emp.firstName = content;
case "lastName":
emp.lastName = content;
case "location":
emp.location = content;
}
}
}
empList.add(emp);
}
}
//Document document= builder.parse(new FileInputStream("https://gist.githubusercontent.com/vajja/dddd83442abdc5e80240/raw/881a55f577ff4e295d625453263c36dfc27c6ace/user.xml"));
for(Employee2 emp:empList){
System.out.println(emp);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment