简体   繁体   English

如何使用Java将数据从SAS服务器提取到HDFS?

[英]How to pull data from sas server to hdfs using Java?

我想从sas服务器中提取数据并将其放入HDFS中。我想对任何Idea都使用Java代码。

The easiest mechanism is going to be to install the Hadoop client libraries and then upload your SAS files onto HDFS via the client commands: hadoop fs -put <filename> </hdfs/path/filename> 最简单的机制是安装Hadoop客户端库,然后通过客户端命令将SAS文件上传到HDFS: hadoop fs -put <filename> </hdfs/path/filename>

If you don't want to use the client commands, you can always write your own java application to perform the upload from the SAS server to HDFS. 如果您不想使用客户端命令,则始终可以编写自己的Java应用程序来执行从SAS服务器到HDFS的上传。 An untested piece of example code that transfers a file using the fs.FileSystem.copyFromLocalFile interface: 一段未经测试的示例代码,它使用fs.FileSystem.copyFromLocalFile接口传输文件:

package org.mycompany;
import java.security.PrivilegedExceptionAction;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileStatus;

public class HDFSTransfer {

    public static void main(String args[]) {

        try {
            UserGroupInformation ugi
                = UserGroupInformation.createRemoteUser("myuser");

            ugi.doAs(new PrivilegedExceptionAction<Void>() {

                public Void run() throws Exception {

                    Configuration conf = new Configuration();
                    conf.set("fs.defaultFS", "hdfs://10.20.30.40:8020/user/myuser");
                    conf.set("hadoop.job.ugi", "myuser");

                    FileSystem fs = FileSystem.get(conf);

                    fs.copyFromLocalFile(new Path("/local/file/test"), new Path("/user/myuser/test"));

                    FileStatus[] status = fs.listStatus(new Path("/user/myuser"));
                    for(int i=0;i<status.length;i++){
                        System.out.println(status[i].getPath());
                    }
                    return null;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM