【问题标题】:using spark to read file from hdfs使用 spark 从 hdfs 读取文件
【发布时间】:2018-08-10 12:24:37
【问题描述】:

我有下面的代码,它从我的本地驱动器中读取文件,但是我希望它从 hdfs 上的文件夹中读取文件。我想使用 sc.textfile 方法,但是我在正确初始化它时有点卡住了。你能帮忙吗?

session = SparkSession.builder.appName('myapp')
session = session.master ('local').getOrCreate()
sql_context = SQLContext(session.SparkContext)
sql_context.sql("SET spark.sql.autoBroadcastJoinThreshold=-1")
cwd=os.getcwd()

names=session.read.csv(os.path.join (cwd, 'local/path/to/file'),    header=True, inferSchema=True).repartition(nameid)
classes=session.read.csv(os.path.join (cwd, 'local/path/to/file'),    header=True, inferSchema=True).repartition(classid)

【问题讨论】:

    标签: python pyspark hdfs


    【解决方案1】:

    为什么不继续使用 session/spark?

    df_load = sparkSession.read.csv('hdfs://cluster/user/hdfs/test/*.csv')
    

    【讨论】:

    • 如果我这样做,那么我是否替换我的代码 names=session.read.csv(os.path.join (cwd, 'local/path/to/file'), header=True, inferSchema任何东西
    • 是的,你可以适应和做正则表达式模式匹配,不同的目录。我使用 spark 但你有会话变量,很酷。
    • 这有帮助吗?
    猜你喜欢
    • 2015-02-13
    • 1970-01-01
    • 2021-06-18
    • 2021-06-25
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    相关资源
    最近更新 更多