@Override
public TablePreviewModel getPreviewData(PartitionModel model)
throws GwtException {
try {
TablePreviewModel result;
String path = model.getPath();
JobContext jobContext = JobContext.getTempJobContext();
jobContext.setProperties(new HierarchyProperties(
new HashMap<String, String>()));
jobContext.getProperties().setProperty("preview.hdfs.path", path);
jobContext.getProperties().setProperty("preview.hdfs.inputFormat",
model.getInputFormat());
jobContext.getProperties().setProperty("preview.hdfs.isCompressed",
String.valueOf(model.isCompressed()));
Profile profile = profileManager.findByUid(LoginUser.getUser()
.getUid());
if (profile != null) {
String ugi = profile.getHadoopConf().get(
"hadoop.hadoop.job.ugi");
jobContext.getProperties().setProperty(
"preview.hadoop.job.ugi", ugi);
}
DataPreviewJob job = new DataPreviewJob(jobContext);
job.run();
String logContent = job.getJobContext().getJobHistory().getLog()
.getContent();
// log.error("---\n" + logContent + "============\n");
List<Tuple<Integer, List<String>>> datas = new ArrayList<Tuple<Integer, List<String>>>();
int count = 0;
// rcfile没有FieldDelim,读数据采用默认delim '\001'
char fieldDelim = model.getFieldDelim() == null ? DEFAULT_FIELD_DELIM
: model.getFieldDelim().charAt(0);
for (String line : logContent.split("\n")) {
if (line.startsWith("ZEUS# [output]")) {
String data = line.substring("ZEUS# [output]".length());
String[] fields = ZeusStringUtil.split(data, fieldDelim);
// log.error("fields:" + fields + "; FieldDelim:"
// + (int) fieldDelim);
if (fields.length == 0) {
continue;
}
List<String> list = new ArrayList<String>();
for (int i = 0; i < fields.length
&& i < model.getCols().size(); i++) {
list.add(fields[i]);
}
datas.add(new Tuple<Integer, List<String>>(count, list));
}
count++;
}
result = new TablePreviewModel();
List<String> headers = new ArrayList<String>();
for (TableColumnModel col : model.getCols()) {
headers.add(col.getName());
}
result.setHeaders(headers);
result.setData(datas);
return result;
} catch (Exception e) {
log.error("data preview error", e);
throw new GwtException(e.getMessage(), e);
}