写入HDFS的代码
public class HDFS {
private static final String HDFS_ADDR = Config.HDFS_PATH;//hdfs://10.58.121.170:9000
private static FileSystem fs;
static {
Configuration configuration = new Configuration();
configuration.set("dfs.replication", Config.REPLICATION);//crawl.replication_number=2
try {
fs = FileSystem.get(URI.create(HDFS_ADDR), configuration);
} catch (IOException e) {
e.printStackTrace();
}
}
private Path path;
private FSDataOutputStream out;
public HDFS(String file) {
if (file != null && file.length() > 0) {
init(file);
}
}
private void init(String file) {
path = new Path(HDFS_ADDR + file);
try {
out = fs.create(path);
} catch (IOException e) {
e.printStackTrace();
}
}
public void insert(String content) {
try {
out.write((content + "\n").getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
public void sync() {
try {
out.hflush();
out.hsync();
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}