java中使用protobuf总结

基本没怎么接触过java编程,别的团队发过来一个用java编写的存储pb的文件,让拆分和解析,硬着头皮做一下,在此将步骤做个记录:

  1. 下载安装protobuf

https://github.com/protocolbuffers/protobuf/tags?after=v3.6.1.2

  1. 编译protoc解释器

tar -zxvf protobuf-all-3.6.1.tar.gz 
cd protobuf-3.6.1/
sudo ./autogen.sh
sudo ./configure
sudo make
sudo make check
sudo make install
sudo ldconfig  
  1. 定义接口

syntax = "proto3";
package com.union.fun;

message Metric {
    string name = 1;
    string type = 2;
    float value = 3;
    repeated string tags = 4;
  }
  1. 生成java语言的接口文件

protoc -I=/home/tiger/java_protobuf --java_out=/tiger/java_protobuf metric.proto

  1. 安装jdk8

sudo apt-get update

sudo apt-get install openjdk-8-jdk

  1. 安装maven

wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
tar -zvxf apache-maven-3.5.2-bin.tar.gz
复制到/opt/目录
cp apache-maven-3.5.2-bin.tar.gz /opt
设置环境变量
打开/etc/profile,配置PATH变量
sudo vim /etc/profile
export  MAVEN_HOME=/opt/apache-maven-3.5.2
export PATH=$MAVEN_HOME/bin:$PATH
  1. 生成protobuf对应jar包

cd protobuf-3.6.1/java

测试maven

mvn test

打包生成jar文件

mvn package

protobuf-java-3.6.1.jar文件 在 core/target目录

protobuf-java-util-3.6.1.jar文件 在 util/target目录

  1. 生成java接口

protoc -I=/home/tiger/java_protobuf --java_out=/tiger/java_protobuf metric.proto

  1. 代码编写

HelloWorld.java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.FileWriter;
import java.io.Writer;

import com.union.fun.MetricOuterClass;

public class HelloWorld {
    private static String currentPath = "";
    private static String strFileName = "testData.log";
    private static String strFileNameCatalog = "testData_Catalog.txt";
    private static File outputFile = null;
    private static FileOutputStream fileOutputStream = null;
    private static File outputCatalogFile = null;
    private static PrintWriter outputCatalogFileWriter = null;

