@Test public void testSplitEmail() throws Exception { String[] directives = new String[] { "split-email email", }; List<Row> rows = Arrays.asList( new Row("email", "root@cask.co"), new Row("email", "joltie.xxx@gmail.com"), new Row("email", "joltie_xxx@hotmail.com"), new Row("email", "joltie.\"@.\"root.\"@\".@yahoo.com"), new Row("email", "Joltie, Root <joltie.root@hotmail.com>"), new Row("email", "Joltie,Root<joltie.root@hotmail.com>"), new Row("email", "Joltie,Root<joltie.root@hotmail.com") ); rows = TestingRig.execute(directives, rows); Assert.assertTrue(rows.size() == 7); Assert.assertEquals("root", rows.get(0).getValue("email_account")); Assert.assertEquals("cask.co", rows.get(0).getValue("email_domain")); Assert.assertEquals("joltie.xxx", rows.get(1).getValue("email_account")); Assert.assertEquals("gmail.com", rows.get(1).getValue("email_domain")); Assert.assertEquals("joltie_xxx", rows.get(2).getValue("email_account")); Assert.assertEquals("hotmail.com", rows.get(2).getValue("email_domain")); Assert.assertEquals("joltie.\"@.\"root.\"@\".", rows.get(3).getValue("email_account")); Assert.assertEquals("yahoo.com", rows.get(3).getValue("email_domain")); Assert.assertEquals("joltie.root", rows.get(4).getValue("email_account")); Assert.assertEquals("hotmail.com", rows.get(4).getValue("email_domain")); Assert.assertEquals("joltie.root", rows.get(5).getValue("email_account")); Assert.assertEquals("hotmail.com", rows.get(5).getValue("email_domain")); Assert.assertNull(rows.get(6).getValue("email_account")); Assert.assertNull(rows.get(6).getValue("email_domain")); }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { row.add(generatedAccountCol, null); row.add(generatedDomainCol, null); continue; } if (object instanceof String) { String emailAddress = (String) object; int nameIdx = emailAddress.lastIndexOf("<"); if (nameIdx == -1) { Pair<String, String> components = extractDomainAndAccount(emailAddress); row.add(generatedAccountCol, components.getFirst()); row.add(generatedDomainCol, components.getSecond()); } else { String name = emailAddress.substring(0, nameIdx); int endIdx = emailAddress.lastIndexOf(">"); if (endIdx == -1) { row.add(generatedAccountCol, null); row.add(generatedDomainCol, null); } else { emailAddress = emailAddress.substring(nameIdx + 1, endIdx); Pair<String, String> components = extractDomainAndAccount(emailAddress); row.add(generatedAccountCol, components.getFirst()); row.add(generatedDomainCol, components.getSecond()); } } } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. " + "It should be of type 'String'.", column, object.getClass().getSimpleName())); } } else { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", column)); } } return rows; }
SplitEmail implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { row.add(generatedAccountCol, null); row.add(generatedDomainCol, null); continue; } if (object instanceof String) { String emailAddress = (String) object; int nameIdx = emailAddress.lastIndexOf("<"); if (nameIdx == -1) { Pair<String, String> components = extractDomainAndAccount(emailAddress); row.add(generatedAccountCol, components.getFirst()); row.add(generatedDomainCol, components.getSecond()); } else { String name = emailAddress.substring(0, nameIdx); int endIdx = emailAddress.lastIndexOf(">"); if (endIdx == -1) { row.add(generatedAccountCol, null); row.add(generatedDomainCol, null); } else { emailAddress = emailAddress.substring(nameIdx + 1, endIdx); Pair<String, String> components = extractDomainAndAccount(emailAddress); row.add(generatedAccountCol, components.getFirst()); row.add(generatedDomainCol, components.getSecond()); } } } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. " + "It should be of type 'String'.", column, object.getClass().getSimpleName())); } } else { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", column)); } } return rows; } }
SplitEmail implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { row.add(generatedAccountCol, null); row.add(generatedDomainCol, null); continue; } if (object instanceof String) { String emailAddress = (String) object; int nameIdx = emailAddress.lastIndexOf("<"); if (nameIdx == -1) { Pair<String, String> components = extractDomainAndAccount(emailAddress); row.add(generatedAccountCol, components.getFirst()); row.add(generatedDomainCol, components.getSecond()); } else { String name = emailAddress.substring(0, nameIdx); int endIdx = emailAddress.lastIndexOf(">"); if (endIdx == -1) { row.add(generatedAccountCol, null); row.add(generatedDomainCol, null); } else { emailAddress = emailAddress.substring(nameIdx + 1, endIdx); Pair<String, String> components = extractDomainAndAccount(emailAddress); row.add(generatedAccountCol, components.getFirst()); row.add(generatedDomainCol, components.getSecond()); } } } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. " + "It should be of type 'String'.", column, object.getClass().getSimpleName())); } } else { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", column)); } } return rows; }  }
SplitEmail implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { row.add(generatedAccountCol, null); row.add(generatedDomainCol, null); continue; } if (object instanceof String) { String emailAddress = (String) object; int nameIdx = emailAddress.lastIndexOf("<"); if (nameIdx == -1) { Pair<String, String> components = extractDomainAndAccount(emailAddress); row.add(generatedAccountCol, components.getFirst()); row.add(generatedDomainCol, components.getSecond()); } else { String name = emailAddress.substring(0, nameIdx); int endIdx = emailAddress.lastIndexOf(">"); if (endIdx == -1) { row.add(generatedAccountCol, null); row.add(generatedDomainCol, null); } else { emailAddress = emailAddress.substring(nameIdx + 1, endIdx); Pair<String, String> components = extractDomainAndAccount(emailAddress); row.add(generatedAccountCol, components.getFirst()); row.add(generatedDomainCol, components.getSecond()); } } } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. " + "It should be of type 'String'.", column, object.getClass().getSimpleName())); } } else { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", column)); } } return rows; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); }
SplitEmail implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int idx = row.find(column); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { row.add(generatedAccountCol, null); row.add(generatedDomainCol, null); continue; } if (object instanceof String) { String emailAddress = (String) object; int nameIdx = emailAddress.lastIndexOf("<"); if (nameIdx == -1) { Pair<String, String> components = extractDomainAndAccount(emailAddress); row.add(generatedAccountCol, components.getFirst()); row.add(generatedDomainCol, components.getSecond()); } else { String name = emailAddress.substring(0, nameIdx); int endIdx = emailAddress.lastIndexOf(">"); if (endIdx == -1) { row.add(generatedAccountCol, null); row.add(generatedDomainCol, null); } else { emailAddress = emailAddress.substring(nameIdx + 1, endIdx); Pair<String, String> components = extractDomainAndAccount(emailAddress); row.add(generatedAccountCol, components.getFirst()); row.add(generatedDomainCol, components.getSecond()); } } } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. " + "It should be of type 'String'.", column, object.getClass().getSimpleName())); } } else { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", column)); } } return rows; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); static final String NAME; }