private Schema buildOutputSchema(
      final PluginTask task, Schema inputSchema, List<ColumnConfig> jsonColumns) {
    ImmutableList.Builder<Column> builder = ImmutableList.builder();

    int i = 0;
    for (Column inputColumn : inputSchema.getColumns()) {
      Column outputColumn = new Column(i++, inputColumn.getName(), inputColumn.getType());
      builder.add(outputColumn);
    }
    for (ColumnConfig columnConfig : jsonColumns) {
      String columnName = task.getJoinedColumnsPrefix() + columnConfig.getName();
      builder.add(new Column(i++, columnName, columnConfig.getType()));
    }
    return new Schema(builder.build());
  }
  @Override
  public PageOutput open(
      TaskSource taskSource,
      final Schema inputSchema,
      final Schema outputSchema,
      final PageOutput output) {
    final PluginTask task = taskSource.loadTask(PluginTask.class);

    // create jsonColumns/baseColumn
    final List<Column> outputColumns = outputSchema.getColumns();
    final List<Column> inputColumns = inputSchema.getColumns();

    Map<String, Column> inputColumnMap = Maps.newHashMap();
    final List<Column> jsonColumns = new ArrayList<>();
    for (Column column : outputColumns) {
      if (!inputColumns.contains(column)) {
        jsonColumns.add(column);
      } else {
        inputColumnMap.put(column.getName(), column);
      }
    }

    final Column baseColumn = inputColumnMap.get(task.getBaseColumn().getName());

    // create timestampParserMap
    final HashMap<String, TimestampParser> timestampParserMap = Maps.newHashMap();
    for (ColumnConfig jsonColumnConfig : task.getJsonColumns()) {
      if (Types.TIMESTAMP.equals(jsonColumnConfig.getType())) {
        String format = jsonColumnConfig.getOption().get(String.class, "format");
        DateTimeZone timezone = DateTimeZone.forID(task.getTimeZone());
        TimestampParser parser = new TimestampParser(task.getJRuby(), format, timezone);
        timestampParserMap.put(task.getJoinedColumnsPrefix() + jsonColumnConfig.getName(), parser);
      }
    }

    // get jsonTable
    final HashMap<String, HashMap<String, String>> jsonTable = task.getJsonTable();

    return new LeftOuterJoinJsonTableFilterFilteredPageOutput(
        inputSchema, outputSchema, baseColumn, jsonTable, jsonColumns, timestampParserMap, output);
  }
Exemplo n.º 3
0
 @Override
 public void run(TaskSource taskSource, Schema schema, FileInput input, PageOutput output) {
   try (final PageBuilder pageBuilder =
       new PageBuilder(Exec.getBufferAllocator(), schema, output)) {
     while (input.nextFile()) {
       Buffer buffer = input.poll();
       if (buffer != null) {
         for (Column column : schema.getColumns()) {
           Type type = column.getType();
           switch (type.getName()) {
             case "boolean":
               pageBuilder.setBoolean(column, true);
               break;
             case "long":
               pageBuilder.setLong(column, 2L);
               break;
             case "double":
               pageBuilder.setDouble(column, 3.0D);
               break;
             case "string":
               pageBuilder.setString(column, "45");
               break;
             case "timestamp":
               pageBuilder.setTimestamp(column, Timestamp.ofEpochMilli(678L));
               break;
             default:
               throw new IllegalStateException("Unknown type: " + type.getName());
           }
         }
         pageBuilder.addRecord();
         if (raiseException) {
           throw new RuntimeException("emulated exception");
         }
       }
     }
     pageBuilder.finish();
   }
 }