Skip to content

Instantly share code, notes, and snippets.

@shrijeet
Created April 11, 2012 00:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shrijeet/2355991 to your computer and use it in GitHub Desktop.
Save shrijeet/2355991 to your computer and use it in GitHub Desktop.
Protobuf message from a tab delimited record
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