org.apache.flink.types.Row类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(900)

本文整理了Java中org.apache.flink.types.Row类的一些代码示例,展示了Row类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Row类的具体详情如下:
包路径:org.apache.flink.types.Row
类名称:Row

Row介绍

[英]A Row can have arbitrary number of fields and contain a set of fields, which may all be different types. The fields in Row can be null. Due to Row is not strongly typed, Flink's type extraction mechanism can't extract correct field types. So that users should manually tell Flink the type information via creating a RowTypeInfo.

The fields in the Row can be accessed by position (zero-based) #getField(int). And can set fields by #setField(int,Object).

Row is in principle serializable. However, it may contain non-serializable fields, in which case serialization will fail.
[中]一行可以有任意数量的字段,并包含一组字段,这些字段可能都是不同类型的。第行中的字段可以为空。由于行不是强类型,Flink的类型提取机制无法提取正确的字段类型。因此,用户应该通过创建RowTypeInfo手动告诉Flink类型信息。
行中的字段可以通过位置(基于零)#getField(int)访问。并且可以通过#setField(int,Object)设置字段。
Row原则上是可序列化的。但是,它可能包含不可序列化的字段,在这种情况下,序列化将失败。

代码示例

代码示例来源:origin: apache/flink

@Override
  protected Object[] extractFields(Row record) {

    Object[] fields = new Object[record.getArity()];
    for (int i = 0; i < fields.length; i++) {
      fields[i] = record.getField(i);
    }
    return fields;
  }
}

代码示例来源:origin: apache/flink

@Override
public Row copy(Row from) {
  int len = fieldSerializers.length;
  if (from.getArity() != len) {
    throw new RuntimeException("Row arity of from does not match serializers.");
  }
  Row result = new Row(len);
  for (int i = 0; i < len; i++) {
    Object fromField = from.getField(i);
    if (fromField != null) {
      Object copy = fieldSerializers[i].copy(fromField);
      result.setField(i, copy);
    }
    else {
      result.setField(i, null);
    }
  }
  return result;
}

代码示例来源:origin: apache/flink

@Test
public void testRowOf() {
  Row row1 = Row.of(1, "hello", null, Tuple2.of(2L, "hi"), true);
  Row row2 = new Row(5);
  row2.setField(0, 1);
  row2.setField(1, "hello");
  row2.setField(2, null);
  row2.setField(3, new Tuple2<>(2L, "hi"));
  row2.setField(4, true);
  assertEquals(row1, row2);
}

代码示例来源:origin: apache/flink

@Override
protected Row fillRecord(Row reuse, Object[] parsedValues) {
  Row reuseRow;
  if (reuse == null) {
    reuseRow = new Row(arity);
  } else {
    reuseRow = reuse;
  }
  for (int i = 0; i < parsedValues.length; i++) {
    reuseRow.setField(i, parsedValues[i]);
  }
  return reuseRow;
}

代码示例来源:origin: apache/flink

@Override
  public Object apply(Object o) {
    if (o == null) {
      return null;
    } else {
      Row r = (Row) o;
      Row copy = new Row(copyFields.length);
      for (int i = 0; i < copyFields.length; i++) {
        copy.setField(i, copyFields[i].apply(r.getField(i)));
      }
      return copy;
    }
  }
}

代码示例来源:origin: apache/flink

format.open(split);
Row result = new Row(5);
assertEquals(11.1, result.getField(0));
assertEquals(22.2, result.getField(1));
assertEquals(33.3, result.getField(2));
assertEquals(44.4, result.getField(3));
assertEquals(55.5, result.getField(4));
assertEquals(66.6, result.getField(0));
assertEquals(77.7, result.getField(1));
assertEquals(88.8, result.getField(2));
assertEquals(99.9, result.getField(3));
assertEquals(0.0, result.getField(4));

代码示例来源:origin: com.alibaba.blink/flink-table

@Override
  protected Row convert(Row current) {
    if (reuse == null) {
      return Row.copy(current);
    } else {
      for (int i = 0; i < current.getArity(); ++i) {
        reuse.setField(i, current.getField(i));
      }
      return reuse;
    }
  }
}

代码示例来源:origin: apache/flink

@Override
public Row copy(Row from, Row reuse) {
  int len = fieldSerializers.length;
  // cannot reuse, do a non-reuse copy
  if (reuse == null) {
    return copy(from);
  }
  if (from.getArity() != len || reuse.getArity() != len) {
    throw new RuntimeException(
      "Row arity of reuse or from is incompatible with this RowSerializer.");
  }
  for (int i = 0; i < len; i++) {
    Object fromField = from.getField(i);
    if (fromField != null) {
      Object reuseField = reuse.getField(i);
      if (reuseField != null) {
        Object copy = fieldSerializers[i].copy(fromField, reuseField);
        reuse.setField(i, copy);
      }
      else {
        Object copy = fieldSerializers[i].copy(fromField);
        reuse.setField(i, copy);
      }
    }
    else {
      reuse.setField(i, null);
    }
  }
  return reuse;
}

代码示例来源:origin: apache/flink

@Override
  protected Object[] extract(Row record) {
    Object[] al = new Object[rowArity];
    for (int i = 0; i < rowArity; i++) {
      al[i] = record.getField(i);
    }
    return al;
  }
}

代码示例来源:origin: apache/flink

