spark01-创建RDD

  1. parallelize方法
java 复制代码
# 参数分别为List<T>,numSlices:参数用于指定将数据集切分成多少个分区
# Spark 推荐的分区数通常是物理核心数的 2-4 倍。
JavaRDD<Integer> rdd1 = sc.parallelize(Arrays.asList(1,2,3,4,5),1)
# 相对于parallelize通用性,偏重于Double类型,如果数据为double类型,性能较快
JavaDoubleRDD rdd2 = sc.parallelizeDoubles(Arrays.asList(1.0,2.0,3.0),1)
# 生成键值对类型的RDD
JavaPairRDD<String,Integer> rdd3 = sc.parallelizePairs(Arrays.asList(new Tuple2<String,Integer>("tom",12),new Tuple2<String,Integer>("jack",13)))
# 实体类,实体类需要实现序列化
JavaRDD<User> rdd4 = sc.parallelize(Arrays.asList(new User("tom",12),new User("jack",13)))

# 注意,Java只有生成PairRDD键值对类型才能使用reduceByKey等<K,V>方法
JavaRDD<Tuple2<String,Integer>> rdd5 = sc.parallelize(Arrays.asList(new Tuple2<String,Integer>("tom",12),new Tuple2<String,Integer>("jack",13)))
# rdd5不是键值对类型,通过mapToPair转为JavaPariRdd<String,Integer>,虽然二者很像,但是不相同
JavaPairRDD<String,Integer> rdd6 = rdd5.mapToPair(x->new Tuple2(x._1(),x._2()))
# PairRDD这种以对象作为主键的,需要针对对象重写equals和hashCode方法,并且需要实现序列化类
JavaPairRDD<User,Double> rdd7 = sc.parallelizePari(Arrays.asList(new Tuple2<User,Double>(new User("tom",12),85),new Tuple2<User,Double>(new User("tom",12),90)))

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(id, employee.id) && Objects.equals(name, employee.name) && Objects.equals(age, employee.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age);
    }
python 复制代码
# python只提供了方法parallize(self,c,numSlices),python泛型机制
rdd1 = sc.parallelize([1,2,3,4,5],1)
rdd2 = sc.parallelize([1.0,2.0,3.0],2)
rdd3 = sc.parallelize([("tom",12),("jack",13)])
rdd4 = sc.parallelize([User("tom",12),User("jack",13)])
# rdd1虽然不是<K,V>型,但是依然提供了groupByKey方法,但是运行报错
# 注意,python也支持<K,V>其中K为实体,但是需要实现__eq__和__hash__方法
    def __eq__(self, other):
        if not isinstance(other,Employee):
            return False
        return self.name == other.name and self.age == other.age
    def __hash__(self):
        return hash((self.name, self.age))
  1. 通过读取文件创建RDD
java 复制代码
// 1. textFile(文件路径,最小分区数):如果实际分区小于最小分区数,按照最小分区数分区,否则落在最小分区数-理想分区数之间,如果想要强行修改分区,coalesce()是个不错方法,不涉及shuffle,但是容易造成数据倾斜
JavaRDD<String> rdd1 = sc.textFile(filePath,minPartitions)
// 2. wholeTextFiles-->返回(filename,content)
// 文件夹logs 01.txt
// 01.txt内容如下  
hello world
this is world
// 02.txt
this is world
hello world 
JavaPairRDD<String,String> rdd2 = sc.wholeTextFiles();
// 返回
01.txt hello world\nthis is world
02.txt this is world\nhello world
java 复制代码
# python版本
data = sc.textFile(path)
data1 = sc.wholeTextFiles(path)
相关推荐
右耳朵猫AI14 分钟前
Python周刊2026W38 | 标准流编码修复、PEP 845/846 草案、解析器提速 10%、集合字典二次复杂度
python·ai·数据科学
zx_7414848114 分钟前
【Linux入门】Shell 函数、正则表达式与文本处理:cut 与 awk
linux·运维·正则表达式
linux_cfan16 分钟前
17 · 引擎适配器全景:HLS/DASH/Vimeo/Mux/Cast
前端·javascript·音视频
微软技术分享18 分钟前
大模型网络结构:模型接口测试用例参考
python
Zenova EdgeOS26 分钟前
Linux dmesg 工业边缘实战:内核日志过滤、持久化与故障定位
linux·运维·边缘计算·工业边缘·dmesg·内核日志
金士顿37 分钟前
System.Text.Json Source Generator 深度解析:为什么 Native AOT 不喜欢反射
linux·asp.net core·arm64·net·native aot
计算机魔术师1 小时前
烧掉2780亿美元还不够?OpenAI的资本豪赌让人头皮发麻
前端
Bruce_Liuxiaowei1 小时前
Python 实例赋值遮蔽类属性:一个从不报错的静默陷阱
开发语言·python·语法糖
IT_陈寒2 小时前
Java线程池用错参数,我的服务居然悄悄崩溃了
前端·人工智能·后端
做运维的阿瑞2 小时前
Python 标准库汇总:分类速览与常用模块清单
linux·运维·python