Database Reference
In-Depth Information
user
)
throws
IOException
A
Configuration
object encapsulates a client or server's configuration, which is set
using configuration files read from the classpath, such as
etc/hadoop/core-site.xml
. The
first method returns the default filesystem (as specified in
core-site.xml
, or the default loc-
al filesystem if not specified there). The second uses the given
URI
's scheme and author-
ity to determine the filesystem to use, falling back to the default filesystem if no scheme is
specified in the given
URI
. The third retrieves the filesystem as the given user, which is
In some cases, you may want to retrieve a local filesystem instance. For this, you can use
the convenience method
getLocal()
:
public static
LocalFileSystem
getLocal
(
Configuration conf
)
throws
IOException
With a
FileSystem
instance in hand, we invoke an
open()
method to get the input
stream for a file:
public
FSDataInputStream
open
(
Path f
)
throws
IOException
public abstract
FSDataInputStream
open
(
Path f
,
int
bufferSize
)
throws
IOException
The first method uses a default buffer size of 4 KB.
Putting this together, we can rewrite
Example 3-1
as shown in
Example 3-2
.
Example 3-2. Displaying files from a Hadoop filesystem on standard output by using the
FileSystem directly
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
);
}
}
}