重写Sink合并多行
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合并多行的更多相关文章
- jquery动态合并表格行
利用<td rowspan = "num"/>;原理来实现,其中num为要合并的行数. <!DOCTYPE html> <html> <h ...
- Js 合并 table 行 的实现方法
Js 合并 table 行 的实现方法 需求如下: 某公司的员工档案,如下, 经理看员工的信息不是很清晰: 姓名 所在学校 毕业时间 张三 小学 2000 张三 中学 2006 张三 大学 2010 ...
- SQL中合并多行记录的方法总汇
-- =============================================================================-- Title: 在SQL中分类合并数 ...
- C# 使用Epplus导出Excel [4]:合并指定行
C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...
- 【HANA系列】SAP HANA SQL合并多行操作
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL合并多行 ...
- 合并表格行---三层for循环遍历数据
合并表格行---三层for循环遍历数据 示例1 json <!DOCTYPE html> <html lang="zh_cn"> <head> ...
- 详细说明svn分支与合并---命令行
一,svn分支与合并有什么用? 作程序的,对svn在熟悉不过了,但对svn分支熟悉的,我想并不多.因为一般情况下,是用不着svn分支的,其实也没有那个必要.下面我例举几个需要用到svn分支的情况: 1 ...
- html表格合并(行,一排)
<table> <tr> <td colspan="2">失败的例子:</td> </tr> {% for ip , j ...
- SQL SERVER 字符合并多行为一列
[字符合并多行为一列] 思路1:行转列,在与字符拼接(适用每组列数名相同) 思路2:转xml,去掉多余字符(适用所有) 假设兴趣表Hobbys Name Hobby 小张 打篮球 小张 踢足球 Nam ...
随机推荐
- 【Android Studio安装部署系列】三十四、将Eclipse项目导入到Android Studio中
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 我采用的是笨方法:新创建Android Studio项目,然后将Eclipse项目中的目录一一复制到Android Studio项目 ...
- SpringBoot系列——Redis
前言 Redis是一个缓存.消息代理和功能丰富的键值存储.StringBoot提供了基本的自动配置.本文记录一下springboot与redis的简单整合实例 官方文档:https://docs.sp ...
- 基于“formData批量上传的多种实现” 的多图片预览、上传的多种实现
前言 图片上传是web项目常见的需求,我基于之前的博客的代码(请戳:formData批量上传的多种实现)里的第三种方法实现多图片的预览.上传,并且支持三种方式添加图片到上传列表:选择图片.复制粘贴图片 ...
- 第64章 学习 - Identity Server 4 中文文档(v1.0.0)
以下是一些在线,远程和课堂培训选项,以了解有关ASP.NET Core Identity和IdentityServer4的更多信息. 64.1 现代应用程序的身份和访问控制(使用ASP.NET Cor ...
- 程序猿想聊天 - 創問 4C 團隊教練心得(一)
今天難得參加了創問舉辦的 4C 團隊教練課程 From : http://www.cccoach.cn/Home/Activity/show/id/449.html 整個課程主要圍繞著 Common ...
- js转换时间戳-转换成 yyyy-MM-dd HH:mm:ss
比如:转换成 yyyy-MM-dd HH:mm:ss //时间戳转换方法 date:时间戳数字 function formatDate(date) { var date = new Date(date ...
- linux下载服务器上的文件命令-sz
语法:sz 文件 比如要下载下面这个com.zip这个压缩包 输入sz com.zip 弹出下载页面,即可开始下载文件
- IO流-输入输出的简单实例
InputStream和OutputStream 抽象类InputStream和OutputStream是IO流最底层的两个抽象类,所有输入/输出流的类都基于这两个类. 这两个类里最核心的三个方法是r ...
- JavaScript基础-3
3 运算符 按照个数分类可分为:一元运算符.二元运算符.三元运算符: 按照功能分类可分为:算数运算符.自增运算符.比较运算符.逻辑运算符.赋值运算符: 3.1 算数运算符 算术运算符包含了加减乘除,符 ...
- Django 传递额外参数及 URL别名
传递额外参数到视图函数中 在 urls.py 文件中添加下面内容 from django.conf.urls import url urlpatterns = [ url(r'index', view ...