Example 3-1. Displaying files from a Hadoop filesystem on standard output using a
URLStreamHandler
Java代码
//Reading Data from a Hadoop URL
public class URLCat {
static {
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}
public static void main(String[] args) throws Exception {
InputStream in = null ;
try {
in = new URL(args[ 0 ]).openStream();
IOUtils.copyBytes(in, System.out, 4096 , false );
} finally {
IOUtils.closeStream(in);
}
}
}
-----------------------------------------
result:
Here’s a sample run:
% hadoop URLCat hdfs://localhost/user/tom/quangle.txt
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.
Example 3-2. Displaying files from a Hadoop filesystem on standard output by using the FileSystem
directly
Java代码
public class FileSystemCat {
public static void main(String[] args) throws Exception {
String uri = args[0 ];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf);
InputStream in = null ;
try {
in = fs.open(new Path(uri));
IOUtils.copyBytes(in, System.out, 4096 , false );
} finally {
IOUtils.closeStream(in);
}
}
}
------------------------------------------
The program runs as follows:
% hadoop FileSystemCat hdfs://localhost/user/tom/quangle.txt
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.
The
Example 3-3 is a simple extension of Example 3-2 that writes a file to standard out
twice: after writing it once, it seeks to the start of the file and streams through it once
again.
Java代码
//Example 3-3. Displaying files from a Hadoop filesystem on standard output twice, by using seek
public class FileSystemDoubleCat {
public static void main(String[] args) throws Exception {
String uri = args[0 ];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf); //通过get()方法获得一个FileSystem流
FSDataInputStream in = null ;
try {
in = fs.open(new Path(uri)); //通过open()方法打开一个FSDataInputStream流
IOUtils.copyBytes(in, System.out, 4096 , false );
in.seek(0 ); // go back to the start of the file
IOUtils.copyBytes(in, System.out, 4096 , false );
} finally {
IOUtils.closeStream(in);
}
}
}
----------------------------------------------------
Here’s the result of running it on a small file:
% hadoop FileSystemDoubleCat hdfs://localhost/user/tom/quangle.txt
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.
Example 3-4 shows how to copy a local file to a Hadoop filesystem. We illustrate progress
by printing a period every time the progress() method is called by Hadoop, which
is after each 64 K packet of data is written to the datanode pipeline. (Note that this
particular behavior is not specified by the API, so it is subject to change in later versions
of Hadoop. The API merely allows you to infer that “something is happening.”)
Java代码
//Example 3-4. Copying a local file to a Hadoop filesystem, and shows progress
public class FileCopyWithProgress {
public static void main(String[] args) throws Exception {
String localSrc = args[0 ];
String dst = args[1 ];
InputStream in = new BufferedInputStream( new FileInputStream(localSrc));
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst), conf);
OutputStream out = fs.create(new Path(dst), new Progressable() {
public void progress() {
System.out.print("." );
}
});
IOUtils.copyBytes(in, out, 4096 , true );
}
}
Typical usage:
% hadoop FileCopyWithProgress input/docs/1400 - 8 .txt hdfs: //localhost/user/tom/1400-8.txt
...............
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com