in odps-sdk-impl/odps-common-local/src/main/java/com/aliyun/odps/local/common/utils/SchemaUtils.java [126:213]
public static TableMeta readSchema(File dir) {
if (dir == null || !dir.exists()) {
return null;
}
File schemaFile = new File(dir, Constants.SCHEMA_FILE);
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(schemaFile)));
} catch (FileNotFoundException e) {
throw new RuntimeException(
"__schema__ file not exists in direcotry " + dir.getAbsolutePath());
}
String line = null;
try {
line = br.readLine();
} catch (IOException e1) {
throw new RuntimeException(e1);
}
String project = null;
String table = null;
Column[] cols = null;
Column[] partitionCols = null;
while (line != null) {
line = line.trim();
if (line.equals("") || line.startsWith("#")) {
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
continue;
}
String[] kv = line.split(KEY_VALUE_SEPARATOR);
if (kv == null || kv.length != 2 || kv[0] == null || kv[0].trim().isEmpty() || kv[1] == null
|| kv[1].trim().isEmpty()) {
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
continue;
}
kv[0] = kv[0].trim();
kv[1] = kv[1].trim();
if (kv[0].equals("project")) {
project = kv[1];
} else if (kv[0].equals("table")) {
table = kv[1];
if (table == null || table.trim().isEmpty()) {
throw new RuntimeException("Table schema file '_schema_' must include 'table'");
}
} else if (kv[0].equals("columns")) {
String columns = kv[1];
if (columns == null || columns.trim().isEmpty()) {
throw new RuntimeException("Table schema file '_schema_' must include 'columns'");
}
cols = fromString(columns);
if (cols.length == 0) {
throw new RuntimeException("'columns' in table schema file '_schema_' has invalid value");
}
} else if (kv[0].equals("partitions")) {
String partitions = kv[1];
if (partitions != null && !partitions.trim().isEmpty()) {
partitionCols = fromString(partitions);
}
}
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
try {
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return new TableMeta(project, table, cols, partitionCols);
}