    private static void savetestDataCatalog(String catalog) {
        try {
            if (!outputCatalogFile.getParentFile().exists()) {
                outputCatalogFile.getParentFile().mkdirs();
                System.out.println("savetestDataCatalog 1");
            }

            if (!outputCatalogFile.exists()) {
                outputCatalogFile.createNewFile();
                System.out.println("savetestDataCatalog 2");
            }
            if (outputCatalogFileWriter == null) {
                outputCatalogFileWriter = new PrintWriter(outputCatalogFile);
                System.out.println("savetestDataCatalog 3");
            }
            outputCatalogFileWriter.print(catalog);
            //System.out.println(catalog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void savetestData(MetricOuterClass.Metric testData) {
        try {
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
                System.out.println("savetestData 1");
            }

            if (!outputFile.exists()) {
                outputFile.createNewFile();
                System.out.println("savetestData 2");
            }
            if (fileOutputStream == null) {
                fileOutputStream = new FileOutputStream(outputFile, true);
                System.out.println("savetestData 3");
            }
            testData.writeDelimitedTo(fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void showUseMethod() {
        System.out.println("check totoal pb frame use param: {-i} {file}");
        System.out.println("cut pb use param: {-c} {file} {startpos} {endpos}");
    }

    private static void showPbTotalFrame(String inputFile) {
        File file = null;
        if (file == null) {
            file = new File(inputFile);
        }
        if (!file.exists()) {
            System.out.println(inputFile + " not exists !");
            return;
        }
        
        if (outputCatalogFile.exists()) {
            outputCatalogFile.delete();
        }

        if (!outputCatalogFile.getParentFile().exists()) {
            outputCatalogFile.getParentFile().mkdirs();
        }

        try {
            if (!outputCatalogFile.exists()) {
                outputCatalogFile.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        try {
            outputCatalogFileWriter = new PrintWriter(outputCatalogFile);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            return;
        }

        int iNormal = 0;
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            while (fileInputStream.available() != 0) {
                MetricOuterClass.Metric mWorldPackage =
                    MetricOuterClass.Metric.parseDelimitedFrom(fileInputStream);
                //iTotal++;
                if (mWorldPackage == null) {
                    //System.out.println("mWorldPackage == null");
                    continue;
                }
                
                //System.out.println("strTimeStamp = " + strTimeStamp);
                //System.out.println("strSub = " + strSub);
                /*
                String currentTime = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(millTimeStamp);
                if (!strSub.isEmpty()) {
                    currentTime += ".";
                    currentTime += strSub;
                }
               
                iNormal++;  
                String strItem = String.format("%d %s %s\r\n", iNormal, strTimeStamp, currentTime);
                */
                //System.out.println(currentTime);
                iNormal++; 
                int size = mWorldPackage.getSerializedSize(); 
                String strItem = String.format("%d %d\r\n", iNormal, size);
                savetestDataCatalog(strItem); 

            }
        } catch (IOException e) {
            System.out.println("IOException:" + e);
            e.printStackTrace();
        } finally {
        }
        outputCatalogFileWriter.close();
        System.out.println(String.format("total frame: %d", iNormal));
    }

    private static void cutPbFrame(String inputFile, int fromPos, int toPos) {
        File file = null;
        if (file == null) {
            file = new File(inputFile);
        }
        if (!file.exists()) {
            System.out.println(inputFile + " not exists !");
            return;
        }
        if (outputFile.exists()) {
            outputFile.delete();
        }
        int iPos = 0;
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            while (fileInputStream.available() != 0) {
                MetricOuterClass.Metric mWorldPackage =
                        MetricOuterClass.Metric.parseDelimitedFrom(fileInputStream);
                if (mWorldPackage == null) {
                    System.out.println("mWorldPackage == null");
                    continue;
                }
                iPos++;
                //notifyVehicleDataChanged(mWorldPackage);
                //SystemClock.sleep(SLEEP_TIME);
                
                //int size = mWorldPackage.getSerializedSize();
                //ByteBuffer byteBuffer = ByteBuffer.allocateDirect(size);
                //System.out.println(size);
                if (iPos >= fromPos && iPos <= toPos) {
                    savetestData(mWorldPackage);
                }
            }
        } catch (IOException e) {
            System.out.println("IOException:" + e);
            e.printStackTrace();
        } finally {

        }
    }

    public static void main(String[] args) {
        System.out.println("======  Welcome to using anp2 pb cutting tool ==========");
        //System.out.println(args.length);
        //System.out.println(args);
        
        /*
        System.out.println(System.getProperty("user.dir"));
        HelloWorld user = new HelloWorld();
        System.out.println(user.getClass().getResource("").getPath());
        System.out.println(user.getClass().getResource("/").getPath());
        System.out.println(user.getClass().getResource("./").getPath());
        */

        if (args.length < 2) {
            showUseMethod();
            return;
        }
        String inputFile = args[1];
        String filePriPath = "";
        String outputDataFile = "";
        String outputDataCatalogFile = "";
        String[] split = inputFile.split("\\\\");
        if (split.length > 1) {
            System.out.println("windows platfoem");
            String fileName = split[split.length - 1];
            filePriPath = inputFile.replace(fileName, "");
            //System.out.println(fileName);
            //System.out.println(filePriPath);
            currentPath = System.getProperty("user.dir") + "\\";
            outputDataFile = currentPath + "output\\" + strFileName;
            outputFile = new File(outputDataFile);

            outputDataCatalogFile = currentPath + "output\\" + strFileNameCatalog;
            outputCatalogFile = new File(outputDataCatalogFile);            
            try {
                outputCatalogFileWriter = new PrintWriter(outputCatalogFile);
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
                return;
            }
        }
        else {
            System.out.println("linux platfoem");
            split = inputFile.split("/");
            String fileName = split[split.length - 1];
            filePriPath = inputFile.replace(fileName, "");
            //System.out.println(fileName);
            //System.out.println(filePriPath);
            currentPath = System.getProperty("user.dir") + "/";
            outputDataFile = currentPath + "output/" + strFileName;
            outputFile = new File(outputDataFile);

            outputDataCatalogFile = currentPath + "output/" + strFileNameCatalog;
            outputCatalogFile = new File(outputDataCatalogFile);
        }
        System.out.println("user.dir is: " + currentPath);
 
        if (args[0].equals("-i")) {
            System.out.println("you input file is: " + inputFile);
            System.out.println("output file is: " + outputDataCatalogFile);
            showPbTotalFrame(inputFile);
            return;
        } else {
            if (args.length < 4) {
                showUseMethod();
                return;
            }
        }
        
        System.out.println("output file is: " + outputDataFile);
        int startPos = Integer.parseInt(args[2]);
        int endPos = Integer.parseInt(args[3]);
        System.out.println(String.format("you input file is: %s, start pos is: %d, end pos is: %d", 
                inputFile, startPos, endPos));
        cutPbFrame(inputFile, startPos, endPos);
    }
}
  1. 编译执行

javac -encoding UTF-8 -classpath .:./protobuf-java-3.6.1.jar:./protobuf-java-util-3.6.1.jar -d . HelloWorld.java

编译后代码结构如下:

java -classpath .:./protobuf-java-3.6.1.jar:./protobuf-java-util-3.6.1.jar HelloWorld -i testData.log

运行结果如下:

解析得到文件中每个PB顺序号和占用字节数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值