flume1.6+elasticsearch6.3.2

Pom

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.4.</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.4.</version>
</dependency>
<!-- <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId>
<version>4.1..Final</version> </dependency> -->
<!-- https://mvnrepository.com/artifact/org.apache.flume.flume-ng-sinks/flume-ng-elasticsearch-sink -->
<dependency>
<groupId>org.apache.flume.flume-ng-sinks</groupId>
<artifactId>flume-ng-elasticsearch-sink</artifactId>
<version>1.6.</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.</version>
</dependency> </dependencies>

ElasticSearchForLogSink.java

package com.jachs.sink.elasticsearch;

import org.apache.flume.Channel;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.EventDeliveryException;
import org.apache.flume.Transaction;
import org.apache.flume.conf.Configurable;
import org.apache.flume.sink.AbstractSink;
import org.apache.flume.sink.elasticsearch.ElasticSearchEventSerializer;
import org.apache.flume.sink.elasticsearch.client.RoundRobinList;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient; import com.google.gson.Gson; import static org.apache.flume.sink.elasticsearch.ElasticSearchSinkConstants.CLUSTER_NAME;
import static org.apache.flume.sink.elasticsearch.ElasticSearchSinkConstants.INDEX_NAME; import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; import static org.apache.flume.sink.elasticsearch.ElasticSearchSinkConstants.HOSTNAMES; public class ElasticSearchForLogSink extends AbstractSink implements Configurable {
private String hostNames;
private String indexName;
private String clusterName;
static TransportClient client;
static Map<String, String> dataMap = new HashMap<String, String>();; public void configure(Context context) {
hostNames = context.getString(HOSTNAMES);
indexName = context.getString(INDEX_NAME);
clusterName = context.getString(CLUSTER_NAME);
} @Override
public void start() {
Settings settings = Settings.builder().put("cluster.name", clusterName).build();
try {
client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(
InetAddress.getByName(hostNames.split(":")[]), Integer.parseInt(hostNames.split(":")[])));
} catch (UnknownHostException e) {
e.printStackTrace();
}
} @Override
public void stop() {
super.stop();
} public Status process() throws EventDeliveryException {
Status status = Status.BACKOFF;
Channel ch = getChannel();
Transaction txn = ch.getTransaction();
txn.begin();
try {
Event event = ch.take();
if (event == null) {
txn.rollback();
return status;
}
String data = new String(event.getBody(), "UTF-8");
if (data.indexOf("token") != -) {
String token = data.substring(data.length() - , data.length());
System.out.println("获取标识" + token);
String sb = dataMap.get(token);
if (sb != null) {
sb = sb + data;
} else {
dataMap.put(token, data);
}
}
System.out.println("打印" + dataMap.size());
if (dataMap.size() >= ) {//十条数据一提交,条件自己改
BulkRequestBuilder bulkRequest = client.prepareBulk(); bulkRequest.add(client.prepareIndex(indexName, "text").setSource(dataMap));
bulkRequest.execute().actionGet();
dataMap.clear();
System.out.println("归零" + dataMap.size());
}
// Map<String, Object> map = new HashMap<String, Object>(); // for (String key : head.keySet()) {
// map.put("topic", key);
// map.put("timestamp", head.get(key));
// map.put("data", new String(event.getBody(), "UTF-8"));
// } // IndexRequestBuilder create = client.prepareIndex(indexName,
// "text").setSource(map);
// IndexResponse response = create.execute().actionGet(); txn.commit();
status = Status.READY;
} catch (Throwable t) {
txn.rollback();
status = Status.BACKOFF;
t.printStackTrace();
if (t instanceof Error) {
throw (Error) t;
}
} finally {
txn.close();
}
return status;
}
}

kafka生成者模仿日志写入代码

package com.test.Kafka;

