【Spark学习笔记】4、Java版-算子系列之flatMap(f:T => Seq[U]) : RDD[T] => RDD[U]

本文深入探讨了Spark中flatMap操作符的实现原理与应用。通过源码分析,解释了flatMap如何将输入数据集的每个元素转换为多个输出元素,并通过示例展示了其在数据处理中的灵活性与强大功能。

flatMap源码

/**
 *  Return a new RDD by first applying a function to all elements of this
 *  RDD, and then flattening the results.
 */
def flatMap[U: ClassTag](f: T => TraversableOnce[U]): RDD[U] = withScope {
  val cleanF = sc.clean(f)
  new MapPartitionsRDD[U, T](this, (context, pid, iter) => iter.flatMap(cleanF))
}

demo

public class FlatMapOperator {

	public static void main(String[] args) {
		SparkConf conf = new SparkConf().setAppName("FlatMapOperator").setMaster("local");
		JavaSparkContext sc = new JavaSparkContext(conf);

		/**
		 * Similar to map, but each input item can be mapped to 0 or more output items (so func should return a Seq rather than a single item).
		 * 
		 */
		List<String> lineList = Arrays.asList("hello hadoop", "hello hdfs", "hello mapreduce", "hello spark");
		JavaRDD<String> lines = sc.parallelize(lineList);

		JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Iterable<String> call(String line) throws Exception {
				return Arrays.asList(line.split(" "));
			}
		});
		words.foreach(new VoidFunction<String>() {
			private static final long serialVersionUID = 1L;

			@Override
			public void call(String word) throws Exception {
				System.out.println(word);
			}
		});

		sc.close();
	}

}

输出

hello
hadoop
hello
hdfs
hello
mapreduce
hello
spark

 

请修改下面代码,使数据在一行中显示, @cancel="handleAddQtimeCancel"> <!-- Add Qtime 表单内容 --> <a-form > <a-row :gutter="16"> <a-col :span="12"> <a-form-item label="QTime Type"> <a-select v-decorators="['controlType', { rules: [{ required: true, message: '请选择类型!' }] }]"> <a-select-option value="Max Time">Max Time</a-select-option> <a-select-option value="Min Time">Min Time</a-select-option> </a-select> </a-form-item> </a-col> <a-col :span="12"> <a-form-item label="QTime Period"> <a-input-number v-decorators="['intervalTime', { rules: [{ required: true, message: '请输入时间段!' }] }]" :style="{ width: '100%' }" placeholder="请输入时间段" /> </a-form-item> </a-col> </a-row> <a-row :gutter="16"> <a-col :span="12"> <a-form-item label="QTime From Seq"> <a-select v-decorators="['strStepSeq', { rules: [{ required: true, message: '请选择起始步骤!' }] }]"> <a-select-option v-for="seq in seqs" :key="seq" :value="seq">{{ seq }}</a-select-option> </a-select> </a-form-item> </a-col> <a-col :span="12"> <a-form-item label="QTime To Seq"> <a-select v-decorators="['endStepSeq', { rules: [{ required: true, message: '请选择结束步骤!' }] }]"> <a-select-option v-for="seq in seqs" :key="seq" :value="seq">{{ seq }}</a-select-option> </a-select> </a-form-item> </a-col> </a-row> <a-row :gutter="16"> <a-col :span="12"> <a-form-item label="QTime Expire Action"> <a-select v-decorators="['expireAction', { rules: [{ required: true, message: '请选择过期动作!' }] }]"> <a-select-option value="Hold Lot">Hold Lot</a-select-option> <a-select-option value="Show Message">Show Message</a-select-option> </a-select> </a-form-item> </a-col> </a-row> </a-form> </a-modal>
最新发布
12-07
要使表单的所有字段在一行中显示,可以将所有字段放在一个 `<a-row>` 中,并调整每个 `<a-col>` 的 `:span` 值,使它们能够水平排列。Ant Design Vue 的栅格系统中,一行的总 `span` 为 24,因此可以根据字段数量合理分配 `span` 值。 以下是修改后的代码,使所有字段在一行中显示: ```vue <a-modal :visible="addQtimeVisible" title="Add Qtime" @ok="handleAddQtimeOk" @cancel="handleAddQtimeCancel" > <a-form :form="form"> <a-row :gutter="16"> <a-col :span="4"> <a-form-item label="QTime Type"> <a-select v-decorators="[ 'controlType', { rules: [{ required: true, message: '请选择类型!' }] } ]" > <a-select-option value="Max Time">Max Time</a-select-option> <a-select-option value="Min Time">Min Time</a-select-option> </a-select> </a-form-item> </a-col> <a-col :span="4"> <a-form-item label="QTime Period"> <a-input-number v-decorators="[ 'intervalTime', { rules: [{ required: true, message: '请输入时间段!' }] } ]" style="width: 100%" placeholder="时间段" /> </a-form-item> </a-col> <a-col :span="4"> <a-form-item label="QTime From Seq"> <a-select v-decorators="[ 'strStepSeq', { rules: [{ required: true, message: '请选择起始步骤!' }] } ]" > <a-select-option v-for="seq in seqs" :key="seq" :value="seq">{{ seq }}</a-select-option> </a-select> </a-form-item> </a-col> <a-col :span="4"> <a-form-item label="QTime To Seq"> <a-select v-decorators="[ 'endStepSeq', { rules: [{ required: true, message: '请选择结束步骤!' }] } ]" > <a-select-option v-for="seq in seqs" :key="seq" :value="seq">{{ seq }}</a-select-option> </a-select> </a-form-item> </a-col> <a-col :span="4"> <a-form-item label="QTime Expire Action"> <a-select v-decorators="[ 'expireAction', { rules: [{ required: true, message: '请选择过期动作!' }] } ]" > <a-select-option value="Hold Lot">Hold Lot</a-select-option> <a-select-option value="Show Message">Show Message</a-select-option> </a-select> </a-form-item> </a-col> <a-col :span="4"> <a-form-item label=" "> <a-button type="primary" html-type="submit" block>添加</a-button> </a-form-item> </a-col> </a-row> </a-form> </a-modal> ``` --- ### 说明: - 使用 `a-row` 和 `a-col` 布局,所有字段都在同一行显示。 - 每个字段占 `:span="4"`,总共 6 个字段,刚好占满 24 栅格。 - 最后一个字段放置“添加”按钮,用于提交表单。 - 表单布局更紧凑,适合屏幕宽度较大的设备。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值