在eclipse操作数据到HDFS

查看数: 47311 | 评论数: 13 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2014-11-21 16:21

正文摘要:

本帖最后由 LoveJW 于 2014-11-21 16:23 编辑 我在eclipse里往hdfs的表里追加数据,代码是追加操作,为什么一直报该文件已存在的错误呢?? 错误: org.apache.hadoop.ipc.RemoteException: failed to ...

回复

sstutu 发表于 2014-11-21 17:19:40
elena 发表于 2015-5-25 11:17:25
sstutu 发表于 2014-11-21 17:19
可以尝试下,不过你先贴出代码来,让我一个个敲出来,唉

通过这种方法解决了追加的问题,但有个疑问,使用 FileSystem.get( URI.create(hdfs_path), conf ) 是对HDFS上的文件进行追加,如果我用Eclipse开发,文件没有上传到HDFS,只在本地,使用 FileSystem.get( conf ) 为什么不能实现追加呢?

点评

每个函数功能不一样,具体看源码  发表于 2015-5-25 13:53
codefarmer 发表于 2015-4-27 16:17:46
根本不是楼上说的那几个解决方法。上面的问题我几乎都遇到过。

需要在eclise项目中的java代码中添加
conf.setBoolean( "dfs.support.append", true );
conf.set( "dfs.client.block.write.replace-datanode-on-failure.policy" ,"NEVER" );
conf.set( "dfs.client.block.write.replace-datanode-on-failure.enable","true" );
远程机器上面的hdfs-site.xml中配置了,本地的hadoop这个配置文件中也配置了没什么用,必须得在代码中加上。亲测。
desehawk 发表于 2014-11-25 22:18:21
LoveJW 发表于 2014-11-25 19:56
我不是追加的文件。是直接追加从别处拿来的数据


要学会变通,你先以文件的方式追加,看是否可以。
LoveJW 发表于 2014-11-25 19:56:03
desehawk 发表于 2014-11-25 18:58
建议改成这种格式

我不是追加的文件。是直接追加从别处拿来的数据
LoveJW 发表于 2014-11-25 19:54:48
desehawk 发表于 2014-11-25 19:00
楼主你调试了,确定文件名称已经存在。建议使用简单的已经存在的名字。

确定啊,我写的绝对路径 调试看的路径也对,但是就是报新建错误,我没新建的操作啊
desehawk 发表于 2014-11-25 19:00:03
LoveJW 发表于 2014-11-21 18:33
public boolean appendDataToFile(String table, String data) {
        boolean result = false;
    ...




楼主你调试了,确定文件名称已经存在。建议使用简单的已经存在的名字。
desehawk 发表于 2014-11-25 18:58:20
LoveJW 发表于 2014-11-25 15:02
上面那个问题能再帮忙看看吗??




建议改成这种格式
  1. public class PutMerge {
  2. /**
  3. * @param args
  4. *            void
  5. * @throws IOException
  6. */
  7. public static void main(String[] args) throws IOException {
  8. // TODO Auto-generated method stub
  9.    Configuration conf = new Configuration();
  10.         FileSystem hdfs = FileSystem.get(conf);
  11.         FileSystem local = FileSystem.getLocal(conf);
  12.         
  13.         Path inputDir = new Path("/home/du/inout");
  14.         Path hdfsFile = new Path("/myhdfs");
  15.         
  16.         try
  17.         {
  18.             FileStatus[] inputFiles = local.listStatus(inputDir);
  19.             FSDataOutputStream out = hdfs.append(hdfsFile);
  20.             
  21.             for(int i = 0; i < inputFiles.length; i++ )
  22.             {
  23.                 System.out.println(inputFiles[i].getPath().getName());
  24.                 FSDataInputStream in = local.open(inputFiles[i].getPath());
  25.                 byte buffer[] = new byte[1024];
  26.                 int bytesRead = 0;
  27.                 while( (bytesRead = in.read(buffer)) > 0)
  28.                 {
  29.                     out.write(buffer, 0, bytesRead);
  30.                 }
  31.                 in.close();
  32.             }
  33.             out.close();
  34.         }
  35.         catch (IOException e)
  36.         {
  37.             e.printStackTrace();
  38.         }
  39. }
  40. }
复制代码



LoveJW 发表于 2014-11-25 15:02:47
sstutu 发表于 2014-11-21 17:19
可以尝试下,不过你先贴出代码来,让我一个个敲出来,唉

上面那个问题能再帮忙看看吗??
LoveJW 发表于 2014-11-21 18:33:47
本帖最后由 LoveJW 于 2014-11-21 18:36 编辑
sstutu 发表于 2014-11-21 17:19
可以尝试下,不过你先贴出代码来,让我一个个敲出来,唉

public boolean appendDataToFile(String table, String data) {
        boolean result = false;
        try {
            if(StringUtils.isBlank(table)||StringUtils.isBlank(data)){
                throw new NullPointerException("appendDataToFile:table or data is null!");
            }
            Path filePath = new Path(host + path + table + "/part-m-00000");
            Configuration conf = new Configuration();
            FileSystem fs = filePath.getFileSystem(conf);
            fs.setReplication(filePath, (short) 3);
            OutputStream out = fs.append(filePath);
            InputStream in = new BufferedInputStream(new ByteArrayInputStream(data.getBytes()));
            IOUtils.copyBytes(in, out, conf);
//            fs.close();
//            IOUtils.closeStream(in);
            result = true;
            

        } catch (Exception e) {
            log.warn("appendDataToFile:data append error",e);
        }   
        return result;
    }

我就是要直接把字符串存过去的啊,Hdfs上有建好的文件,就是要实现追加功能,那两个错误参数是哪两个??



关闭

推荐上一条 /2 下一条