/**
 * Sets a repeating value to all objects or row fields of the passed vals array.
 *
 * @param vals The array of objects or Rows.
 * @param fieldIdx If the objs array is an array of Row, the index of the field that needs to be filled.
 *                 Otherwise a -1 must be passed and the data is directly filled into the array.
 * @param repeatingValue The value that is set.
 * @param childCount The number of times the value is set.
 */
private static void fillColumnWithRepeatingValue(Object[] vals, int fieldIdx, Object repeatingValue, int childCount) {
  if (fieldIdx == -1) {
    // set value as an object
    Arrays.fill(vals, 0, childCount, repeatingValue);
  } else {
    // set value as a field of Row
    Row[] rows = (Row[]) vals;
    for (int i = 0; i < childCount; i++) {
      rows[i].setField(fieldIdx, repeatingValue);
    }
  }
}

代码示例来源:origin: apache/flink

/**
 * Stores the next resultSet row in a tuple.
 *
 * @param row row to be reused.
 * @return row containing next {@link Row}
 * @throws java.io.IOException
 */
@Override
public Row nextRecord(Row row) throws IOException {
  try {
    if (!hasNext) {
      return null;
    }
    for (int pos = 0; pos < row.getArity(); pos++) {
      row.setField(pos, resultSet.getObject(pos + 1));
    }
    //update hasNext after we've read the record
    hasNext = resultSet.next();
    return row;
  } catch (SQLException se) {
    throw new IOException("Couldn't read data - " + se.getMessage(), se);
  } catch (NullPointerException npe) {
    throw new IOException("Couldn't access resultSet", npe);
  }
}

代码示例来源:origin: apache/flink

@Test
public void testRowToString() {
  Row row = new Row(5);
  row.setField(0, 1);
  row.setField(1, "hello");
  row.setField(2, null);
  row.setField(3, new Tuple2<>(2, "hi"));
  row.setField(4, "hello world");
  assertEquals("1,hello,null,(2,hi),hello world", row.toString());
}

代码示例来源:origin: apache/flink

@Test
public void testRowCopy() {
  Row row = new Row(5);
  row.setField(0, 1);
  row.setField(1, "hello");
  row.setField(2, null);
  row.setField(3, new Tuple2<>(2, "hi"));
  row.setField(4, "hello world");
  Row copy = Row.copy(row);
  assertEquals(row, copy);
  assertTrue(row != copy);
}

代码示例来源:origin: apache/flink

@Test
  public void testRowProject() {
    Row row = new Row(5);
    row.setField(0, 1);
    row.setField(1, "hello");
    row.setField(2, null);
    row.setField(3, new Tuple2<>(2, "hi"));
    row.setField(4, "hello world");

    Row projected = Row.project(row, new int[]{0, 2, 4});

    Row expected = new Row(3);
    expected.setField(0, 1);
    expected.setField(1, null);
    expected.setField(2, "hello world");
    assertEquals(expected, projected);
  }
}

代码示例来源:origin: apache/flink

@Override
public Row createInstance() {
  return new Row(fieldSerializers.length);
}

代码示例来源:origin: apache/flink

final String[] changeRow = new String[change.f1.getArity() + 1];
final String[] row = rowToString(change.f1);
System.arraycopy(row, 0, changeRow, 1, row.length);

代码示例来源:origin: apache/flink

@Override
public void run(SourceContext<Row> ctx) throws Exception {
  long offsetMS = offsetSeconds * 2000L;
  while (ms < durationMs) {
    synchronized (ctx.getCheckpointLock()) {
      for (int i = 0; i < numKeys; i++) {
        ctx.collect(Row.of(i, ms + offsetMS, "Some payload..."));
      }
      ms += sleepMs;
    }
    Thread.sleep(sleepMs);
  }
}

代码示例来源:origin: apache/flink

format.open(split);
Row result = new Row(4);
assertEquals(Date.valueOf("1990-10-14"), result.getField(0));
assertEquals(Time.valueOf("02:42:25"), result.getField(1));
assertEquals(Timestamp.valueOf("1990-10-14 02:42:25.123"), result.getField(2));
assertEquals(Timestamp.valueOf("1990-01-04 02:02:05"), result.getField(3));
assertEquals(Date.valueOf("1990-10-14"), result.getField(0));
assertEquals(Time.valueOf("02:42:25"), result.getField(1));
assertEquals(Timestamp.valueOf("1990-10-14 02:42:25.123"), result.getField(2));
assertEquals(Timestamp.valueOf("1990-01-04 02:02:05.3"), result.getField(3));

代码示例来源:origin: apache/flink

/**
 * Creates a new Row and assigns the given values to the Row's fields.
 * This is more convenient than using the constructor.
 *
 * <p>For example:
 *
 * <pre>
 *     Row.of("hello", true, 1L);}
 * </pre>
 * instead of
 * <pre>
 *     Row row = new Row(3);
 *     row.setField(0, "hello");
 *     row.setField(1, true);
 *     row.setField(2, 1L);
 * </pre>
 *
 */
public static Row of(Object... values) {
  Row row = new Row(values.length);
  for (int i = 0; i < values.length; i++) {
    row.setField(i, values[i]);
  }
  return row;
}

代码示例来源:origin: com.alibaba.blink/flink-table

@Override
public Row convert(Row current) {
  if (reuse == null) {
    return Row.copy(current);
  } else {
    Preconditions.checkArgument(current.getArity() == reuse.getArity());
    for (int i = 0; i < current.getArity(); ++i) {
      reuse.setField(i, current.getField(i));
    }
    return reuse;
  }
}

相关文章