本文以Loadrunner的Java_Vuser腳本為例驱敲,來做一次HDFS的文件操作測試弥咪,由于LoadRunner 11只支持JDK1.6,所以Hadoop選擇的Jar包也只能用Hadoop2.6.0悠垛,但是這不影響連接高版本的hadoop-HDFS(本次測試就實(shí)現(xiàn)了連接操作hadoop2.7下HDFS)但荤。
1、在loadrunner中新建腳本(本文以LoadRunner11為例)彼城,要求選擇協(xié)議類型為Java->Java Vuser
2诅蝶、在Run-time Settings設(shè)置JDK路徑,由于LoadRunner11不支持jdk1.8募壕,本次測試是拷貝了一份低版本的JDK1.6调炬,所以路徑選擇固定路徑模式,如下所示:
3舱馅、可以上網(wǎng)下載一份Hadoop2.6.0的JAR包(官網(wǎng)和網(wǎng)上都能搜到)缰泡,我專門準(zhǔn)備了一份供大家下載(做了精簡,去掉了一些與本次測試無關(guān)的Jar包):http://download.csdn.net/detail/smooth00/9881769代嗤,將JAR包放到Loadrunner的include目錄下或其它指定目錄下棘钞,并在Run-time Settings中配置Classpath:
4、在Loadrunner中以java Vuser協(xié)議創(chuàng)建腳本干毅,腳本樣例如下:
/*
* LoadRunner Java script. (Build: _build_number_)
*
* Script Description:
*? ? ? ? ? ? ? ? ? ?
*/
import java.io.FileInputStream;?
import java.io.FileNotFoundException;?
import java.io.FileOutputStream;?
import java.io.InputStream;?
import java.io.IOException;?
import java.io.OutputStream;?
import java.net.URI;?
import java.net.URISyntaxException;?
import org.apache.hadoop.conf.Configuration;?
import org.apache.hadoop.fs.BlockLocation;?
import org.apache.hadoop.fs.FileStatus;?
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FSDataOutputStream;?
import org.apache.hadoop.fs.Path;?
import org.apache.hadoop.io.IOUtils;?
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
import java.io.File;
import lrapi.lr;
public class Actions
{
? ? FileSystem fs = null;
? ? private Configuration configuration =null;
? ? private String keyFS ="fs.defaultFS";
? ? private String keyUser ="hadoop.user";?
? ? //上傳文件? 測試成功
? ? // @uploadFile 要上傳的本地文件
? ? // @intputFile 輸入到HDFS的文件
? ? public void testUpload(String uploadFile,String intputFile) throws Exception{?
? ? ? ? InputStream in = new FileInputStream(uploadFile);?
? ? ? ? OutputStream out = fs.create(new Path(intputFile));
? ? ? ? IOUtils.copyBytes(in, out, 1024, true);//in輸入源, out輸出源頭, 1024緩沖區(qū)大小 ,true 是否關(guān)閉數(shù)據(jù)流宜猜。如果是false,就在finally里關(guān)掉?
? ? }
? ? //創(chuàng)建文件夾? 測試成功?
? ? public void testMkdir(String dirs) throws IllegalArgumentException, IOException{?
? ? ? ? boolean? flag = fs.mkdirs(new Path(dirs));
? ? ? ? System.out.println(flag);//如果創(chuàng)建目錄成功會返回true?
? ? }
? ? //下載文件? 測試成功
? ? // @outputFile 要下載的HDFS文件
? ? // @downloadFile 要下載到本地的文件
? ? public void testDownload(String outputFile,String downloadFile) throws IllegalArgumentException, IOException{?
InputStream in = fs.open(new Path(outputFile));//intput.txt
? ? ? ? OutputStream out = new FileOutputStream(downloadFile);//下載到本地路徑 以及重命名后的名字?
? ? ? ? IOUtils.copyBytes(in, out, 4096, true);?
? ? }
? ? //刪除文件? 測試成功?
? ? public void testDelFile(String delFile) throws IllegalArgumentException, IOException{?
? ? ? ? boolean flag = fs.delete(new Path(delFile),true);//如果是刪除路徑? 把參數(shù)換成路徑即可"/a/test4"? inpufile
? ? ? ? //第二個參數(shù)true表示遞歸刪除硝逢,如果該文件夾下還有文件夾或者內(nèi)容 姨拥,會變遞歸刪除,若為false則路徑下有文件則會刪除不成功?
? ? ? ? System.out.println("刪除文件 or 路徑");?
? ? ? ? System.out.println("delete?"+flag);//刪除成功打印true?
? ? }
? ? //重命名文件? 測試成功
? ? // @oldFile 要下載的HDFS文件
? ? // @newFile 要下載到本地的文件
? ? public void testRename(String oldFile,String newFile) throws IllegalArgumentException, IOException{?
? ? ? ? boolean flag = fs.rename(new Path(oldFile),new Path(newFile));//第一個參數(shù)改名為第二個參數(shù)?
? ? ? ? String result=flag?"成功":"失敗";?
? ? ? ? System.out.println("result of rename?"+result);//刪除成功打印true?
? ? }
? ? //查看文件是否存在? ? 測試成功?
? ? public void CheckFile(String existFile) throws IllegalArgumentException, IOException{?
? ? ? ? boolean? flag = fs.exists(new Path(existFile));?
? ? ? ? System.out.println("Exist?"+flag);//如果創(chuàng)建目錄成功會返回true?
? ? }
? ? //尋找文件在文件集中位置? 測試成功?
? ? public void FileLoc(String searchFile) throws IllegalArgumentException, IOException{?
? ? ? ? FileStatus? filestatus = fs.getFileStatus(new Path(searchFile));?
? ? ? ? //System.out.println("filestatus?"+filestatus);//如果創(chuàng)建目錄成功會返回true?
? ? ? ? BlockLocation[] blkLocations=fs.getFileBlockLocations(filestatus, 0, filestatus.getLen());//文件開始與結(jié)束?
? ? ? ? int blockLen=blkLocations.length;//塊的個數(shù)
System.out.println("塊數(shù):"+blockLen);
? ? ? ? System.out.println("---------分割線--------");?
? ? ? ? for(int i=0;i
? ? ? ? ? ? String[] hosts=blkLocations[i].getHosts();?
? ? ? ? ? ? System.out.println("block_"+i+"location:"+hosts[0]);?
? ? ? ? }?
? ? ? ? System.out.println("---------分割線---------");?
? ? }
? ? //直接在hdfs上創(chuàng)建文件并在其中輸入文字? 測試成功
? ? // @content 要寫入文件的文字內(nèi)容
? ? // @outputFile 寫入的HDFS文件
? ? public void testCreateTextFile(String content,String outputFile) throws IllegalArgumentException, IOException{?
? ? ? ? byte[] buff=content.getBytes();//想要輸入內(nèi)容
? ? ? ? Path path=new Path(outputFile);//文件存放路徑及文件名稱? /a/test4/javawrite.txt
? ? ? ? FSDataOutputStream outputStream=fs.create(path);?
? ? ? ? outputStream.write(buff, 0, buff.length);?
? ? ? ? System.out.println("輸出文件成功");?
? ? }
public int init() throws Throwable {
? ? //System.setProperty("hadoop.home.dir", "D:\\Program Files\\HP\\LoadRunner\\include\\hadoop\\hadoop-common-2.2.0-bin-master");
? ? //加載日志輸出方式
? ? File directory = new File(".");
? ? DOMConfigurator.configure(directory.getCanonicalPath()+"\\log4j.xml");//加載log4j.xml文件
? ? //PropertyConfigurator.configure("E:/study/log4j/log4j.properties");//加載.properties文件
? ? Logger log=Logger.getLogger("org.zblog.test");
? ? System.setProperty("hadoop.home.dir", directory.getCanonicalPath()+"\\hadoop-common-2.2.0-bin-master");
? ? ? ? ? ? //初始化
? ? configuration=new Configuration();
? ? //configuration.set(keyFS,"hdfs://172.17.2.12:8020");
? ? ? ? ? ? configuration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
? ? configuration.set("fs.file.impl", "org.apache.hadoop.fs.LocalFileSystem");
? ? ? ? ? ? //configuration.set("fs.hdfs.impl",org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
? ? fs = FileSystem.get(new URI("hdfs://172.16.1.80:8020"), configuration,"hdfs");//hdfs為用戶名
? ? //fs = FileSystem.get(URI.create(fsname), configuration, user);
return 0;
}//end of init
public int action() throws Throwable {
? ? testMkdir("/a//test4");
? ? testCreateTextFile("hello hadoop world!\n","/a/test4/intput.txt");
? ? testUpload("d://textdownload.txt","/a/test4/intput.txt");
? ? testDownload("/a/test4/intput.txt","d://textdownload2.txt");
? ? testRename("/a/test4/intput.txt","/a/test4/intput");
? ? FileLoc("/a/test4/intput");
? ? testDelFile("/a/test4/intput");
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
需要說明的渠鸽,本次樣例腳本中叫乌,還引用了hadoop-common-2.2.0-bin-master(主要是因?yàn)樵趙indows下開發(fā)Hadoop需要用到winutils.exe,本次的目的是為了在Windows下運(yùn)行以上腳本后徽缚,能同時(shí)輸出hadoop-HDFS運(yùn)行日志)憨奸,下載地址為(免積分):http://download.csdn.net/detail/nma_123456/8663763
5、將下載的hadoop-common-2.2.0-bin-master解壓到Loadrnner腳本目錄下(hadoop-common-2.2.0-bin-master下一層是bin目錄)
6凿试、同樣在Loadrnner腳本目錄下創(chuàng)建hadoop的日志配置文件log4j.xml排宰,配置內(nèi)容如下:
7、一切就緒后红省,就可以運(yùn)行腳本了(記住HDFS的連接地址额各,如hdfs://172.17.2.12:8020和用戶要配置正確)国觉,運(yùn)行成功如下所示: