org.apache.flink.api.common.typeinfo.Types.TUPLE()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(157)

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

Types.TUPLE介绍

[英]Returns type information for typed subclasses of Flink's org.apache.flink.api.java.tuple.Tuple. Typed subclassed are classes that extend org.apache.flink.api.java.tuple.Tuple0 till org.apache.flink.api.java.tuple.Tuple25 to provide types for all fields and might add additional getters and setters for better readability. Additional member fields must not be added. A tuple must not be null.

A tuple is a fixed-length composite type for storing multiple values in a deterministic field order. Fields of a tuple are typed. Tuples are the most efficient composite type; a tuple does not support null-valued fields unless the type of the field supports nullability.

The generic types for all fields of the tuple can be defined in a hierarchy of subclasses.

If Flink's type analyzer is unable to extract a tuple type information with type information for all fields, an org.apache.flink.api.common.functions.InvalidTypesExceptionis thrown.

Example use:

class MyTuple extends Tuple2 public int getId() { return f0; } 
public String getName() { return f1; } 
} 
} 
Types.TUPLE(MyTuple.class)

[中]返回Flink组织的类型化子类的类型信息。阿帕奇。弗林克。应用程序编程接口。JAVA元组。元组。类型化子类是扩展org的类。阿帕奇。弗林克。应用程序编程接口。JAVA元组。Tuple0组织。阿帕奇。弗林克。应用程序编程接口。JAVA元组。Tuple25为所有字段提供类型,并可能添加额外的getter和setter以提高可读性。不得添加其他成员字段。元组不能为null。
元组是一种固定长度的复合类型,用于以确定性字段顺序存储多个值。元组的字段是键入的。元组是最有效的复合类型;元组不支持空值字段,除非字段类型支持空值性。
元组的所有字段的泛型类型都可以在子类的层次结构中定义。
如果Flink的类型分析器无法提取具有所有字段类型信息的元组类型信息,则会生成一个组织。阿帕奇。弗林克。应用程序编程接口。常见的功能。InvalidTypesCeptionis被抛出。
示例用法:

class MyTuple extends Tuple2 public int getId() { return f0; } 
public String getName() { return f1; } 
} 
} 
Types.TUPLE(MyTuple.class)

代码示例

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

@Override
public TypeInformation<Tuple> getProducedType() {
  return Types.TUPLE(Types.LONG, Types.STRING);
}

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

@Override
public TypeInformation<Tuple2<Boolean, Row>> getOutputType() {
  return Types.TUPLE(Types.BOOLEAN, getRecordType());
}

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

public CollectStreamResult(TypeInformation<Row> outputType, ExecutionConfig config,
    InetAddress gatewayAddress, int gatewayPort) {
  this.outputType = outputType;
  resultLock = new Object();
  // create socket stream iterator
  final TypeInformation<Tuple2<Boolean, Row>> socketType = Types.TUPLE(Types.BOOLEAN, outputType);
  final TypeSerializer<Tuple2<Boolean, Row>> serializer = socketType.createSerializer(config);
  try {
    // pass gateway port and address such that iterator knows where to bind to
    iterator = new SocketStreamIterator<>(gatewayPort, gatewayAddress, serializer);
  } catch (IOException e) {
    throw new SqlClientException("Could not start socket for result retrieval.", e);
  }
  // create table sink
  // pass binding address and port such that sink knows where to send to
  collectTableSink = new CollectStreamTableSink(iterator.getBindAddress(), iterator.getPort(), serializer);
  retrievalThread = new ResultRetrievalThread();
  monitoringThread = new JobMonitoringThread();
}

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

@Test
public void testWithKryoGenericSer() throws Exception {
  final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
  env.getConfig().enableForceKryo();
  Path in = new Path(inFile.getAbsoluteFile().toURI());
  AvroInputFormat<User> users = new AvroInputFormat<>(in, User.class);
  DataSet<User> usersDS = env.createInput(users);
  DataSet<Tuple2<String, Integer>> res = usersDS
    .groupBy((KeySelector<User, String>) value -> String.valueOf(value.getName()))
    .reduceGroup((GroupReduceFunction<User, Tuple2<String, Integer>>) (values, out) -> {
      for (User u : values) {
        out.collect(new Tuple2<>(u.getName().toString(), 1));
      }
    })
    .returns(Types.TUPLE(Types.STRING, Types.INT));
  res.writeAsText(resultPath);
  env.execute("Avro Key selection");
  expected = "(Charlie,1)\n(Alyssa,1)\n";
}

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

