You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-user@hadoop.apache.org by 贺齐 <fo...@gmail.com> on 2007/09/24 06:31:56 UTC
How to read and write the HDFS in java?
Hi,
Are there any examples using HDFS in java program. My requirement
is simple: read and writer on the HDFS.
Thank you.
Regards
HeQi
Re: How to read and write the HDFS in java?
Posted by 贺齐 <fo...@gmail.com>.
Hi Ion,
Thanks!
2007/9/26, Ion Badita <io...@mcr.ro>:
> I don't know if this is useful... but you could try to use this small
> example:
>
>
> import org.apache.hadoop.fs.FileSystem;
> import org.apache.hadoop.fs.Path;
> import org.apache.hadoop.fs.FSDataOutputStream;
> import org.apache.hadoop.fs.FSDataInputStream;
> import org.apache.hadoop.conf.Configuration;
>
> import java.io.IOException;
>
> public class ReadWrite {
> public static void main(String[] args) throws IOException {
> FileSystem hdfs = FileSystem.get(new Configuration());
>
> Path path = new Path("/testfile");
>
> //writing
> FSDataOutputStream dos = hdfs.create(path);
> dos.writeUTF("Hello World");
> dos.close();
>
> //reading
> FSDataInputStream dis = hdfs.open(path);
> System.out.println(dis.readUTF());
> dis.close();
>
> hdfs.close();
> }
> }
>
>
>
> I'm assuming that you have in your classpath the hadoop-site.xml file
> correctly configured, meaning that the property "fs.default.name" has a
> valid name node for your cluster, or you could use FileSystem.getLocal()
> for local file system.
>
> John
>
>
> > Hi,
> > Are there any examples using HDFS in java program. My requirement
> > is simple: read and writer on the HDFS.
> > Thank you.
> >
> >
> >
> >
> >
> >
> >
> > Regards
> > HeQi
> >
>
>
Re: How to read and write the HDFS in java?
Posted by Ion Badita <io...@mcr.ro>.
I don't know if this is useful... but you could try to use this small
example:
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.conf.Configuration;
import java.io.IOException;
public class ReadWrite {
public static void main(String[] args) throws IOException {
FileSystem hdfs = FileSystem.get(new Configuration());
Path path = new Path("/testfile");
//writing
FSDataOutputStream dos = hdfs.create(path);
dos.writeUTF("Hello World");
dos.close();
//reading
FSDataInputStream dis = hdfs.open(path);
System.out.println(dis.readUTF());
dis.close();
hdfs.close();
}
}
I'm assuming that you have in your classpath the hadoop-site.xml file
correctly configured, meaning that the property "fs.default.name" has a
valid name node for your cluster, or you could use FileSystem.getLocal()
for local file system.
John
> Hi,
> Are there any examples using HDFS in java program. My requirement
> is simple: read and writer on the HDFS.
> Thank you.
>
>
>
>
>
>
>
> Regards
> HeQi
>