Flume thrift source C++ Demo

#include "gen-cpp/flume_constants.h"
#include "gen-cpp/flume_types.h"
#include "gen-cpp/ThriftSourceProtocol.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/protocol/TCompactProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>
#include <vector>

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
#define LOOP 200000

int right_num = 0;
int error_num = 0;

class ThriftClient{
    public:
    /* Thrift protocol needings... */
    boost::shared_ptr<TTransport> socket;
    boost::shared_ptr<TTransport> transport;
    boost::shared_ptr<TProtocol> protocol;
    ThriftSourceProtocolClient* pClient;

    public:
    void sendEvent();
    ThriftClient();

};

ThriftClient::ThriftClient():
        socket(new TSocket("10.0.6.227",9999)),
        transport(new TFramedTransport(socket)),
        protocol(new TCompactProtocol(transport))
    {
        pClient = new ThriftSourceProtocolClient(protocol);
    }

//transport(new TBufferedTransport(socket)),
void ThriftClient::sendEvent()
{
    std::map<std::string, std::string>  headers;
    headers.insert(std::make_pair("head", "head"));
    std::string sBody = "TableName:TEST_TABLE ConfigID:5555 ResponseIP:77522222 ProtoType:67 StartTime:3333 Interval:24544 AccessTimes:45 DomainLen:12\n";
    if(!transport->isOpen())
    {
        transport->open();
    }
    ThriftFlumeEvent tfEvent;
    tfEvent.__set_headers(headers);
    tfEvent.__set_body(sBody);
    Status::type res;
    int i=0;
    std::vector<ThriftFlumeEvent> eventbatch;
    for(;i<LOOP;i++){
        //tfEvent.__set_body(sBody);
        eventbatch.clear();
        int j=1;
        for(;j<=50;j++)
            eventbatch.push_back(tfEvent);
        res =pClient->appendBatch(eventbatch);
        if(res == Status::OK){
            right_num++;
        }else{
            error_num++;
            printf("WARNING: send event via thrift failed, return code:%d\n",res);
        }
    }
}

int main(int argc, char * argv[]){
    //boost::shared_ptr<TTransport> socket(new TSocket("localhost", 5496));
    //boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    //boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    ThriftClient *client = new ThriftClient();
    client->sendEvent();
    printf("RIGHT: success num:%d\n",right_num);
    printf("ERROR: failed num:%d\n",error_num);
    client->transport->close();
}

使用flume 的thrift source,用c++编写客户端的代码,批量追加信息。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值