android打点数据通过http发送到flume,再到kafka
http source -> kafka sink 配置文件
# 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.
agent.sources=r1
agent.sinks=k1
agent.channels=c1
agent.sources.r1.type=http
agent.sources.r1.bind=192.168.1.232
agent.sources.r1.port=5140
agent.sources.r1.channels=c1
agent.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
agent.sinks.k1.kafka.topic = my-replicated-test
agent.sinks.k1.kafka.bootstrap.servers = 192.168.1.232:9092,192.168.1.233:9092,192.168.1.234:9092
agent.sinks.k1.kafka.flumeBatchSize = 100
agent.sinks.k1.kafka.producer.acks = 1
agent.sinks.k1.kafka.producer.linger.ms = 1
agent.sinks.k1.serializer.class=kafka.serializer.StringEncoder
agent.sinks.k1.channel=c1
agent.channels.c1.type=memory
agent.channels.c1.capacity=1000
agent.channels.c1.transactionCapacity=100
Android端通过http请求发送json数据到kafka
package com.lrc.sports.util; import com.alibaba.fastjson.JSONArray; import com.lrc.sports.bean.Student; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; /** * Created by li on 2016/11/28. * 通过http发送json数据到flume */ public class HttpUtil { /** * @param url * @param param * @return */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); //设定 请求格式 json,也可以设定xml格式的 // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); } //使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } public static void main(String[] args) throws InterruptedException { while(true) { List<Student> list = new ArrayList<Student>(1); Student s = new Student(); s.setAge((int) (Math.random() * 100)); s.setName("test"); list.add(s); sendPost("http://192.168.1.235:5140", JSONArray.toJSONString(list)); Thread.sleep(1000); } } }

浙公网安备 33010602011771号