Flink Double to Int Conversion: Simple Guide with Example
在 Flink 中,可以使用 toInt() 方法将 Double 类型转换为 Int 类型。以下是一个示例代码:
import org.apache.flink.api.common.functions.MapFunction;
public class DoubleToIntConverter implements MapFunction<Double, Integer> {
@Override
public Integer map(Double value) throws Exception {
return value.intValue();
}
}
然后,可以在 Flink 程序中使用 map() 函数将 Double 类型的数据转换为 Int 类型的数据:
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
public class FlinkExample {
public static void main(String[] args) throws Exception {
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
// 创建一个包含 Double 类型数据的数据源
DataSource<Double> input = env.fromElements(1.0, 2.0, 3.0, 4.0, 5.0);
// 使用 DoubleToIntConverter 进行类型转换
input.map(new DoubleToIntConverter()).print();
}
}
上述代码将输出转换后的 Int 类型数据:1, 2, 3, 4, 5。
原文地址: https://www.cveoy.top/t/topic/qbZ0 著作权归作者所有。请勿转载和采集!