java—从jar将文件加载到hadoop数据节点

mxg2im7a  于 2021-06-02  发布在  Hadoop
关注(0)|答案(0)|浏览(209)

我正在多个数据节点上运行hadoop作业。为此我提供了一个 .jar 包含我的工作的文件。
在工作中,我想访问谷歌分析api(见介绍教程)。对于身份验证,分析api需要 .p12 用于身份验证的文件。基本上我有这个问题。使用gradle,我可以将此文件打包到我的 jar 文件:

from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}

文件位于 jar . 现在我尝试修改示例代码以阅读 .p12 从jar内部创建文件或以任何其他方式访问它。
我试过很多种方法,但最后总是以失败告终 NullPointerException . 以下是我最近的一次尝试:

File file = new File(HelloAnalytics.class.getResource("client_secrets.p12").toURI());

HttpTransport httpTransport = GoogleNetHttpTransport
        .newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
        .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
        .setServiceAccountPrivateKeyFromP12File(file)
        .setServiceAccountScopes(AnalyticsReportingScopes.all())
        .build();

我也试过用 BufferedReader 并将其导出回文件系统:

File file = null;
    String resource = "client_secrets.p12";
    URL res = getClass().getResource(resource);
    if (res.toString().startsWith("jar:")) {
        InputStream input = getClass().getResourceAsStream(resource);
        file = File.createTempFile("client_secrets", ".p12");
        OutputStream out = new FileOutputStream(file);
        int read;
        byte[] bytes = new byte[1024];
        while ((read = input.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        file.deleteOnExit();
    } else {
        //this will probably work in your IDE, but not from a JAR
        file = new File(res.getFile());
    }

    if (file != null && !file.exists()) {
        throw new IOException("...");
    }

    HttpTransport httpTransport = GoogleNetHttpTransport
            .newTrustedTransport();
    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountPrivateKeyFromP12File(file)
            .setServiceAccountScopes(AnalyticsReportingScopes.all())
            .build();

关于如何进入 jar 合适吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题