比较好的网站开发教学网站,wordpress 七牛插件,百度问答怎么赚钱,贵阳网站建设哪家好环境#xff1a;
virtualbox ubantu1604 Linux idea社区版2023
jdk1.8
hadoop相关依赖
使用java操作
1. 判断/user/stu/input/test.txt文件是否存在#xff0c;存在则读出文件内容#xff0c;打印在控制台上。反之#xff0c;输出“文件不存在”。
package abc;impo…环境
virtualbox ubantu1604 Linux idea社区版2023
jdk1.8
hadoop相关依赖
使用java操作
1. 判断/user/stu/input/test.txt文件是否存在存在则读出文件内容打印在控制台上。反之输出“文件不存在”。
package abc;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;public class HDFSFlieifExit {public static void main(String[] args) {try {String filename input/test.txt;Configuration conf new Configuration();conf.set(fs.defaultFS, hdfs://localhost:9000);conf.set(fs.hdfs.impl,org.apache.hadoop.hdfs.DistributedFileSystem);FileSystem fs FileSystem.get(conf);Path file new Path(filename);if (fs.exists(file)) {FSDataInputStream getIt fs.open(file);BufferedReader d new BufferedReader(new InputStreamReader(getIt));String con null;while ((con d.readLine()) ! null) {System.out.println(con);}d.close(); //关闭文件fs.close(); //关闭hdfs} else {System.out.println(文件不存在);}} catch (Exception e) {e.printStackTrace();}}
} 2. 使用JAVA编程实现 1 在根目录下创建hdfsjava目录
package abc;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class createDir {public static void main(String[] args) {try {Configuration conf new Configuration();conf.set(fs.defaultFS, hdfs://localhost:9000);conf.set(fs.hdfs.impl,org.apache.hadoop.hdfs.DistributedFileSystem);FileSystem fs FileSystem.get(conf);boolean isok fs.mkdirs(new Path(hdfs:/hdfsjava));if (isok) {System.out.println(成功创建目录!);} else {System.out.println(创建目录失败);}fs.close();} catch (Exception e) {e.printStackTrace();}}
}2 在hdfsjava目录下创建文件mobiles.txt内容是“My telephone is HUAWEI”
package abc;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 Writemobile {public static void main(String[] args) {try {Configuration conf new Configuration();conf.set(fs.defaultFS, hdfs://localhost:9000);conf.set(fs.hdfs.impl,org.apache.hadoop.hdfs.DistributedFileSystem);FileSystem fs FileSystem.get(conf);Path inFile new Path(hdfs:/hdfsjava/mobile.txt);FSDataOutputStream outputStream fs.create(inFile);outputStream.writeUTF(My telephone is HUAWEI);outputStream.flush();outputStream.close();fs.close();} catch (Exception e) {e.printStackTrace();}}
} 3 将linux本地的myLocalFile.txt文件上传到hdfsjava目录下。
package abc;import com.sun.org.apache.xerces.internal.util.URI;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 putfile {public static void main(String[] args) {try {Configuration conf new Configuration();conf.set(fs.defaultFS, hdfs://localhost:9000);conf.set(fs.hdfs.impl,org.apache.hadoop.hdfs.DistributedFileSystem);FileSystem fs FileSystem.get(conf);Path src new Path(/usr/local/hadoop/myLocalFile.txt);Path dst new Path(/hdfsjava);fs.copyFromLocalFile(src, dst);System.out.println(上传完成...);fs.close();} catch (Exception e) {e.printStackTrace();}}
}4 列表显示hdfsjava下的所有文件打印在控制台上。
package abc;import com.sun.org.apache.xerces.internal.util.URI;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;public class printconsole {public static void main(String[] args) {try {Configuration conf new Configuration();conf.set(fs.defaultFS, hdfs://localhost:9000);conf.set(fs.hdfs.impl,org.apache.hadoop.hdfs.DistributedFileSystem);FileSystem fs FileSystem.get(conf);Path dst new Path(/hdfsjava);FileStatus[] status fs.listStatus(dst);for (int i 0; i status.length; i) {System.out.println(status[i].getPath().toString());}fs.close();} catch (Exception e) {e.printStackTrace();}}
} 5 查看hdfs上的hdfsjava目录下myLocalFile.txt文件内容
package abc;import com.sun.org.apache.xerces.internal.util.URI;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;import java.io.BufferedReader;
import java.io.InputStreamReader;public class readfile {public static void main(String[] args) {try {Configuration conf new Configuration();conf.set(fs.defaultFS, hdfs://localhost:9000);conf.set(fs.hdfs.impl,org.apache.hadoop.hdfs.DistributedFileSystem);FileSystem fs FileSystem.get(conf);Path dst new Path(/hdfsjava/myLocalFile.txt);FSDataInputStream in fs.open(dst);BufferedReader d new BufferedReader(new InputStreamReader(in));String line null;while ((line d.readLine()) ! null) {String[] stra line.split( );for (int i 0; i stra.length; i) {System.out.print(stra[i]);System.out.print( );}System.out.println( );}fs.close();} catch (Exception e) {e.printStackTrace();}}
} 6 将hdfs上的hdfsjava目录下mobiles.txt文件下载到本地/home/hadoop中。 7 删除hdfsjava目录。
package abc;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;public class delete {public static void main(String[] args) {try {Configuration conf new Configuration();conf.set(fs.defaultFS, hdfs://localhost:9000);conf.set(fs.hdfs.impl,org.apache.hadoop.hdfs.DistributedFileSystem);FileSystem fs FileSystem.get(conf);Path src new Path(/hdfsjava);fs.delete(src, true);} catch (Exception e) {e.printStackTrace();}}
} 文章转载自: http://www.morning.rqxtb.cn.gov.cn.rqxtb.cn http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.nkjkh.cn.gov.cn.nkjkh.cn http://www.morning.mrfr.cn.gov.cn.mrfr.cn http://www.morning.skmpj.cn.gov.cn.skmpj.cn http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn http://www.morning.nkkpp.cn.gov.cn.nkkpp.cn http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.mfmbn.cn.gov.cn.mfmbn.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.tpssx.cn.gov.cn.tpssx.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.hlshn.cn.gov.cn.hlshn.cn http://www.morning.xkhxl.cn.gov.cn.xkhxl.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.yhdqq.cn.gov.cn.yhdqq.cn http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.rnribht.cn.gov.cn.rnribht.cn http://www.morning.nbpqx.cn.gov.cn.nbpqx.cn http://www.morning.lyhrg.cn.gov.cn.lyhrg.cn http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.btpzn.cn.gov.cn.btpzn.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.fkyqm.cn.gov.cn.fkyqm.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.jlqn.cn.gov.cn.jlqn.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.sxbgc.cn.gov.cn.sxbgc.cn http://www.morning.lkpzx.cn.gov.cn.lkpzx.cn http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.plnry.cn.gov.cn.plnry.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn http://www.morning.qwrb.cn.gov.cn.qwrb.cn http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.grqlc.cn.gov.cn.grqlc.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.xzsqb.cn.gov.cn.xzsqb.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.ptslx.cn.gov.cn.ptslx.cn http://www.morning.kgmkl.cn.gov.cn.kgmkl.cn http://www.morning.qdlr.cn.gov.cn.qdlr.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.pakistantractors.com.gov.cn.pakistantractors.com http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.kqbwr.cn.gov.cn.kqbwr.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.mzmqg.cn.gov.cn.mzmqg.cn http://www.morning.nccyc.cn.gov.cn.nccyc.cn http://www.morning.jpqmq.cn.gov.cn.jpqmq.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.ppbrq.cn.gov.cn.ppbrq.cn http://www.morning.gxqpm.cn.gov.cn.gxqpm.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.rdwm.cn.gov.cn.rdwm.cn http://www.morning.rqqn.cn.gov.cn.rqqn.cn