kafka二次开发代码实例

17 篇文章 0 订阅

配置文件b.txt

#选择输出类型(0.筛选输出 1.全体输出 2.不输出数据)
input_type = 1
#筛选条件 source_mac(多条检索)、user(多条检索)、message(模糊查询) #time(1.当输入一个时间 格式:yyyy-MM-dd-HH:mm:ss 或者输入一个时间戳[秒级时间戳11位] 当输入时间小于等于当前时间不会收集数据 当输入时间大于当前时间将收集当前时间到输入时间数据 2.当输入二个时间将筛选俩时间段间数据)
Screening_conditions = message
#筛选内容
Screening_content = www
#输入左面时间小,右面时间大2017-12-06 12:12:12,2017-12-06 15:12:12
#数据输出路径
output_Route = E:\data1\shenji.txt
数据输出文件大小(字节) 1M=1048576B 1G=1073741824B
output_Size = 150000
----------------------------------------
#探针数据配置
#选择输出类型(0.筛选输出 1.全体输出 2.不输出数据)
tanzhen_input_type = 1
#筛选条件 ap_mac(设备mac) sta_mac(用户mac) time(时间)
tanzhen_Screening_conditions = ap_mac
#筛选内容
tanzhen_Screening_content = 60:cd:a9:01:1f:db,60:cd:a9:01:0e:89
tanzhen_output_Route=E:\data1\tanzhen
tanzhen_output_Size = 15000000
-----------------------------------------
#用户数据配置
#选择输出类型(0.筛选输出 1.全体输出 2.不输出数据))
yonghu_input_type = 1
#筛选条件 ap_mac(设备mac) sta_mac(用户mac) time(时间) phone(手机等验证信息)
yonghu_Screening_conditions = time
#筛选内容
yonghu_Screening_content =	1111
yonghu_output_Route=E:\data1\yonghu.txt
yonghu_output_Size = 150000

demo.java