import java.util.Properties;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord; import com.google.gson.Gson; public class App {
public static void main(String[] args) {
Properties properties = new Properties();
// properties.put("bootstrap.servers",
// "192.168.2.200:9092,192.168.2.157:9092,192.168.2.233:9092,192.168.2.194:9092,192.168.2.122:9092");
// properties.put("bootstrap.servers",
// "192.168.2.200:9092,192.168.2.233:9092,192.168.2.122:9092");
properties.put("bootstrap.servers", "127.0.0.1:9092");
properties.put("acks", "all");
properties.put("retries", );
properties.put("batch.size", );
properties.put("linger.ms", );
properties.put("buffer.memory", );
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = null;
RandomStringUtils randomStringUtils=new RandomStringUtils();
try {
producer = new KafkaProducer<String, String>(properties);
for (int i = ; i < ; i++) {// topID无所谓
producer.send(new ProducerRecord<String, String>("test1", "tokenk"+randomStringUtils.random()));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
producer.close();
}
}
}

修改flume配置

a1.sinks.elasticsearch.type=com.jachs.sink.elasticsearch.ElasticSearchForLogSink

重写Sink合并多行的更多相关文章

  1. jquery动态合并表格行

    利用<td rowspan = "num"/>;原理来实现,其中num为要合并的行数. <!DOCTYPE html> <html> <h ...

  2. Js 合并 table 行 的实现方法

    Js 合并 table 行 的实现方法 需求如下: 某公司的员工档案,如下,  经理看员工的信息不是很清晰: 姓名 所在学校 毕业时间 张三 小学 2000 张三 中学 2006 张三 大学 2010 ...

  3. SQL中合并多行记录的方法总汇

    -- =============================================================================-- Title: 在SQL中分类合并数 ...

  4. C# 使用Epplus导出Excel [4]:合并指定行

    C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...

  5. 【HANA系列】SAP HANA SQL合并多行操作

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL合并多行 ...

  6. 合并表格行---三层for循环遍历数据

    合并表格行---三层for循环遍历数据 示例1 json <!DOCTYPE html> <html lang="zh_cn"> <head> ...

  7. 详细说明svn分支与合并---命令行

    一,svn分支与合并有什么用? 作程序的,对svn在熟悉不过了,但对svn分支熟悉的,我想并不多.因为一般情况下,是用不着svn分支的,其实也没有那个必要.下面我例举几个需要用到svn分支的情况: 1 ...

  8. html表格合并(行,一排)

    <table> <tr> <td colspan="2">失败的例子:</td> </tr> {% for ip , j ...

  9. SQL SERVER 字符合并多行为一列

    [字符合并多行为一列] 思路1:行转列,在与字符拼接(适用每组列数名相同) 思路2:转xml,去掉多余字符(适用所有) 假设兴趣表Hobbys Name Hobby 小张 打篮球 小张 踢足球 Nam ...

随机推荐

  1. Spring Boot 2.0 的快速入门(图文教程)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! Spring Boot 2.0 的快速入门(图文教程) 大家都 ...

  2. C#通过序列化实现深表复制

    利用二进制序列化的方式进行深拷贝  有一个缺陷 序列化的类型必须标识为刻序列化的[Serializable] 否则无法进行二进制序列化 class Program { static void Main ...

  3. CSS超出省略号样式

    overflow:hidden;text-overflow:ellipsis;-wekit-line-clamp:3;display:-webkit-box;-webkit-box-orient:ve ...

  4. Spring Aop底层原理详解

    Spring Aop底层原理详解(来源于csdn:https://blog.csdn.net/baomw)

  5. Vue.js实现注册功能

    编写html,通过vue-resource.js库向后台提交数据 <!DOCTYPE html> <html lang="en"> <head> ...

  6. mac 卸载通过官网下载包安装的node

    sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}

  7. jQuery中的动画方法

    对角线动画 hide():显示 可以写两个参数——第一个参数 数字类型:毫秒 字符串类型: slow:慢 600ms  normal:比slow快   比fast慢   400ms  fast:快   ...

  8. 【升鲜宝】生鲜配送管理系统_升鲜宝供应链系统V2.0 客户管理模块功能与设计,欢迎大家批评指点。

    [升鲜宝] 客户管理模块功能设计与介绍 客户模块分为以下子功能  客户列表 价格组 价格组商品价格 客户退货 客户星级 客户类型 客户存储位 客户来源 物流公司 打印模板 子模块介绍        客 ...

  9. Android studio怎么使用自定义的framework而避免冲突报错和点不进去报红。

    文件:xx\project_abc\video\build.gradle保证可以运行到自定义的framework而不报错,可能因为project和module名字相同所以导致下面的路径是绝对路径,其他 ...

  10. MyDAL - .Where() & .And() & .Or() 使用

    索引: 目录索引 一.API 列表 1.Where .Where(Func<M, bool> func) 如: .Where( it => (it.Prop1>=条件1 &am ...