Created
April 11, 2012 00:45
-
-
Save shrijeet/2355991 to your computer and use it in GitHub Desktop.
Protobuf message from a tab delimited record
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import java.io.*; | |
import java.util.List; | |
import java.util.regex.Pattern; | |
import com.google.protobuf.Descriptors.FieldDescriptor; | |
import com.google.protobuf.Descriptors.FieldDescriptor.JavaType; | |
import com.example.generated.LogFileProtos.LogFile; | |
import com.example.generated.LogFileProtos.LogRecord; | |
public class TabDelimToProtoMessage { | |
static Pattern DELIMITER = Pattern.compile("\t"); | |
public static void main(String args[]) throws Exception { | |
if (args.length == 0) { | |
System.out.println("Give file names"); | |
} | |
LogFile.Builder logfile = LogFile.newBuilder(); | |
LogRecord.Builder recordB = LogRecord | |
.newBuilder(); | |
FileInputStream fstream = new FileInputStream(args[0]); | |
DataInputStream in = new DataInputStream(fstream); | |
BufferedReader br = new BufferedReader(new InputStreamReader(in), 20000); | |
String line; | |
List<FieldDescriptor> recordFields = recordB.getDescriptor() | |
.getFields(); | |
while ((line = br.readLine()) != null) { | |
String fields[] = DELIMITER.split(line); | |
for (int i = 0; i < fields.length; i++) { | |
FieldDescriptor current_field = recordFields.get(i); | |
if (fields[i].isEmpty()) { | |
recordB.setField(current_field, current_field | |
.getDefaultValue()); | |
} else { | |
recordB.setField(current_field, | |
getValueInType(current_field | |
.getJavaType(), fields[i])); | |
} | |
} | |
logfile.addRecord(recordB.build()); | |
} | |
FileOutputStream output = new FileOutputStream(args[1]); | |
logfile.build().writeTo(output); | |
output.close(); | |
} | |
private static Object getValueInType(JavaType type, String value) { | |
switch (type) { | |
case INT: | |
return Integer.valueOf(value); | |
case LONG: | |
return Long.valueOf(value); | |
case BOOLEAN: | |
return Boolean.valueOf(value); | |
case DOUBLE: | |
return Double.valueOf(value); | |
case STRING: | |
return value; | |
case FLOAT: | |
return Float.valueOf(value); | |
default: | |
return value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment