import com.alibaba.e3.connector.IEromangaConnector;
import com.alibaba.e3.connector.IMessagesProcessor;
import com.alibaba.e3.connector.SingleNodeEromangaConnector;
import com.alibaba.e3.packets.E3;
import com.alibaba.erosa.protocol.protobuf.ErosaEntry;
import com.google.protobuf.ByteString;
import com.alibaba.erosa.protocol.protobuf.ErosaEntry.Entry;
import com.alibaba.erosa.protocol.protobuf.ErosaEntry.EntryType;
import com.alibaba.erosa.protocol.protobuf.ErosaEntry.EventType;
import com.sun.rowset.internal.Row;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import static com.alibaba.e3.packets.E3.Messages;
/**
* Created with IntelliJ IDEA.
* User: fyq
* Date: 12-6-29
* Time: 下午2:21
* To change this template use File | Settings | File Templates.
*/
public class EromangaClient {
String eventSource = "mysql_fyqtest";
String subscriber = "subscriber0000000006";
String userName = "ottermysql";
String password = "ottermysql";
int TransactionBeginCount = 0;
int TransactionEndCount = 0;
int RowDataCount = 0;
public void testSimpleConnector(){
InetSocketAddress address = new InetSocketAddress("127.0.0.1",11116);
IEromangaConnector connector = new SingleNodeEromangaConnector(address,eventSource,subscriber,userName,password);
try{
connector.connect();
IMessagesProcessor mp = new IMessagesProcessor(){
public void process(Messages messages) throws Exception{
List<ByteString> events;
events = messages.getMessagesList();
if(events == null || events.isEmpty()){
return;
}
for(ByteString byteString : events){
Entry entry = Entry.parseFrom(byteString);
EntryType entryType = entry.getEntryType();
if(entryType == EntryType.TRANSACTIONBEGIN){
++TransactionBeginCount;
ErosaEntry.TransactionBegin trbegin = ErosaEntry.TransactionBegin.parseFrom(entry.getStoreValue());
String strisNull = trbegin.getTransactionId() + "|" +trbegin.getPropsList().toString();
System.out.println("||TransactionBegin get:" + strisNull);
}else if(entryType == EntryType.TRANSACTIONEND){
++TransactionEndCount;
ErosaEntry.TransactionEnd trend = ErosaEntry.TransactionEnd.parseFrom(entry.getStoreValue());
String strisNull = trend.getTransactionId() + "|" + trend.getPropsList().toString();
System.out.println("||TransactionEnd get:" + strisNull);
}else if(entryType == EntryType.ROWDATA){
++RowDataCount;
ErosaEntry.RowData rowData = ErosaEntry.RowData.parseFrom(entry.getStoreValue());
String strisNull = rowData.getTransactionId();
System.out.println("||rowData TransactionId:" + strisNull);
EventType eventType = rowData.getEventType();
List<ErosaEntry.RowData.Column> columns = null;
StringBuilder columnString = new StringBuilder();
if(eventType == EventType.INSERT){
columns = rowData.getAfterColumnsList();
}else if(eventType == EventType.UPDATE){
columns = rowData.getAfterColumnsList();
}else if(eventType == EventType.DELETE){
columns = rowData.getBeforeColumnsList();
}else if(eventType == EventType.CREATE){
columnString.append("[" + rowData.getTableName()
+ ":" + rowData.getSql() + "]");
}else if (eventType == EventType.ALTER) {
columnString.append("[" + rowData.getTableName()
+ ":" + rowData.getSql() + "]");
} else if (eventType == EventType.ERASE) {
columnString.append("[" + rowData.getTableName()
+ ":" + rowData.getSql() + "]");
}
if(columns!=null){
for(ErosaEntry.RowData.Column column : columns){
columnString.append("[" + column.getName()
+ ":" + column.getValue() + "]");
}
}
String strInfo = "the tableName is " +rowData.getSchemaName() +"."+rowData.getTableName()
+",the column value is" + columnString.toString();
if(entryType == EntryType.ROWDATA){
System.out.println("||RowData:" + strInfo);
}
}
}
//connector.ack();
if(events.size() > 0){
System.out.println("TransactionBegin count is " + TransactionBeginCount);
System.out.println("RowData Count is " + RowDataCount);
System.out.println("TransactionEnd count is " + TransactionEndCount);
}
}
};
while(true){
connector.fetchAndProcess(1024,mp);
}
}catch(Exception e){
e.printStackTrace();
}finally{
connector.disconnect();
}
}
public static void main(String[] args){
EromangaClient eromangaClient = new EromangaClient();
eromangaClient.testSimpleConnector();
}
}
复制 搜索