@Override public DbData readData(InputStream in) throws XMLStreamException { XMLStreamReader sr = _staxInFactory.createXMLStreamReader(in); DbData result = new DbData(); sr.nextTag(); expectTag(FIELD_TABLE, sr); try { while (sr.nextTag() == XMLStreamReader.START_ELEMENT) { result.addRow(readRow(sr)); } } catch (IllegalArgumentException iae) { throw new XMLStreamException("Data problem: " + iae.getMessage(), sr.getLocation()); } sr.close(); return result; }
@Override protected void map(LongWritable key, Text value, Mapper.Context context) throws IOException, InterruptedException { String document = value.toString(); System.out.println("'" + document + "'"); try { XMLStreamReader reader = XMLInputFactory.newInstance() .createXMLStreamReader(new ByteArrayInputStream(document.getBytes())); String propertyName = ""; String propertyValue = ""; String currentElement = ""; while (reader.hasNext()) { int code = reader.next(); switch (code) { case XMLStreamConstants.START_ELEMENT: // START_ELEMENT: currentElement = reader.getLocalName(); break; case XMLStreamConstants.CHARACTERS: // CHARACTERS: if (currentElement.equalsIgnoreCase("uid")) { propertyName += reader.getText().trim(); System.out.println(propertyName); } else if (currentElement.equalsIgnoreCase("location")) { propertyValue += reader.getText().trim(); System.out.println(propertyValue); } else if (currentElement.equalsIgnoreCase("age")) { propertyValue += ("," + reader.getText().trim()); System.out.println(propertyValue); } break; } } reader.close(); context.write(new Text(propertyName.trim()), new Text(propertyValue.trim())); } catch (Exception e) { throw new IOException(e); } }