Para interactuar con HDFS con Java, se hace a través de la clase FileSystem.
El desarrollo es en Java y los ejecutamos desde línea de comando tras haber arrancado los demonios Hadoop (/bin/start-all.sh).
Lo primero que he hecho es crear una carpeta pruebas en mi sistema de ficheros HDFS:
hadoop fs -mkdir pruebas
Algunos ejemplos que también podéis descargar en este enlace son:
- Copiar un fichero o carpeta del sistema de ficheros local a HDFS
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class LocalToHdfs { public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); FileSystem hdfs = FileSystem.get(conf); Path sourcePath = new Path(args[0]); Path destPath = new Path(args[1]); hdfs.copyFromLocalFile(sourcePath, destPath); } }
Y este es el resultado en el que copio desde mi carpeta local training el fichero score.txt a mi carpeta pruebas en HDFS:
$ hadoop jar training/JavaHdfs.jar LocalToHdfs training/score.txt /user/elena/pruebas
$ hadoop fs -ls pruebas
Found 1 items
-rw-r--r-- 1 elena supergroup 363 2013-02-11 19:31 /user/elena/pruebas/score.txt
- Crear un fichero HDFS
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class CreateFileHdfs { public static void main(String[] args) throws Exception{ String contentFile = "Este es el contenido del fichero\n"; byte[] texto = contentFile.getBytes(); Configuration conf = new Configuration(); FileSystem hdfs = FileSystem.get(conf); Path path = new Path(args[0]+"/nombreFichero"); FSDataOutputStream outputStream = hdfs.create(path); outputStream.write(texto, 0, texto.length); } }
En el código Java tengo puesto el nombre del fichero y paso como argumento la carpeta donde se debe crear el fichero.
$ hadoop jar training/JavaHdfs.jar CreateFileHdfs pruebas
$ hadoop fs -ls pruebas
Found 2 items
-rw-r--r-- 1 elena supergroup 32 2013-02-11 19:34 /user/elena/pruebas/nombreFichero
-rw-r--r-- 1 elena supergroup 363 2013-02-11 19:31 /user/elena/pruebas/score.txt
$ hadoop fs -cat pruebas/nombreFichero
Este es el contenido del fichero
- Leer el contenido de un fichero HDFS
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; public class ReadFileHdfs { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); FileSystem hdfs = FileSystem.get(conf); Path path = new Path(args[0]+"/nombreFichero"); FSDataInputStream inputStream = hdfs.open(path); IOUtils.copyBytes(inputStream, System.out, conf, true); } }
$ hadoop jar training/JavaHdfs.jar ReadFileHdfs pruebas
Este es el contenido del fichero
- Borrar un fichero HDFS
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class DeleteFileHdfs { public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); FileSystem hdfs = FileSystem.get(conf); Path path = new Path(args[0]+"/nombreFichero"); hdfs.delete(path, false); } }
$ hadoop jar training/JavaHdfs.jar DeleteFileHdfs pruebas
$ hadoop fs -ls pruebas
Found 1 items
-rw-r--r-- 1 elena supergroup 363 2013-02-11 19:31 /user/elena/pruebas/score.txt
Muy interesante el blog Elena. Te seguiré porque me parecen interesantes estos temas y estoy también profundizando en ellos.
ResponderEliminarGracias