文章目录
pom.xml具体代码以及注释
在一些情景下,需要用java读取hadoop的文件的具体内容,而不是把文件复制到本地。
pom.xml
pom.xml如下:
<dependencies>
<dependency>
<groupId>org.apache.hive
</groupId>
<artifactId>hive-jdbc
</artifactId>
<version>2.1.1
</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop
</groupId>
<artifactId>hadoop-client
</artifactId>
<version>3.0.0
</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop
</groupId>
<artifactId>hadoop-common
</artifactId>
<version>3.0.0
</version>
</dependency>
</dependencies>
具体代码以及注释
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 java
.io
.BufferedReader
;
import java
.io
.IOException
;
import java
.io
.InputStreamReader
;
import java
.net
.URI
;
import java
.net
.URISyntaxException
;
public class test {
public static void main(String
[] args
) throws URISyntaxException
, IOException
, InterruptedException
{
FileSystem fs
=FileSystem
.get(
new URI("hdfs://192.168.153.129:9000"), new Configuration(), "root");
FSDataInputStream in
= fs
.open(new Path(
"hdfs://192.168.153.129:9000/user/hive/warehouse/test.db/t1/dt=3/t1"));
BufferedReader d
= new BufferedReader(new InputStreamReader(in
));
String line
;
while ((line
= d
.readLine()) != null
) {
System
.out
.println(line
)
}
d
.close();
in
.close();
fs
.close();
}
}
这样就可以读取到hadoop文件中的具体内容,结合实际需求做相应处理即可。 这里有一个和这个有关的项目的文章,建议一看 Java,Scala - 使用代码统计hadoop中hdfs的文件大小以及文件夹大小