RegexMatchMapOperator extends BaseOperator { public void setRegex(String regex) { this.regex = regex; pattern = Pattern.compile(this.regex); }  String getRegex(); void setRegex(String regex); @Override void setup(OperatorContext context); void processTuple(String line); final transient DefaultInputPort<String> data; final transient DefaultOutputPort<Map<String, Object>> output; }
@Test public void testNoMatches() { String string = "abcd"; String regex = "^(?<myfield>\\d+)"; RegexMatchMapOperator oper = new RegexMatchMapOperator(); oper.setRegex(regex); CollectorTestSink<Object> sink = new CollectorTestSink<Object>(); oper.output.setSink(sink); oper.beginWindow(0); oper.data.process(string); oper.endWindow(); log.debug(String.format("Line is : %s", string)); log.debug(String.format("Regex is : %s", regex)); Assert.assertEquals("number emitted tuples", 0, sink.collectedTuples.size()); }
@Test public void testMatching() { ArrayList<HashMap<String, String>> test_cases = new ArrayList<HashMap<String, String>>(); HashMap<String, String> test_case = new HashMap<String, String>(); test_case.put("string", "12345 \"foo bar\" baz;goober"); test_case.put("regex", "^(?<id>\\d+) \"(?<username>[^\"]+)\" (?<action>[^;]+);(?<cookie>.+)"); test_case.put("fields", "id,username,action,cookie"); test_cases.add(test_case); test_case = new HashMap<String, String>(); test_case.put("string", "127.0.0.1 - - [04/Apr/2013:17:17:21 -0700] \"GET /favicon.ico HTTP/1.1\" 404 498 \"http: "\"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537" + ".31\""); test_case.put("regex", "^(?<ip>[\\d\\.]+) (\\S+) (\\S+) \\[(?<date>[\\w:/]+\\s[+\\-]\\d{4})\\] \"[A-Z]+ (?<url>.+?) HTTP/\\S+\" " + "(?<status>\\d{3}) (?<bytes>\\d+) \"(?<referer>[^\"]+)\" \"(?<agent>[^\"]+)\""); test_case.put("fields", "ip,date,url,status,bytes,referer,agent"); test_cases.add(test_case); for (HashMap<String, String> curr_case : test_cases) { String string = curr_case.get("string"); String regex = curr_case.get("regex"); String[] fields = curr_case.get("fields").split(","); RegexMatchMapOperator oper = new RegexMatchMapOperator(); oper.setRegex(regex); CollectorTestSink<Object> sink = new CollectorTestSink<Object>(); oper.output.setSink(sink); oper.beginWindow(0); oper.data.process(string); oper.endWindow(); Assert.assertEquals("number emitted tuples", 1, sink.collectedTuples.size()); @SuppressWarnings("unchecked") Map<String, Object> output = (Map<String, Object>)sink.collectedTuples.get(0); log.debug(String.format("Line is : %s", string)); log.debug(String.format("Regex is : %s", regex)); log.debug(String.format("Map is : %s", output.toString())); for (String field : fields) { Assert.assertTrue("Map is missing '" + field + "' key", output.containsKey(field)); } } }