Hadoop Streaming cacheFile 和cacheArchive选项

Hadoop Streaming中的大文件和档案

任务使用-cacheFile和-cacheArchive选项在集群中分发文件和档案,选项的参数是用户已上传至HDFS的文件或档案的URI。这些文件和档案在不同的作业间缓存。用户可以通过fs.default.name.config配置参数的值得到文件所在的host和fs_port。

这个是使用-cacheFile选项的例子:

-cacheFile hdfs://host:fs_port/user/testfile.txt#testlink

在上面的例子里,url中#后面的部分是建立在任务当前工作目录下的符号链接的名字。这里的任务的当前工作目录下有一个“testlink”符号链接,它指向testfile.txt文件在本地的拷贝。如果有多个文件,选项可以写成:

-cacheFile hdfs://host:fs_port/user/testfile1.txt#testlink1 -cacheFile hdfs://host:fs_port/user/testfile2.txt#testlink2

-cacheArchive选项用于把jar文件拷贝到任务当前工作目录并自动把jar文件解压缩。例如:

-cacheArchive hdfs://host:fs_port/user/testfile.jar#testlink3

在上面的例子中,testlink3是当前工作目录下的符号链接,它指向testfile.jar解压后的目录。

下面是使用-cacheArchive选项的另一个例子。其中,input.txt文件有两行内容,分别是两个文件的名字:testlink/cache.txt和testlink/cache2.txt。“testlink”是指向档案目录(jar文件解压后的目录)的符号链接,这个目录下有“cache.txt”和“cache2.txt”两个文件。

$HADOOP_HOME/bin/hadoop  jar $HADOOP_HOME/hadoop-streaming.jar /
                  -input "/user/me/samples/cachefile/input.txt"  /
                  -mapper "xargs cat"  /
                  -reducer "cat"  /
                  -output "/user/me/samples/cachefile/out" /  
                  -cacheArchive 'hdfs://hadoop-nn1.example.com/user/me/samples/cachefile/cachedir.jar#testlink' /  
                  -jobconf mapred.map.tasks=1 /
                  -jobconf mapred.reduce.tasks=1 / 
                  -jobconf mapred.job.name="Experiment"
$ ls test_jar/
cache.txt  cache2.txt
$ jar cvf cachedir.jar -C test_jar/ .
added manifest
adding: cache.txt(in = 30) (out= 29)(deflated 3%)
adding: cache2.txt(in = 37) (out= 35)(deflated 5%)
$ hadoop dfs -put cachedir.jar samples/cachefile
$ hadoop dfs -cat /user/me/samples/cachefile/input.txt
testlink/cache.txt
testlink/cache2.txt
$ cat test_jar/cache.txt 
This is just the cache string
$ cat test_jar/cache2.txt 
This is just the second cache string
$ hadoop dfs -ls /user/me/samples/cachefile/out      
Found 1 items
/user/me/samples/cachefile/out/part-00000     69
$ hadoop dfs -cat /user/me/samples/cachefile/out/part-00000
This is just the cache string   
This is just the second cache string
在不同的技术场景中,“streaming”有不同的含义使用方法: ### Hadoop Streaming Hadoop Streaming是一种使用非Java语言编写的MapReduce程序的机制。 - **简介**:它允许用户使用任何可执行文件或脚本作为MapReduce任务,使得用户可以用Python、Ruby等语言编写MapReduce作业,扩展了Hadoop的编程能力[^1]。 - **优点**:支持多种编程语言,方便不同技术栈的开发者使用Hadoop进行大数据处理。 - **不足**:相对Java编写的MapReduce程序,性能可能较低。 - **使用方法**:在执行Hadoop Streaming作业时,需要指定一些参数。例如,`-file` 用于将本地文件复制到Hadoop集群的每个节点上;`-cacheFile` 用于将文件缓存到节点上;`-cacheArchive` 用于将归档文件(如.tar.gz)缓存到节点上。还可以进行压缩处理全局排序等操作。以下是一个简单的使用示例(假设使用Python脚本作为MapperReducer): ```bash hadoop jar $HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-*.jar \ -input input_data \ -output output_data \ -mapper "python mapper.py" \ -reducer "python reducer.py" \ -file mapper.py \ -file reducer.py ``` ### Spark Streaming Spark Streaming用于流式数据的处理,是Spark生态系统中的一个组件。 - **简介**:支持多种数据输入源,如Kafka、Flume、Twitter、ZeroMQ简单的TCP套接字等。数据输入后可以使用Spark的高度抽象算子(如map、reduce、join、window等)进行运算,结果可以保存到HDFS、数据库等地方[^2]。 - **使用方法**:以下是一个简单的使用Python编写的Spark Streaming处理TCP套接字输入数据的示例: ```python from pyspark import SparkContext from pyspark.streaming import StreamingContext # 创建SparkContextStreamingContext sc = SparkContext("local[2]", "NetworkWordCount") ssc = StreamingContext(sc, 1) # 创建DStream,连接到本地的9999端口 lines = ssc.socketTextStream("localhost", 9999) # 对每行数据进行分割并计数 words = lines.flatMap(lambda line: line.split(" ")) pairs = words.map(lambda word: (word, 1)) wordCounts = pairs.reduceByKey(lambda x, y: x + y) # 打印结果 wordCounts.pprint() # 启动流计算 ssc.start() # 等待计算结束 ssc.awaitTermination() ``` ### Spark Structured Streaming Structured Streaming是一个基于Spark SQL引擎的可扩展容错流处理引擎。 - **简介**:它把持续不断的流式数据当做一个不断追加的表,使得新的流处理模型与批处理模型非常相似。用户可以将流计算表示为在静态表上的标准批处理查询,Spark将其作为无界输入表上的增量查询运行[^3]。 - **使用方法**:使用时需要指定输入数据源、输出模式、sink输出结果等。还可以使用时间窗口时间水印进行更复杂的流处理。以下是一个简单的使用示例: ```python from pyspark.sql import SparkSession from pyspark.sql.functions import explode from pyspark.sql.functions import split # 创建SparkSession spark = SparkSession.builder.appName("StructuredNetworkWordCount").getOrCreate() # 创建流DataFrame lines = spark.readStream.format("socket").option("host", "localhost").option("port", 9999).load() # 对每行数据进行分割并计数 words = lines.select(explode(split(lines.value, " ")).alias("word")) wordCounts = words.groupBy("word").count() # 启动流查询 query = wordCounts.writeStream.outputMode("complete").format("console").start() query.awaitTermination() ``` ### Unity Render Streaming Unity Render Streaming用于实现Unity项目的实时流传输,基于WebRTC技术。 - **简介**:可以将Unity场景的渲染结果实时传输到网页或其他客户端。 - **使用方法**:在github下载相应版本的库(GitHub - Unity - Technologies/UnityRenderStreaming: Streaming server for Unity 3.2),然后从模板创建Unity项目进行基础搭建使用[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值