spark读取csv转化为rdd(python+scala+java三种代码汇总)

--------------------------------------------------------------------基本信息----------------------------------------------------------

编程语言运行方式sc.textFile默认路径
Pythonpysparkhdfs://
Scalaspark-shellhdfs://
JavaIntellijfile://

这里的默认路径的意思是说:

如果你只写了sc.textFile("/xxx")

那么就会默认是本地路径下面的根目录或者是hdfs下面的根目录。

所以最好是都统一写成sc.textFile("/hdfs://xxx")

------------------------------------------------------------------------------------Pyspark[1]--------------------------------------------------------------

>>> import pandas as pd
>>> from pyspark.sql import SparkSession
>>> from pyspark import SparkContext
>>> from pyspark.sql import SQLContext
>>> from pyspark.sql.types import *
>>> lines = sc.textFile("/rdd1.csv")
>>> header = lines.first()
>>> lines = lines.filter(lambda row:row != header)                              
>>> lines
PythonRDD[13] at RDD at PythonRDD.scala:53
>>> lines.collect()
['002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '002,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello', '003,hello']

>>> exit()

这里注意哈,这里的路径仅仅适用于local模式,如果是集群模式,必须是传递到hdfs上去。[2]

------------------------------------------------------Scala----------------------------------------------------------------------------------------------

scala> val lines = sc.textFile("/rdd1.csv")
lines: org.apache.spark.rdd.RDD[String] = /rdd1.csv MapPartitionsRDD[1] at textFile at <console>:24

scala> var header = lines.first()
header: String = 001,hello                                                      

scala> val lines2=lines.filter(_!=header)
lines2: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[2] at filter at <console>:27

scala> lines2.collect()
res0: Array[String] = Array(002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 002,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,hello, 003,he...

scala> :q

------------------------------------------------------Java----------------------------------------------------------------------------------------

src/main/java/javaread.java如下:

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.*;
import scala.Tuple2;
import java.util.*;
import org.apache.spark.SparkConf;

import javax.swing.*;
import java.util.Iterator;
import java.util.Random;

public class javaread {


    public static void main(String[]  args)
    {
        SparkConf conf = new SparkConf().setMaster("spark://Desktop:7077").setJars(new String[]{"/home/appleyuchi/桌面/spark_success/Spark数据倾斜处理/Java/sampling_salting/target/sampling-salting-1.0-SNAPSHOT.jar"}).setAppName("TestSpark");
        JavaSparkContext sc = new JavaSparkContext(conf);
        sc.setLogLevel("WARN");
        final JavaRDD<String>lines = sc.textFile("hdfs://Desktop:9000/rdd1.csv");


        final String header= lines.first();
        JavaRDD<String> lines2 = (JavaRDD<String>) lines.filter(
                new Function<String, Boolean>()
                {
                    private static final long serialVersionUID = 1L;
                    @Override
            public Boolean call(String v1) throws Exception
                    {
                return !v1.equals(header);
            }
        });

//        System.out.println(lines2.foreach(print));
        System.out.println(lines2.collect());

    }



}

pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sampling-salting</groupId>
    <artifactId>sampling-salting</artifactId>
    <version>1.0-SNAPSHOT</version>




    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.12</artifactId>
            <version>3.0.0</version>
        </dependency>

    </dependencies>



</project>

 

上述java代码是连接的真实集群,不是local模式,所以运行步骤和local模式不太一样:

①mvn package

②Ctrl+Shift+F10

--------------------------------------------------------------------------------------------------------------------------------------------------------

Reference:

[1]pyspark学习系列(二)读取CSV文件 为RDD或者DataFrame进行数据处理

[2]spark集群模式下textFile读取file本地文件报错解决

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java实现: ```java import org.apache.spark.SparkConf; import org.apache.spark.api.java.JavaRDD; import org.apache.spark.api.java.JavaSparkContext; import scala.Tuple2; import java.util.ArrayList; import java.util.List; public class GlomExample { public static void main(String[] args) { SparkConf conf = new SparkConf().setAppName("GlomExample").setMaster("local"); JavaSparkContext sc = new JavaSparkContext(conf); List<Integer> data = new ArrayList<>(); for (int i = 1; i <= 10; i++) { data.add(i); } JavaRDD<Integer> rdd = sc.parallelize(data, 2); JavaRDD<List<Integer>> glomRdd = rdd.glom(); List<List<Integer>> result = glomRdd.collect(); for (int i = 0; i < result.size(); i++) { System.out.println("Partition " + i + ": " + result.get(i)); } sc.stop(); } } ``` Scala实现: ```scala import org.apache.spark.{SparkConf, SparkContext} object GlomExample { def main(args: Array[String]): Unit = { val conf = new SparkConf().setAppName("GlomExample").setMaster("local") val sc = new SparkContext(conf) val data = 1 to 10 val rdd = sc.parallelize(data, 2) val glomRdd = rdd.glom() val result = glomRdd.collect() for (i <- result.indices) { println(s"Partition $i: ${result(i).toList}") } sc.stop() } } ``` Python实现: ```python from pyspark import SparkConf, SparkContext conf = SparkConf().setAppName("GlomExample").setMaster("local") sc = SparkContext(conf=conf) data = range(1, 11) rdd = sc.parallelize(data, 2) glom_rdd = rdd.glom() result = glom_rdd.collect() for i in range(len(result)): print(f"Partition {i}: {list(result[i])}") sc.stop() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值