20,807
社区成员
发帖
与我相关
我的任务
分享
package hdfs;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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 TestProblem {
public static void main(String[] args) throws FileNotFoundException, IOException {
final String dst = "hdfs://172.16.4.14:9000/user/hdfs/apk/20.apk";
final Configuration conf = new Configuration();
final FileSystem fs = FileSystem.get(URI.create(dst), conf);
//客户端请求线程数
int count = 10;
ExecutorService es = Executors.newCachedThreadPool();
for (int i = 0; i < count; i++) {
es.execute(new Runnable() {
@Override
public void run() {
try {
long begin = System.currentTimeMillis();
FSDataInputStream hdfsInStream = fs.open(new Path(dst));
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copyBytes(hdfsInStream, out, 1024, true);
System.out.println(out.size());
System.out.println("consuming time:" + (System.currentTimeMillis() - begin));
hdfsInStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
}