@Test
public void testWithAvroGenericSer() throws Exception {
  final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
  env.getConfig().enableForceAvro();
  Path in = new Path(inFile.getAbsoluteFile().toURI());
  AvroInputFormat<User> users = new AvroInputFormat<>(in, User.class);
  DataSet<User> usersDS = env.createInput(users);
  DataSet<Tuple2<String, Integer>> res = usersDS
    .groupBy((KeySelector<User, String>) value -> String.valueOf(value.getName()))
    .reduceGroup((GroupReduceFunction<User, Tuple2<String, Integer>>) (values, out) -> {
      for (User u : values) {
        out.collect(new Tuple2<>(u.getName().toString(), 1));
      }
    })
    .returns(Types.TUPLE(Types.STRING, Types.INT));
  res.writeAsText(resultPath);
  env.execute("Avro Key selection");
  expected = "(Charlie,1)\n(Alyssa,1)\n";
}

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

.returns(Types.TUPLE(Types.INT, Types.DOUBLE)); // for lambda with generics

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

@Test
public void testKeySelection() throws Exception {
  final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
  env.getConfig().enableObjectReuse();
  Path in = new Path(inFile.getAbsoluteFile().toURI());
  AvroInputFormat<User> users = new AvroInputFormat<>(in, User.class);
  DataSet<User> usersDS = env.createInput(users);
  DataSet<Tuple2<String, Integer>> res = usersDS
    .groupBy("name")
    .reduceGroup((GroupReduceFunction<User, Tuple2<String, Integer>>) (values, out) -> {
      for (User u : values) {
        out.collect(new Tuple2<>(u.getName().toString(), 1));
      }
    })
    .returns(Types.TUPLE(Types.STRING, Types.INT));
  res.writeAsText(resultPath);
  env.execute("Avro Key selection");
  expected = "(Alyssa,1)\n(Charlie,1)\n";
}

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

.returns(Types.TUPLE(Types.STRING, Types.LONG, Types.INT, Types.STRING))
    .returns(Types.TUPLE(Types.STRING, Types.INT))
    .returns(Types.TUPLE(Types.STRING, Types.INT));
    .returns(Types.TUPLE(Types.STRING, Types.INT));
DataSet<Tuple2<String, Integer>> iteration = initial
  .map(x -> Tuple2.of(x.f0, x.f1 * 2))
    .returns(Types.TUPLE(Types.STRING, Types.INT));
DataSet<Boolean> termination = iteration
    .returns(Types.TUPLE(Types.INT, Types.INT))
    .returns(Types.TUPLE(Types.INT, Types.INT));

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

private static DataSet<String> analyze(DataSet<String> input, DataSet<String> stats, int branches) {
    for (int i = 0; i < branches; i++) {
      final int ii = i;

      if (stats != null) {
        input = input.map(
          new RichMapFunction<String, String>() {
            @Override
            public String map(String value) {
              return value;
            }
        }).withBroadcastSet(stats.map(s -> "(" + s + ").map"), "stats");
      }

      DataSet<String> branch = input
        .map(s -> new Tuple2<>(0, s + ii)).returns(Types.TUPLE(Types.STRING, Types.INT))
        .groupBy(0)
        .minBy(1)
        .map(kv -> kv.f1).returns(Types.STRING);
      if (stats == null) {
        stats = branch;
      } else {
        stats = stats.union(branch);
      }
    }
    return stats.map(s -> "(" + s + ").stats");
  }
}

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

public static <T> TypeInformation<Tuple2<String, T>> getStreamTupleTypeInformation(TypeInformation<T> typeInformation) {
    return Types.TUPLE(Types.STRING, typeInformation);
  }
}

代码示例来源:origin: org.apache.flink/flink-connector-elasticsearch-base_2.11

@Override
public TypeInformation<Tuple2<Boolean, Row>> getOutputType() {
  return Types.TUPLE(Types.BOOLEAN, getRecordType());
}

代码示例来源:origin: haoch/flink-siddhi

public static <T> TypeInformation<Tuple2<StreamRoute, T>> getStreamTupleTypeInformation(TypeInformation<T> typeInformation) {
    return Types.TUPLE(TypeInformation.of(StreamRoute.class), typeInformation);
  }
}

代码示例来源:origin: haoch/flink-siddhi

public static <T extends Tuple> TypeInformation<T> getTupleTypeInformation(AbstractDefinition definition) {
  List<TypeInformation> types = new ArrayList<>();
  for (Attribute attribute : definition.getAttributeList()) {
    types.add(TypeInformation.of(getJavaType(attribute.getType())));
  }
  try {
    return Types.TUPLE(types.toArray(new TypeInformation[0]));
  } catch (IllegalArgumentException ex) {
    throw new IllegalArgumentException("Unable to parse ", ex);
  }
}

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

public static <T extends Tuple> TypeInformation<T> getTupleTypeInformation(AbstractDefinition definition) {
  int tupleSize = definition.getAttributeList().size();
  TypeInformation[] typeInformations = new TypeInformation[tupleSize];
  List<Attribute> attributes = definition.getAttributeList();
  try {
    for (int i = 0; i < attributes.size() ; i++) {
      Class<?> clazz = getJavaType(attributes.get(i).getType());
      typeInformations[i] = TypeInformation.of(clazz);
    }
    return Types.TUPLE(typeInformations);
  } catch (IllegalArgumentException ex) {
    throw new IllegalArgumentException("Failed to get Type Information.", ex);
  }
}

代码示例来源:origin: haoch/flink-siddhi

@SuppressWarnings("unchecked")
  public static <OUT> DataStream<OUT> createDataStream(SiddhiOperatorContext context, DataStream<Tuple2<StreamRoute, Object>> namedStream) {
    return namedStream.transform(
      context.getName(),
      Types.TUPLE(TypeInformation.of(String.class), TypeInformation.of(Object.class)),
      new SiddhiStreamOperator(context));
  }
}

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

@Test
public void testTypeInfoParser() {
  TypeInformation<Tuple3<String, Long, Object>> type1 =
      Types.TUPLE(Types.STRING, Types.LONG, Types.GENERIC(Object.class));
  Assert.assertNotNull(type1);
  TypeInformation<Tuple4<String, Long, Object, InnerPojo>> type2 =
      Types.TUPLE(Types.STRING, Types.LONG, Types.GENERIC(Object.class), Types.GENERIC(InnerPojo.class));
  Assert.assertNotNull(type2);
}

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

@Test
public void testStreamTupleSerializerWithTuple() {
  TypeInformation<Tuple4> typeInfo = Types.GENERIC(Tuple4.class);
  StreamSchema<Tuple4> schema = new StreamSchema<>(typeInfo, "id", "timestamp", "name", "price");
  assertEquals(Tuple4.class, schema.getTypeInfo().getTypeClass());
  TypeInformation<Tuple2<String, Tuple4>> tuple2TypeInformation = Types.TUPLE(Types.STRING, schema.getTypeInfo());
  assertEquals("Java Tuple2<String, GenericType<" + Tuple4.class.getName() + ">>", tuple2TypeInformation.toString());
}

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

@Test
public void testStreamTupleSerializerWithPojo() {
  TypeInformation<Event> typeInfo = TypeInformation.of(Event.class);
  assertTrue("Type information should be PojoTypeInfo", typeInfo instanceof PojoTypeInfo);
  StreamSchema<Event> schema = new StreamSchema<>(typeInfo, "id", "timestamp", "name", "price");
  assertEquals(Event.class, schema.getTypeInfo().getTypeClass());
  TypeInformation<Tuple2<String, Event>> tuple2TypeInformation = Types.TUPLE(Types.STRING, Types.GENERIC(schema.getTypeInfo().getTypeClass()));
  assertEquals("Java Tuple2<String, GenericType<" + Event.class.getName() + ">>", tuple2TypeInformation.toString());
}

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

@Test
  public void testStreamTupleSerializerWithPrimitive() {
    TypeInformation<String> typeInfo = Types.STRING;
    StreamSchema<String> schema = new StreamSchema<>(typeInfo, "words");
    assertEquals(String.class, schema.getTypeInfo().getTypeClass());
    TypeInformation<Tuple2<String, String>> tuple2TypeInformation = Types.TUPLE(Types.STRING, schema.getTypeInfo());
    assertEquals("Java Tuple2<String, String>", tuple2TypeInformation.toString());
  }
}

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

@Test
public void testStreamSchemaWithTuple() {
  TypeInformation<Tuple4> typeInfo = Types.TUPLE(Types.INT, Types.LONG, Types.STRING, Types.DOUBLE);
  StreamSchema<Tuple4> schema = new StreamSchema<>(typeInfo, "id", "timestamp", "name", "price");
  assertEquals(Tuple4.class, schema.getTypeInfo().getTypeClass());
  assertEquals(4, schema.getFieldIndexes().length);
  assertEquals(Tuple4.class, schema.getTypeInfo().getTypeClass());
}

相关文章