return 如何使用 HDFS API 来自 HDFS 的文件列表
How return the list of file form HDFS using the HDFS API
我创建了一个 java 函数来打开 HDFS 中的文件。该函数仅用于API HDFS。我没有在我的代码中使用任何 Hadoop 依赖项。
我的功能运行良好:
public static openFile()
{
System.out.print("main for testing the Hdfs WEB API");
URL url = new URL("http://URI/webhdfs/v1/PATH_TO_File?op=OPEN");
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection() ;
con.setRequestMethod("GET");
con.setDoInput(true);
InputStream in = con.getInputStream();
int ch;
while((ch=in.read())!=-1)
{
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
}
我正在对 return HDFS 中的文件列表执行一项新功能。第二个函数是:
public static ListFile()
{
System.out.print("main for testing the Hdfs WEB API");
URL url = new URL("http://URI/webhdfs/v1/PATH_TO_File?op=LISTSTATUS");
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection() ;
con.setRequestMethod("GET");
con.setDoInput(true);
InputStream in = con.getInputStream();
logger.info("list is '{}' ", url.openStream());
} catch (IOException e) {
e.printStackTrace();
}
}
你能帮我吗,我如何return HDFS 中的文件列表使用流来使用扫描器获取响应?当我在浏览器中 运行 时知道这些 URL 运行良好。
提前致谢
您可以使用与第一个解决方案完全相同的逻辑,但这次,使用 StringBuilder 获取完整响应,然后您需要使用 JSON 库对其进行解析。
InputStream in = con.getInputStream();
int ch;
StringBuilder sb = new StringBuilder();
while((ch=in.read())!=-1) {
sb.append((char) ch);
}
String response = sb.toString();
// TODO: parse response string
注意:像 Retrofit / Gson 这样的库会让这更简单
我创建了一个 java 函数来打开 HDFS 中的文件。该函数仅用于API HDFS。我没有在我的代码中使用任何 Hadoop 依赖项。 我的功能运行良好:
public static openFile()
{
System.out.print("main for testing the Hdfs WEB API");
URL url = new URL("http://URI/webhdfs/v1/PATH_TO_File?op=OPEN");
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection() ;
con.setRequestMethod("GET");
con.setDoInput(true);
InputStream in = con.getInputStream();
int ch;
while((ch=in.read())!=-1)
{
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
}
我正在对 return HDFS 中的文件列表执行一项新功能。第二个函数是:
public static ListFile()
{
System.out.print("main for testing the Hdfs WEB API");
URL url = new URL("http://URI/webhdfs/v1/PATH_TO_File?op=LISTSTATUS");
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection() ;
con.setRequestMethod("GET");
con.setDoInput(true);
InputStream in = con.getInputStream();
logger.info("list is '{}' ", url.openStream());
} catch (IOException e) {
e.printStackTrace();
}
}
你能帮我吗,我如何return HDFS 中的文件列表使用流来使用扫描器获取响应?当我在浏览器中 运行 时知道这些 URL 运行良好。 提前致谢
您可以使用与第一个解决方案完全相同的逻辑,但这次,使用 StringBuilder 获取完整响应,然后您需要使用 JSON 库对其进行解析。
InputStream in = con.getInputStream();
int ch;
StringBuilder sb = new StringBuilder();
while((ch=in.read())!=-1) {
sb.append((char) ch);
}
String response = sb.toString();
// TODO: parse response string
注意:像 Retrofit / Gson 这样的库会让这更简单