forked from apache/hadoop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetDataItems_SS.java
More file actions
430 lines (404 loc) · 13.4 KB
/
Copy pathgetDataItems_SS.java
File metadata and controls
430 lines (404 loc) · 13.4 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package edu.okstate.cs.EHL.EnhancedMetaDataGenerator;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
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 getDataItems_SS
{
/*
* XmlJsonInputFormat is custom input format class to pass data to the mapper.
*/
public static class XmlJsonInputFormat extends TextInputFormat
{
public RecordReader<LongWritable, Text> createRecordReader(
InputSplit split, TaskAttemptContext context) {
return new XmlJsonRecordReader();
}
/**
* XmlJsonRecordReader class to read through a given xml document to output
* xml blocks as records
*
*/
//custom
public static class XmlJsonRecordReader extends
RecordReader<LongWritable, Text> {
private byte[] startTag;
private byte[] endTag;
private long start;
private long end;
private FSDataInputStream fsin;
private DataOutputBuffer buffer = new DataOutputBuffer();
private LongWritable key = new LongWritable();
private Text value = new Text();
InputStream is;
SAXBuilder sax;
Document doc;
private static StringBuilder b=new StringBuilder();
@Override
public void initialize(InputSplit split, TaskAttemptContext context)
throws IOException, InterruptedException {
Configuration conf = context.getConfiguration();
FileSplit fileSplit = (FileSplit) split;
// open the file and seek to the start of the split
start = fileSplit.getStart();
end = start + fileSplit.getLength();
Path file = fileSplit.getPath();
FileSystem fs = file.getFileSystem(conf);
fsin = fs.open(fileSplit.getPath());
fsin.seek(start);
}
@Override
public boolean nextKeyValue() throws IOException,
InterruptedException {
if (fsin.getPos() < end)
{
System.out.println("POS:"+fsin.getPos());
int i = 0;
while (true)
{
int b = fsin.read();
// end of file:
if (b == -1)
break;
// save to buffer:
buffer.write(b);
}
key.set(fsin.getPos());
value.set(buffer.getData(), 0,
buffer.getLength());
return true;
}
return false;
}
@Override
public LongWritable getCurrentKey() throws IOException,
InterruptedException {
return key;
}
@Override
public Text getCurrentValue() throws IOException,
InterruptedException {
return value;
}
@Override
public void close() throws IOException {
fsin.close();
}
@Override
public float getProgress() throws IOException {
return (fsin.getPos() - start) / (float) (end - start);
}
}
}
/*
* Map class parses the xml/json data and decides the data type of each data item i.e., key in case of json and tag in case of xml
*/
public static class Map extends Mapper<LongWritable,Text,Text, Text>
{
public static StringBuilder b=new StringBuilder();
public ArrayList<String> list=new ArrayList<String>();
public String arrayKey;
@Override
protected void map(LongWritable key, Text value,Mapper.Context context) throws IOException, InterruptedException
{
String document = value.toString();
String file=fileType(document);
/*
* if file is of xml type, then it extracts the key and content
*/
if(file.equalsIgnoreCase("xml"))
{
try
{
InputStream is=new ByteArrayInputStream(document.getBytes("UTF-8"));
SAXBuilder sax=new SAXBuilder();
Document doc=sax.build(is);
Element classEl=doc.getRootElement();
b.append(classEl.getName()+"\tvoid;");
List<Element> stList=classEl.getChildren();
printNode(stList, classEl);
StringTokenizer st1=new StringTokenizer(b.toString(),";");
while(st1.hasMoreTokens())
{
String[] tokens=st1.nextToken().split("\t");
if(tokens.length>=2)
{
if(tokens[1].equalsIgnoreCase("void"))
tokens[1]="NULL";
System.out.println("key:"+tokens[0]+" value:"+tokens[1]);
String dataType=getDatatype(tokens[1]);
context.write(new Text(tokens[0]),new Text(dataType));
}
}
}
catch(Exception e){
throw new IOException(e);
}
}
else if(file.equalsIgnoreCase("json"))
{
String jsonData=value.toString();
if(jsonData.startsWith("["))
parseJsonArray( jsonData);
else if(jsonData.startsWith("{"))
parseJsonObject(jsonData);
Iterator<String> iterator = list.iterator();
System.out.println("in list");
System.out.println("size:"+list.size());
while(iterator.hasNext())
{
String values=(String)iterator.next();
String[] tokens=values.split("\t");
if(tokens.length>=2)
{
// System.out.println("key:"+tokens[0]+" value:"+tokens[1]);
String dataType=getDatatype(tokens[1]);
context.write(new Text(tokens[0]),new Text(dataType));
}
}
}
else
System.out.println("Not an XML/JSON File");
}
/*
* Gives the type of file(XML or JSON)
*/
private String fileType(String data)
{
String type="";
try
{
InputStream is=new ByteArrayInputStream(data.getBytes("UTF-8"));
SAXBuilder sax=new SAXBuilder();
Document doc=sax.build(is);
type="xml";
}
catch(JDOMException e)
{
System.out.println("Not an xml File");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
new JSONParser().parse(data);
type="json";
}
catch(Exception e)
{
System.out.println("not a json file");
}
return type;
}
private void printNode(List<Element> el,Element classEl)
{
if(el!=null && el.size()>0)
{
for(int i=0;i<el.size();i++)
{
Element node=el.get(i);
System.out.println(node.getName());
//System.out.println(node.getName()+":"+getValue(node, classEl));
List<Element> childList= node.getChildren();
if(childList.size()==0)
{
System.out.println("in printnode");
if(node.getValue().isEmpty())
{
System.out.println("in spl loop");
b.append(node.getName()+"\tvoid;");
}
else
b.append(node.getName()+"\t"+node.getValue()+";");
System.out.println(node.getName()+"\t"+node.getValue()+";");
}
else
b.append(node.getName()+"\tvoid;");
printNode(node.getChildren(),classEl);
}
}
}
private String getDatatype(String value)
{
if(value.matches("^[0-9]*[.]?[0-9]*[fF]?$"))
{
//System.out.println("in digits");
try{
if(Integer.parseInt(value)%1==0)
return "Integer";
}
catch(NumberFormatException e)
{
if(value.contains("f"))
return "Float";
else
return "Double";
}
}
if(value.equalsIgnoreCase("true")||value.equalsIgnoreCase("false"))
return "Boolean";
return "String";
}
public void parseJsonArray(String data)
{
try
{
JSONParser parser=new JSONParser();
JSONArray json=(JSONArray)parser.parse(data);
for(int i=0;i<json.size();i++)
{
//System.out.println("key:"+json.get(i).toString()+" value:"+json.get(i).toString());
//System.out.println("in array"+json.get(i).toString());
if(json.get(i).toString().startsWith("{"))
parseJsonObject(json.get(i).toString());
else if(json.get(i).toString().startsWith("["))
{
if(json.get(i).toString().contains(":")||json.get(i).toString().contains("{"))
parseJsonArray(json.get(i).toString());
}
else
{
//System.out.println("key:"+arrayKey+" value:"+json.get(i).toString());
list.add(arrayKey+"\t"+json.get(i).toString());
}
}
}
catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void parseJsonObject(String data)
{
try
{
JSONParser parser=new JSONParser();
JSONObject json=(JSONObject)parser.parse(data);
for(Object key:json.keySet())
{
if(json.get(key).toString().startsWith("["))
{
if(json.get(key).toString().contains(":")||json.get(key).toString().contains("{"))
{
//System.out.println("key:"+key.toString()+" value:VOID");
list.add(key.toString()+"\tVOID");
}
arrayKey=key.toString();
parseJsonArray( json.get(key).toString());
}
else if(json.get(key).toString().startsWith("{"))
{
//System.out.println("key:"+key.toString()+" value:VOID");
list.add(key.toString()+"\tVOID");
parseJsonObject(json.get(key).toString());
}
else
{
//System.out.println("key:"+key.toString()+" value:"+json.get(key).toString());
list.add(key.toString()+"\t"+json.get(key).toString());
}
}
//if()
}
catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* Gives max occurance of a datatype
*/
public static class Reduce extends Reducer<Text, Text, Text, Text> {
// private Text outputKey = new Text();
public void reduce(Text key, Iterable<Text> values,Context context)
throws IOException, InterruptedException
{
ArrayList list=new ArrayList();
for(Text value:values)
{
list.add(value.toString());
}
String finalDataType=maxOccDataType(list);
context.write(key, new Text(finalDataType));
}
public String maxOccDataType(ArrayList dataType)
{
HashMap<String,Integer> mapCount=new HashMap<String,Integer>();
int count=0;
String tempKey;
String popularDataType = null;
for(int i=0;i<dataType.size();i++)
{
tempKey=(String)dataType.get(i);
for(int j=0;j<dataType.size();j++)
{
if(tempKey.equalsIgnoreCase((String)dataType.get(j)))
count++;
}
if(!mapCount.containsKey(tempKey))
mapCount.put(tempKey,count);
count=0;
}
int maxValue=Collections.max(mapCount.values());
for(HashMap.Entry<String, Integer> entry:mapCount.entrySet())
{
//System.out.println(entry.getKey()+","+entry.getValue());
if(entry.getValue()==maxValue)
//System.out.println("DataType is :"+entry.getKey());
popularDataType=entry.getKey();
}
return popularDataType;
}
}
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration();
Job job = new Job(conf);
job.setJarByClass(getDataItems_SS.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(getDataItems_SS.Map.class);
job.setReducerClass(getDataItems_SS.Reduce.class);
job.setInputFormatClass(XmlJsonInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
}
}