import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class Demo  {
//探针数据

    public static void main(String[] args) {
//
//        KafkaConsumer kafkaConsumer = new KafkaConsumer(KafkaProperties.TOPIC1);
//        ConsumerConnector consumer=new KafkaConsumer(KafkaProperties.TOPIC1).createConnector();
//        Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
//        topicCountMap.put(kafkaConsumer.topic, 1);
//        Map<String, List<KafkaStream<byte[], byte[]>>> messageStream =  consumer.createMessageStreams(topicCountMap);
//
//        KafkaStream<byte[], byte[]> stream = messageStream.get(kafkaConsumer.topic).get(0);
//        //获取我们每次接收到的数据
//
//        ConsumerIterator<byte[], byte[]> iterator = stream.iterator();
        ConsumerIterator<byte[], byte[]> iterator=new KafkaConsumer(KafkaProperties.TOPIC1).dateOutput(KafkaProperties.TOPIC1,new KafkaProperties().ZK, new KafkaProperties().GROUP_ID);
     //   FileOutputStream fileOutputStream=new NewsScreening().createOutput("tanzhen_output_Route");
        long ts; //第一次输入时间
        long ds;  //第二次输入时间


        String strings = new NewsScreening().output("tanzhen_output_Route");//读取输出文件
        File file = new File(strings);
        Date date=new Date();
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String time4=simpleDateFormat1.format(date);
        File file3=file;
        File file4=file3.getParentFile();//获取输入目录
        String name=file3.getName();//获取输出名字
        String path=file4.toString();
        String path1="";
//        String path2=path+"\\"+time4+"\\"+name;
//        File files=new File(path+"\\"+time4);
        String path2=path+"/"+time4+"/"+name;
        File files=new File(path+"/"+time4);
        if(!files.exists()){
            files.mkdirs();
        }

        FileOutputStream fileOutputStream1= null;

        try {
            fileOutputStream1 = new FileOutputStream(new File(path2));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }





        while (iterator.hasNext()) {

            Date date1=new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH-mm-ss");
            String times=simpleDateFormat.format(date1);



            String time3=simpleDateFormat1.format(date);//时间年月日
//            File file5=new File(path+"\\"+time3);
            File file5=new File(path+"/"+time3);
            if(file5 !=null &&file5.exists()){
//                path1=path+"\\"+time3+"\\"+name;
                path1=path+"/"+time3+"/"+name;
            }else{
                //创建目录
                file5.mkdirs();
//                path1=path+"\\"+time3+"\\"+name;
                path1=path+"/"+time3+"/"+name;
            }



            long size=new NewsScreening().fileSize(path2);
            long sizes=Long.parseLong(new NewsScreening().output("tanzhen_output_Size"));
            if (size >= sizes) {

                //获取输出路径
                path2=path1+""+times;

                try {
                    fileOutputStream1=new FileOutputStream(path2);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }







            String message = new String(iterator.next().message());
            String type=new NewsScreening().output("tanzhen_input_type");//输出类型选择
            long dates = System.currentTimeMillis() / 1000; //获取当前时间戳
            if(type.equals("0")){  //筛选输出

              String string=new NewsScreening().output("tanzhen_Screening_conditions");  //选择筛选条件
                if(string.equals("ap_mac")){
                    String ap_mac=message.split("\"")[1];
                    String aa[] = new NewsScreening().screeningContent("tanzhen_Screening_content"); //筛选内容
                    List<String> list = Arrays.asList(aa);
                    if(list.contains(ap_mac)){
                        new NewsScreening().fileFlow(fileOutputStream1, message);
                    }
                }else if(string.equals("sta_mac")){
                    String sta_mac=message.split("\"")[3];
                    String aa[] = new NewsScreening().screeningContent("tanzhen_Screening_content"); //筛选内容
                    List<String> list = Arrays.asList(aa);
                    if(list.contains(sta_mac)){
                        new NewsScreening().fileFlow(fileOutputStream1, message);
                    }
                }else if(string.equals("time")) {
                    String time = message.split("\"")[19];
                    String aa[] = new NewsScreening().screeningContent("tanzhen_Screening_content");
                    if (aa.length == 1) {
                        String time1 = aa[0]; //获取输入文件时间
                        boolean authentication = new NewsScreening().isNumeric(time1);
                        if (authentication) {
                            ts = Long.parseLong(time1);//输入时间
                        } else {
                            ts = new NewsScreening().timeTransformation(time1);
                        }
                        if (ts >= dates) {

                            new NewsScreening().fileFlow(fileOutputStream1, message);
                        }
                    } else {
                        String time1 = aa[0]; //获取输入文件时间1
                        boolean authentication = new NewsScreening().isNumeric(time1);
                        if (!authentication) {
                            //输入第一次的时间
                            ts = new NewsScreening().timeTransformation(time1);
                        } else {
                            ts = Long.parseLong(time1);
                        }
                        String time2 = aa[1]; //获取输入文件时间2
                        boolean authentication1 = new NewsScreening().isNumeric(time2);
                        if (!authentication1) {
                            //输入第二次的时间
                            ds = new NewsScreening().timeTransformation(time2);
                        } else {
                            ds = Long.parseLong(time2);
                        }
                        if (ds >= dates && dates >= ts) {
                            new NewsScreening().fileFlow(fileOutputStream1, message);

                        }
                    }
                }
            }else if (type.equals("1")) {
                //输出全部
                new NewsScreening().fileFlow(fileOutputStream1, message);
            }else{
                break;
            }
        }
    }

}

生产者代码

package JavaKafka;

import kafka.producer.KeyedMessage;
import kafka.javaapi.producer.Producer;
import kafka.producer.ProducerConfig;
import java.util.Properties;
import java.io.*;
/**
 * kfaka生产者
 */

public class KafkaProducer extends Thread  {
    private String topic;

    private Producer<Integer, String> producer;

    public KafkaProducer(String topic) {
        this.topic = topic;

        Properties properties = new Properties();

        properties.put("metadata.broker.list",KafkaProperties.BROKERLIST);
        properties.put("serializer.class","kafka.serializer.StringEncoder");
        properties.put("request.required.acks","1");

        producer = new Producer<Integer, String>(new ProducerConfig(properties));
    }


    @Override
    public void run() {

        int messageNo = 1;

        while(true) {
            String message = "message_" + messageNo;
            producer.send(new KeyedMessage<Integer, String>(topic, message));
            System.out.println("Sent: " + message);

            messageNo ++ ;

            try{
                Thread.sleep(2000);
            } catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}

消费者代码

import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;


public class KafkaConsumer extends Thread{
    public String topic;


    public KafkaConsumer(String topic){

        this.topic=topic;
    }

//    public ConsumerConnector createConnector(){
//        Properties properties=new Properties();
//        properties.put("zookeeper.connect",KafkaProperties.ZK);
//        properties.put("group.id",KafkaProperties.GROUP_ID);
//        return Consumer.createJavaConsumerConnector(new ConsumerConfig(properties));
//    }

    public ConsumerConnector createConnector(String ZK,String GROUP_ID){
        Properties properties=new Properties();
        properties.put("zookeeper.connect",ZK);
        properties.put("group.id",GROUP_ID);
        return Consumer.createJavaConsumerConnector(new ConsumerConfig(properties));
    }

    public ConsumerIterator dateOutput(String TOPIC,String ZK,String GROUP_ID ) {
        KafkaConsumer kafkaConsumer = new KafkaConsumer(TOPIC);
        ConsumerConnector consumer = new KafkaConsumer(TOPIC).createConnector(ZK,GROUP_ID);
        Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
        topicCountMap.put(kafkaConsumer.topic, 1);
        Map<String, List<KafkaStream<byte[], byte[]>>> messageStream = consumer.createMessageStreams(topicCountMap);

        KafkaStream<byte[], byte[]> stream = messageStream.get(kafkaConsumer.topic).get(0);
        //获取我们每次接收到的数据

        ConsumerIterator<byte[], byte[]> iterator = stream.iterator();
        return iterator;
    }
//    public void run() {
//        ConsumerConnector consumer = createConnector();
//
//        Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
//        topicCountMap.put(topic, 1);
        topicCountMap.put(topic2, 1);
        topicCountMap.put(topic3, 1);
//
//        // String: topic
//        // List<KafkaStream<byte[], byte[]>>  对应的数据流
//        Map<String, List<KafkaStream<byte[], byte[]>>> messageStream =  consumer.createMessageStreams(topicCountMap);
//
//        KafkaStream<byte[], byte[]> stream = messageStream.get(topic).get(0);
//        //获取我们每次接收到的数据
//
//        ConsumerIterator<byte[], byte[]> iterator = stream.iterator();
//        String type=new NewsScreening().output("input_type"); //文件输出类型(0.全体输出 1.筛选输出 2.不输出)
//        FileOutputStream fileOutputStream = new NewsScreening().createOutput("output_Route");
//        //文件流
//        long ts; //第一次输入时间
//        long ds;  //第二次输入时间
//
//
//        while (iterator.hasNext()) {
//        String message = new String(iterator.next().message());
//        long dates = System.currentTimeMillis() / 1000; //获取当前时间戳
//        String str = new NewsScreening().output("Screening_conditions");//获取筛选条件
//        if (type.equals("0")) {
//            //审计数据以source_mac(源端mac)筛选
//            if (str.equals("source_mac")) {
//                if (message.split("\\{").length == 4) {
//                    String source_mac = message.split("\\{")[2].split(":")[3].split("\"")[1];
//
//                    //String aa[]={"3C-8C-40-0C-4E-10","00-0F-E2-41-5E-5B"};
//                    String aa[] = new NewsScreening().screeningContent("Screening_content");
//                    List<String> list = Arrays.asList(aa);
//                    if (list.contains(source_mac)) {
//
//                        new NewsScreening().fileFlow(fileOutputStream, message);
//
//                    }
//                }
//            } else if (str.equals("user")) {
//                //以用户名筛选
//                if (message.split("\\{").length == 4 && message.split("\\{")[3].split("\"").length == 31) {
//                    String user = message.split("\\{")[3].split("\"")[13];
//                    String aa[] = new NewsScreening().screeningContent("Screening_content");
//                    List<String> list = Arrays.asList(aa);
//                    if (list.contains(user)) {
//                        new NewsScreening().fileFlow(fileOutputStream, message);
//                    }
//                }
//            } else if (str.equals("message")) {
//                //模糊查询url
//                if (message.split("\\{").length == 4 && message.split("\\{")[3].split("\"").length == 31) {
//                    String message1 = message.split("\"")[message.split("\"").length - 6];
//                    String aa[] = new NewsScreening().screeningContent("Screening_content");
//                    String url = aa[0];
//                    if (message1.contains(url)) {
//                        new NewsScreening().fileFlow(fileOutputStream, message);
//
//                    }
//                }
//            } else if (str.equals("time")) {
//                //筛选时间戳范围
//                if (message.split("\\{").length == 4) {
//                    String time = message.split("\\{")[1].split(":")[5].split(",")[0];
//                    String aa[] = new NewsScreening().screeningContent("Screening_content");
//                    if (aa.length == 1) {
//                        String time1 = aa[0]; //获取输入文件时间
//                        boolean authentication = new NewsScreening().isNumeric(time1);
//                        if (authentication) {
//                            ts = Long.parseLong(time1);//输入时间
//                        } else {
//                            ts = new NewsScreening().timeTransformation(time1);
//                        }
//                        if (ts >= dates) {
//
//                            new NewsScreening().fileFlow(fileOutputStream, message);
//                        }
//                    } else {
//                        String time1 = aa[0]; //获取输入文件时间1
//                        boolean authentication = new NewsScreening().isNumeric(time1);
//                        if (!authentication) {
//                            //输入第一次的时间
//                            ts = new NewsScreening().timeTransformation(time1);
//                        } else {
//                            ts = Long.parseLong(time1);
//                        }
//                        String time2 = aa[1]; //获取输入文件时间2
//                        boolean authentication1 = new NewsScreening().isNumeric(time2);
//                        if (!authentication1) {
//                            //输入第二次的时间
//                            ds = new NewsScreening().timeTransformation(time2);
//                        } else {
//                            ds = Long.parseLong(time2);
//                        }
//                        if (ds >= dates && dates >= ts) {
//                            new NewsScreening().fileFlow(fileOutputStream, message);
//
//                        }
//
//                    }
//
//                }
//            }
//
//
//        }else if (type.equals("1")) {
//            //输出全部
//            new NewsScreening().fileFlow(fileOutputStream, message);
//        }else{
//            break;
//        }
//
//    }
//
//
//
//
//
//}


}

链接代码

/**
 * Kafka常用配置文件
 */

public class KafkaProperties {
    public static final String ZK = "node01:2181";
    //任子行审计
    public static final String TOPIC = "dess";
    //探针
    public static final String TOPIC1 = "tanzhen";

    public static final String TOPIC2 = "yonghu";

    public static final String TOPIC3 = "shenji";

    public static final String BROKERLIST = "node01:9093,node01:9094,node01:9095";

    public static final String GROUP_ID = "test_group1";
}

二次开发代码

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class NewsScreening {
    //消息筛选
 //   File file=new File("E:\\data1\\b.txt");//配置文件所在路径
    File file=new File("/app/java/b.txt");//配置文件所在路径
    BufferedReader reader=null;
    String tempString= null;    //每行的数据
    String filter_file=null;    //文件筛选内容
    String inputType=null;           //文件传输类型
    int array_long=0;           //数组的长度


    //判断是否是纯数字
    public  boolean isNumeric(String str){
        for (int i = 0; i < str.length(); i++){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }
    //将标准时间格式转化为时间戳

    public long timeTransformation(String string){
        long ts=0;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dt = simpleDateFormat.parse(string);
            ts = dt.getTime()/1000;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ts;
    }
//文件流写入
    public void fileFlow(FileOutputStream fileOutputStream,String string){

        try {
  //          System.out.println(string);
            fileOutputStream.write(string.getBytes());
 //           fileOutputStream.write("\r\n".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //创建输出文件
    public FileOutputStream createOutput(String string){
        FileOutputStream fileOutputStream = null;//文件流

        try {
            fileOutputStream = new FileOutputStream(new File(new NewsScreening().output(string)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return fileOutputStream;
    }

    //读取配置文件
    public String output(String string){

        try {
            reader = new BufferedReader(new FileReader(file));
            while ((tempString = reader.readLine()) != null) {
                if(tempString.startsWith(string)){
                    inputType=tempString.split("=")[1].trim();
                }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return inputType;
    }

    //返回筛选内容
    public String[] screeningContent(String string){
        try {
            reader = new BufferedReader(new FileReader(file));
            while ((tempString = reader.readLine()) != null) {

                if(tempString.startsWith(string)){
                    filter_file=tempString.split("=")[1].trim();
                    array_long = filter_file.split(",").length;
                }
            }
            reader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        String[] demo=new String[array_long];
        for(int a=0;a<array_long;a++){
            demo[a]= filter_file.split(",")[a];
        }
        return demo;
    }

//    public static void main(String[] args) {
//        String bb=new NewsScreening().outputRoute();
//        System.out.println(bb);
//        }



public long fileSize(String string){
  //传递文件目录,返回文件大小
    long s=0;
    File file=new File(string);
    if(file.exists()){
        try {
            FileInputStream fis= new FileInputStream(file);

            try {
                s= fis.available();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }else{
        try {
            //文件不存在,创建文件
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return s;
}



}
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class yonghu  {
    public static void main(String[] args) {

//        KafkaConsumer kafkaConsumer = new KafkaConsumer(KafkaProperties.TOPIC2);
//        ConsumerConnector consumer = new KafkaConsumer(KafkaProperties.TOPIC2).createConnector();
//        Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
//        topicCountMap.put(kafkaConsumer.topic, 1);
//        Map<String, List<KafkaStream<byte[], byte[]>>> messageStream = consumer.createMessageStreams(topicCountMap);
//
//        KafkaStream<byte[], byte[]> stream = messageStream.get(kafkaConsumer.topic).get(0);
//        //获取我们每次接收到的数据
//
//        ConsumerIterator<byte[], byte[]> iterator = stream.iterator();
        ConsumerIterator<byte[], byte[]> iterator=new KafkaConsumer(KafkaProperties.TOPIC2).dateOutput(KafkaProperties.TOPIC2,new KafkaProperties().ZK, new KafkaProperties().GROUP_ID);

        //       FileOutputStream fileOutputStream=new NewsScreening().createOutput("yonghu_output_Route");
//        FileOutputStream fileOutputStream = new NewsScreening().createOutput("yonghu_output_Route");
        long ts; //第一次输入时间
        long ds;  //第二次输入时间



        String strings = new NewsScreening().output("yonghu_output_Route");//读取输出文件
        File file = new File(strings);
        Date date=new Date();
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String time4=simpleDateFormat1.format(date);
        File file3=file;
        File file4=file3.getParentFile();//获取输入目录
        String name=file3.getName();//获取输出名字
        String path=file4.toString();
       String path1="";
//       String path2=path+"\\"+time4+"\\"+name;
//       File files=new File(path+"\\"+time4);

        String path2=path+"/"+time4+"/"+name;
        File files=new File(path+"/"+time4);

        if(!files.exists()){
            files.mkdirs();
        }

        FileOutputStream fileOutputStream1= null;

        try {
            fileOutputStream1 = new FileOutputStream(new File(path2));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        while (iterator.hasNext()) {

            Date date1=new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH-mm-ss");
            String times=simpleDateFormat.format(date1);



            String time3=simpleDateFormat1.format(date);//时间年月日
//            File file5=new File(path+"\\"+time3);
            File file5=new File(path+"/"+time3);

            if(file5 !=null &&file5.exists()){
//                path1=path+"\\"+time3+"\\"+name;

                path1=path+"/"+time3+"/"+name;
            }else{
                //创建目录
                file5.mkdirs();
//                path1=path+"\\"+time3+"\\"+name;
                path1=path+"/"+time3+"/"+name;
            }



            long size=new NewsScreening().fileSize(path2);
            long sizes=Long.parseLong(new NewsScreening().output("yonghu_output_Size"));
            if (size >= sizes) {

                //获取输出路径
                path2=path1+""+times;

                try {
                    fileOutputStream1=new FileOutputStream(path2);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
//            long size=new NewsScreening().fileSize(new NewsScreening().output("yonghu_output_Route"));
//            if(size>=104857600 ){
//
//            }

            String message = new String(iterator.next().message());
            String type=new NewsScreening().output("yonghu_input_type");//输出类型选择
            long dates = System.currentTimeMillis() / 1000; //获取当前时间戳
            if(type.equals("0")) {  //筛选输出
                String string=new NewsScreening().output("yonghu_Screening_conditions");
                if(string.equals("phone")){
                    String phone=message.split("\"")[3];
                    String aa[] = new NewsScreening().screeningContent("yonghu_Screening_content"); //筛选内容
                    List<String> list = Arrays.asList(aa);
                    if(list.contains(phone)){
                        new NewsScreening().fileFlow(fileOutputStream1, message);
                    }
                }else if(string.equals("sta_mac")){
                    String sta_mac=message.split("\"")[7];
                    String aa[] = new NewsScreening().screeningContent("yonghu_Screening_content"); //筛选内容
                    List<String> list = Arrays.asList(aa);
                    if(list.contains(sta_mac)){
                      new NewsScreening().fileFlow(fileOutputStream1, message);
                    }
                }else if(string.equals("ap_mac")){
                    String ap_mac=message.split("\"")[9];
                    String aa[] = new NewsScreening().screeningContent("yonghu_Screening_content"); //筛选内容
                    List<String> list = Arrays.asList(aa);
                    if(list.contains(ap_mac)){
                        new NewsScreening().fileFlow(fileOutputStream1, message);
                    }
                }else if(string.equals("time")){
                    String time=message.split("\"")[1];
                    String aa[] = new NewsScreening().screeningContent("yonghu_Screening_content");
                    if (aa.length == 1) {
                        String time1 = aa[0]; //获取输入文件时间
                        boolean authentication = new NewsScreening().isNumeric(time1);
                        if (authentication) {
                            ts = Long.parseLong(time1);//输入时间
                        } else {
                            ts = new NewsScreening().timeTransformation(time1);
                        }
                        if (ts >= dates) {

                            new NewsScreening().fileFlow(fileOutputStream1, message);
                        }
                    }else {
                        String time1 = aa[0]; //获取输入文件时间1
                        boolean authentication = new NewsScreening().isNumeric(time1);
                        if (!authentication) {
                            //输入第一次的时间
                            ts = new NewsScreening().timeTransformation(time1);
                        } else {
                            ts = Long.parseLong(time1);
                        }
                        String time2 = aa[1]; //获取输入文件时间2
                        boolean authentication1 = new NewsScreening().isNumeric(time2);
                        if (!authentication1) {
                            //输入第二次的时间
                            ds = new NewsScreening().timeTransformation(time2);
                        } else {
                            ds = Long.parseLong(time2);
                        }
                        if (ds >= dates && dates >= ts) {
                            new NewsScreening().fileFlow(fileOutputStream1, message);

                        }
                    }
                }
            }else if(type.equals("1")){
                new NewsScreening().fileFlow(fileOutputStream1, message);
            }else{
                break;
            }
        }
    }
}
import kafka.consumer.ConsumerIterator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class shenji  {
    public static void main(String[] args) {
        ConsumerIterator<byte[], byte[]> iterator=new KafkaConsumer(KafkaProperties.TOPIC).dateOutput(KafkaProperties.TOPIC,new KafkaProperties().ZK, new KafkaProperties().GROUP_ID);
  //      FileOutputStream fileOutputStream=new NewsScreening().createOutput("output_Route");
        long ts; //第一次输入时间
        long ds;  //第二次输入时间


        String strings = new NewsScreening().output("output_Route");//读取输出文件
        File file = new File(strings);
        Date date=new Date();
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String time4=simpleDateFormat1.format(date);
        File file3=file;
        File file4=file3.getParentFile();//获取输入目录
        String name=file3.getName();//获取输出名字
        String path=file4.toString();
        String path1="";

//        String path2=path+"\\"+time4+"\\"+name;
//        File files=new File(path+"\\"+time4);
        String path2=path+"/"+time4+"/"+name;
        File files=new File(path+"/"+time4);

        if(!files.exists()){
            files.mkdirs();
        }

        FileOutputStream fileOutputStream1= null;

        try {
            fileOutputStream1 = new FileOutputStream(new File(path2));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }




        String type=new NewsScreening().output("input_type"); //文件输出类型(0.全体输出 1.筛选输出 2.不输出)
        while (iterator.hasNext()) {

            Date date1=new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH-mm-ss");
            String times=simpleDateFormat.format(date1);



            String time3=simpleDateFormat1.format(date);//时间年月日
//            File file5=new File(path+"\\"+time3);
            File file5=new File(path+"/"+time3);
            if(file5 !=null &&file5.exists()){
  //              path1=path+"\\"+time3+"\\"+name;
                path1=path+"/"+time3+"/"+name;
            }else{
                //创建目录
                file5.mkdirs();
  //              path1=path+"\\"+time3+"\\"+name;
                path1=path+"/"+time3+"/"+name;
            }



            long size=new NewsScreening().fileSize(path2);
            long sizes=Long.parseLong(new NewsScreening().output("output_Size"));
            if (size >= sizes) {

                //获取输出路径
                path2=path1+""+times;

                try {
                    fileOutputStream1=new FileOutputStream(path2);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }










            String message = new String(iterator.next().message());
            long dates = System.currentTimeMillis() / 1000; //获取当前时间戳
            String str = new NewsScreening().output("Screening_conditions");//获取筛选条件
            if (type.equals("0")) {
                //审计数据以source_mac(源端mac)筛选
                if (str.equals("source_mac")) {
                    if (message.split("\\{").length == 4) {
                        String source_mac = message.split("\\{")[2].split(":")[3].split("\"")[1];

                        //String aa[]={"3C-8C-40-0C-4E-10","00-0F-E2-41-5E-5B"};
                        String aa[] = new NewsScreening().screeningContent("Screening_content");
                        List<String> list = Arrays.asList(aa);
                        if (list.contains(source_mac)) {

                            new NewsScreening().fileFlow(fileOutputStream1, message);

                        }
                    }
                } else if (str.equals("user")) {
                    //以用户名筛选
                    if (message.split("\\{").length == 4 && message.split("\\{")[3].split("\"").length == 31) {
                        String user = message.split("\\{")[3].split("\"")[13];
                        String aa[] = new NewsScreening().screeningContent("Screening_content");
                        List<String> list = Arrays.asList(aa);
                        if (list.contains(user)) {
                            new NewsScreening().fileFlow(fileOutputStream1, message);
                        }
                    }
                } else if (str.equals("message")) {
                    //模糊查询url
                    if (message.split("\\{").length == 4 && message.split("\\{")[3].split("\"").length == 31) {
                        String message1 = message.split("\"")[message.split("\"").length - 6];
                        String aa[] = new NewsScreening().screeningContent("Screening_content");
                        String url = aa[0];
                        if (message1.contains(url)) {
                            new NewsScreening().fileFlow(fileOutputStream1, message);

                        }
                    }
                } else if (str.equals("time")) {
                    //筛选时间戳范围
                    if (message.split("\\{").length == 4) {
                        String time = message.split("\\{")[1].split(":")[5].split(",")[0];
                        String aa[] = new NewsScreening().screeningContent("Screening_content");
                        if (aa.length == 1) {
                            String time1 = aa[0]; //获取输入文件时间
                            boolean authentication = new NewsScreening().isNumeric(time1);
                            if (authentication) {
                                ts = Long.parseLong(time1);//输入时间
                            } else {
                                ts = new NewsScreening().timeTransformation(time1);
                            }
                            if (ts >= dates) {

                                new NewsScreening().fileFlow(fileOutputStream1, message);
                            }
                        } else {
                            String time1 = aa[0]; //获取输入文件时间1
                            boolean authentication = new NewsScreening().isNumeric(time1);
                            if (!authentication) {
                                //输入第一次的时间
                                ts = new NewsScreening().timeTransformation(time1);
                            } else {
                                ts = Long.parseLong(time1);
                            }
                            String time2 = aa[1]; //获取输入文件时间2
                            boolean authentication1 = new NewsScreening().isNumeric(time2);
                            if (!authentication1) {
                                //输入第二次的时间
                                ds = new NewsScreening().timeTransformation(time2);
                            } else {
                                ds = Long.parseLong(time2);
                            }
                            if (ds >= dates && dates >= ts) {
                                new NewsScreening().fileFlow(fileOutputStream1, message);

                            }

                        }

                    }
                }


            }else if (type.equals("1")) {
                //输出全部
                new NewsScreening().fileFlow(fileOutputStream1, message);
            }else{
                break;
            }

        }
    }
}



当然,我可以给你介绍一下Kafka在C++中的代码实例Kafka是一个分布式流处理平台,它使用高吞吐量、低延迟的方式来处理大规模的数据流。在C++中使用Kafka,你可以使用librdkafka库来进行操作。 下面是一个简单的Kafka C++代码实例,用于生产者发送消息到Kafka集群: ```cpp #include <iostream> #include <string> #include <librdkafka/rdkafkacpp.h> int main() { std::string brokers = "localhost:9092"; // Kafka集群的地址 std::string topic = "test_topic"; // Kafka主题 // 创建配置对象 RdKafka::Conf *conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL); // 设置Kafka集群的地址 std::string errstr; if (conf->set("bootstrap.servers", brokers, errstr) != RdKafka::Conf::CONF_OK) { std::cerr << "Failed to set broker list: " << errstr << std::endl; delete conf; return 1; } // 创建生产者对象 RdKafka::Producer *producer = RdKafka::Producer::create(conf, errstr); if (!producer) { std::cerr << "Failed to create producer: " << errstr << std::endl; delete conf; return 1; } // 创建消息对象 std::string message = "Hello, Kafka!"; RdKafka::ProducerRecord *record = new RdKafka::ProducerRecord(topic, RdKafka::Topic::PARTITION_UA, RdKafka::ProducerRecord::COPY, &message, message.size()); // 发送消息 RdKafka::ErrorCode resp = producer->produce(record); if (resp != RdKafka::ERR_NO_ERROR) { std::cerr << "Failed to produce message: " << RdKafka::err2str(resp) << std::endl; delete record; delete producer; delete conf; return 1; } // 等待消息发送完成 producer->flush(1000); // 释放资源 delete record; delete producer; delete conf; return 0; } ``` 这个代码示例演示了如何使用librdkafka库创建一个生产者,并发送一条消息到指定的Kafka主题。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值