in phoenix5-flume/src/it/java/org/apache/phoenix/flume/RegexEventSerializerIT.java [225:296]
public void testApacheLogRegex() throws Exception {
sinkContext = new Context ();
final String fullTableName = generateUniqueName();
final String logRegex = "([^ ]*) ([^ ]*) ([^ ]*) (-|\\[[^\\]]*\\]) \"([^ ]+) ([^ ]+)" +
" ([^\"]+)\" (-|[0-9]*) (-|[0-9]*)(?: ([^ \"]*|\"[^\"]*\")" +
" ([^ \"]*|\"[^\"]*\"))?";
final String columns = "host,identity,user,time,method,request,protocol,status,size,referer,agent";
String ddl = "CREATE TABLE " + fullTableName +
" (uid VARCHAR NOT NULL, user VARCHAR, time varchar, host varchar , identity varchar, method varchar, request varchar , protocol varchar," +
" status integer , size integer , referer varchar , agent varchar CONSTRAINT pk PRIMARY KEY (uid))\n";
sinkContext.put(FlumeConstants.CONFIG_TABLE, fullTableName);
sinkContext.put(FlumeConstants.CONFIG_JDBC_URL, getUrl());
sinkContext.put(FlumeConstants.CONFIG_SERIALIZER,EventSerializers.REGEX.name());
sinkContext.put(FlumeConstants.CONFIG_TABLE_DDL, ddl);
sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_REGULAR_EXPRESSION,logRegex);
sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_COLUMN_NAMES,columns);
sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_ROWKEY_TYPE_GENERATOR,DefaultKeyGenerator.UUID.name());
String message1 = "33.22.11.00 - user1 [12/Dec/2013:07:01:19 +0000] " +
"\"GET /wp-admin/css/install.css HTTP/1.0\" 200 813 " +
"\"http://www.google.com\" \"Mozilla/5.0 (comp" +
"atible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)\"";
String message2 = "192.168.20.1 - user2 [13/Dec/2013:06:05:19 +0000] " +
"\"GET /wp-admin/css/install.css HTTP/1.0\" 400 363 " +
"\"http://www.salesforce.com/in/?ir=1\" \"Mozilla/5.0 (comp" +
"atible;)\"";
sink = new PhoenixSink();
Configurables.configure(sink, sinkContext);
assertEquals(LifecycleState.IDLE, sink.getLifecycleState());
final Channel channel = this.initChannel();
sink.setChannel(channel);
sink.start();
final Event event1 = EventBuilder.withBody(Bytes.toBytes(message1));
final Event event2 = EventBuilder.withBody(Bytes.toBytes(message2));
final Transaction transaction = channel.getTransaction();
transaction.begin();
channel.put(event1);
channel.put(event2);
transaction.commit();
transaction.close();
sink.process();
final String query = " SELECT * FROM \n " + fullTableName;
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
final ResultSet rs ;
final Connection conn = DriverManager.getConnection(getUrl(), props);
try{
rs = conn.createStatement().executeQuery(query);
assertTrue(rs.next());
assertTrue(rs.next());
}finally {
if(conn != null) {
conn.close();
}
}
sink.stop();
assertEquals(LifecycleState.STOP, sink.getLifecycleState());
}