forked from apache/hadoop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetDataSetType.java
More file actions
184 lines (151 loc) · 4.88 KB
/
Copy pathgetDataSetType.java
File metadata and controls
184 lines (151 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package edu.okstate.cs.EHL.EnhancedMetaDataGenerator;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DataOutputBuffer;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class getDataSetType
{
static long numberOfRecords=0l;
static String st="";
static String filePath="";
static String s="";
public static class MetaDataMapper extends Mapper < LongWritable, Text, Text, Text>
{
int max=0;
//Text maxWord = new Text();
public void map( LongWritable key,Text values, Context context) throws IOException,InterruptedException
{
Path pt=new Path(filePath);
FileSystem fs = FileSystem.get(new Configuration());
BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
String line="";
line=br.readLine();
while (line != null)
{
//System.out.println(line);
line=br.readLine();
st=line+"\n";
st+=st;
s=s+line;
numberOfRecords++;
}
String splitting[]=values.toString().split("\\s+");
System.out.println("Length of split:"+splitting.length);
if(splitting.length>=2)
{
System.out.println("key:"+splitting[0].trim()+" value:"+splitting[1].trim());
context.write(new Text(splitting[0].trim()), new Text(splitting[1].trim()));
}
}
}
public static class MetaDataReducer extends Reducer < Text, Text, Text, Text>
{
int max=0;
//Text maxWord = new Text();
public void reduce( Text key,Iterable<Text> values, Context context) throws IOException,InterruptedException
{
for(Text value:values)
{
System.out.println("Occurances:"+value.toString());
if(numberOfRecords==Long.parseLong(value.toString()))
{
context.write(value,new Text("S"));
}
else
{
System.out.println("in else");
String type=fileType();
if(type==" ")
{
context.write(value,new Text("U"));
}
else
{
context.write(value,new Text("SS"));
}
}
}
}
}
static String fileType()
{
String type="";
//char ch='';
try
{
InputStream is=new ByteArrayInputStream(s.getBytes("UTF-8"));
SAXBuilder sax=new SAXBuilder();
Document doc=sax.build(is);
type="SS";
}
catch(JDOMException e)
{
type=" ";
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
new JSONParser().parse(s);
type="SS";
}
catch(Exception e)
{
type=" ";
}
return type;
}
public static void main(String[] args) throws Exception
{
Configuration conf=new Configuration();
Job job=Job.getInstance(conf); //change the filename
job.setJarByClass(getDataSetType.class);
job.setMapperClass(getDataSetType.MetaDataMapper.class);
//job.setCombinerClass(MetaDataGenerator.class);
//System.out.println("in main");
filePath=args[2];
job.setReducerClass(getDataSetType.MetaDataReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0: 1);
}
}