/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weiboyi.etl.flume.source.canal;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.alibaba.otter.canal.protocol.Message;
import com.google.gson.Gson;
import org.apache.flume.Event;
import org.apache.flume.event.EventBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CanalEntryChannelEventConverter {

    private static final Logger LOGGER = LoggerFactory.getLogger(CanalEntryChannelEventConverter.class);

    public static List<Event> convert(CanalEntry.Entry entry) {

        List<Event> events = new ArrayList<Event>();

        if (entry.getEntryType() == CanalEntry.EntryType.ROWDATA) {
            CanalEntry.RowChange rowChage = null;
            try {
                rowChage = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
            } catch (Exception e) {
                LOGGER.error("parse event has an error , data:" + entry.toString(), e);
                throw new RuntimeException("parse event has an error , data:" + entry.toString(), e);
            }

            CanalEntry.EventType eventType = rowChage.getEventType();

            if (eventType == CanalEntry.EventType.QUERY || rowChage.getIsDdl()) {

            } else {
                Map<String, String> header = new HashMap<String, String>();
                header.put("table", entry.getHeader().getTableName());

                for (CanalEntry.RowData rowData : rowChage.getRowDatasList()) {
                    if (eventType != CanalEntry.EventType.DELETE) {

                        Map<String, Object> rowMap = new HashMap<String, Object>();

                        for(CanalEntry.Column column : rowData.getAfterColumnsList()) {
                            rowMap.put(column.getName(), column.getValue());
                        }

                        Gson gson = new Gson();
                        events.add(EventBuilder.withBody(gson.toJson(rowMap).getBytes(Charset.forName("UTF-8")), header));
                    }
                }
            }
        }

        return events;
    }

//    public void test () {
//        for (CanalEntry.Entry entry : entries) {
//            if (entry.getEntryType() == CanalEntry.EntryType.TRANSACTIONBEGIN) {
//                CanalEntry.RowChange rowChange= null;
//                try {
//                    CanalEntry.RowChange change = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
//                } catch (Exception e) {
//                    throw new RuntimeException("parse event has an error , data:" + entry.toString(), e);
//                }
//            }
//            if (entry.getEntryType() == CanalEntry.EntryType.ROWDATA) {
//                CanalEntry.RowChange rowChage = null;
//                try {
//                    rowChage = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
//                } catch (Exception e) {
//                    throw new RuntimeException("parse event has an error , data:" + entry.toString(), e);
//                }
//
//                CanalEntry.EventType eventType = rowChage.getEventType();
//
////                logger.info(row_format,
////                        new Object[] { entry.getHeader().getLogfileName(),
////                                String.valueOf(entry.getHeader().getLogfileOffset()), entry.getHeader().getSchemaName(),
////                                entry.getHeader().getTableName(), eventType,
////                                String.valueOf(entry.getHeader().getExecuteTime()), String.valueOf(delayTime) });
//
//
//
//                if (eventType == CanalEntry.EventType.QUERY || rowChage.getIsDdl()) {
//                    continue;
//                }
//
//                for (CanalEntry.RowData rowData : rowChage.getRowDatasList()) {
//                    if (eventType != CanalEntry.EventType.DELETE) {
//
//                        Map<String, String> rowDataMap = new HashMap<String, String>();
//                        Map<String, Object> rowMap = new HashMap<String, Object>();
//
//                        for(CanalEntry.Column column : rowData.getAfterColumnsList()) {
//                            rowDataMap.put(column.getName(), column.getValue());
//                        }
//
//                        Gson gson = new Gson();
//
//                        rowMap.put("data", rowMap);
//
//                        sender.send("local-test-canal", gson.toJson(rowMap));
//                    }
//                }
//            }
//        }
//    }
}

@@ -2986,2444 +2986,7 @@
    %7D
-%0A%0A//    public void test () %7B%0A//        for (CanalEntry.Entry entry : entries) %7B%0A//            if (entry.getEntryType() == CanalEntry.EntryType.TRANSACTIONBEGIN) %7B%0A//                CanalEntry.RowChange rowChange= null;%0A//                try %7B%0A//                    CanalEntry.RowChange change = CanalEntry.RowChange.parseFrom(entry.getStoreValue());%0A//                %7D catch (Exception e) %7B%0A//                    throw new RuntimeException(%22parse event has an error , data:%22 + entry.toString(), e);%0A//                %7D%0A//            %7D%0A//            if (entry.getEntryType() == CanalEntry.EntryType.ROWDATA) %7B%0A//                CanalEntry.RowChange rowChage = null;%0A//                try %7B%0A//                    rowChage = CanalEntry.RowChange.parseFrom(entry.getStoreValue());%0A//                %7D catch (Exception e) %7B%0A//                    throw new RuntimeException(%22parse event has an error , data:%22 + entry.toString(), e);%0A//                %7D%0A//%0A//                CanalEntry.EventType eventType = rowChage.getEventType();%0A//%0A////                logger.info(row_format,%0A////                        new Object%5B%5D %7B entry.getHeader().getLogfileName(),%0A////                                String.valueOf(entry.getHeader().getLogfileOffset()), entry.getHeader().getSchemaName(),%0A////                                entry.getHeader().getTableName(), eventType,%0A////                                String.valueOf(entry.getHeader().getExecuteTime()), String.valueOf(delayTime) %7D);%0A//%0A//%0A//%0A//                if (eventType == CanalEntry.EventType.QUERY %7C%7C rowChage.getIsDdl()) %7B%0A//                    continue;%0A//                %7D%0A//%0A//                for (CanalEntry.RowData rowData : rowChage.getRowDatasList()) %7B%0A//                    if (eventType != CanalEntry.EventType.DELETE) %7B%0A//%0A//                        Map%3CString, String%3E rowDataMap = new HashMap%3CString, String%3E();%0A//                        Map%3CString, Object%3E rowMap = new HashMap%3CString, Object%3E();%0A//%0A//                        for(CanalEntry.Column column : rowData.getAfterColumnsList()) %7B%0A//                            rowDataMap.put(column.getName(), column.getValue());%0A//                        %7D%0A//%0A//                        Gson gson = new Gson();%0A//%0A//                        rowMap.put(%22data%22, rowMap);%0A//%0A//                        sender.send(%22local-test-canal%22, gson.toJson(rowMap));%0A//                    %7D%0A//                %7D%0A//            %7D%0A//        %7D%0A//    %7D
 %0A%7D%0A

delete annotations