用java写数据存入HDFS中,采用追加模式

本文介绍如何使用Java代码将数据追加写入Hadoop的HDFS文件系统,主要涉及Hadoop相关API的使用,适用于大数据处理场景。

代码如下:::::

package com.postpony.bulkprint.BigData;

import com.cloudera.org.codehaus.jackson.map.ObjectMapper;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;

public class Input_HDFS {
    private static Configuration conf = new Configuration();
    static {
        conf.setBoolean("dfs.support.append", true);
        conf.set("fs.defaultFS","hdfs://node01:8020");
        conf.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");
        conf.set("fe.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
        conf.setBoolean("dfs.client.block.write.replace-datanode-on-failure.enable", true);
    }
    public static void main(String[] args) {
        String a = "西安人的城墙下是西安人的火车";
        //JSONObject jo = JSONObject.parseObject(new String("123"));
        try {
            write2hdfs(a,"/test02/test005");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * json 存储
     *
     * @param
     * @param
     * @throws IOException
     */
    public static void write2hdfs(Object listContent, String filePath) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        FileSystem fs = null;
        Path path = new Path(filePath);
        FSDataOutputStream output = null;
        fs = path.getFileSystem(conf);
//如果此文件不存在则创建新文件
        if (!fs.exists(path)) {
            fs.createNewFile(path);
        }
        output = fs.append(new Path(filePath));
        //System.out.println(listContent.toString());
        output.write(objectMapper.writeValueAsString(listContent).getBytes("UTF-8"));
        output.write("\n".getBytes("UTF-8"));//换行
        fs.close();
        output.close();

    }
}

pom文件:

<repositories>
   <repository>
      <id>aliyun</id>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
   </repository>
   <repository>
      <id>cloudera</id>
      <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
   </repository>
   <repository>
      <id>jboss</id>
      <url>http://repository.jboss.com/nexus/content/groups/public</url>
   </repository>
</repositories>
<properties>
   <hadoop.version>2.6.0-cdh5.14.0</hadoop.version>
</properties>
<dependency>
   <groupId> org.apache.cassandra</groupId>
   <artifactId>cassandra-all</artifactId>
   <version>0.8.1</version>

   <exclusions>
      <exclusion>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
      <exclusion>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
      </exclusion>
   </exclusions>

</dependency>
<dependency>
   <groupId>org.apache.hadoop</groupId>
   <artifactId>hadoop-client</artifactId>
   <version>${hadoop.version}</version>
</dependency>

<dependency>
   <groupId>postpony</groupId>
   <artifactId>postpony-micro-service-starter</artifactId>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值