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

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

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

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

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

  1. File file = new File(HelloAnalytics.class.getResource("client_secrets.p12").toURI());
  2. HttpTransport httpTransport = GoogleNetHttpTransport
  3. .newTrustedTransport();
  4. GoogleCredential credential = new GoogleCredential.Builder()
  5. .setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
  6. .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
  7. .setServiceAccountPrivateKeyFromP12File(file)
  8. .setServiceAccountScopes(AnalyticsReportingScopes.all())
  9. .build();

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

  1. File file = null;
  2. String resource = "client_secrets.p12";
  3. URL res = getClass().getResource(resource);
  4. if (res.toString().startsWith("jar:")) {
  5. InputStream input = getClass().getResourceAsStream(resource);
  6. file = File.createTempFile("client_secrets", ".p12");
  7. OutputStream out = new FileOutputStream(file);
  8. int read;
  9. byte[] bytes = new byte[1024];
  10. while ((read = input.read(bytes)) != -1) {
  11. out.write(bytes, 0, read);
  12. }
  13. file.deleteOnExit();
  14. } else {
  15. //this will probably work in your IDE, but not from a JAR
  16. file = new File(res.getFile());
  17. }
  18. if (file != null && !file.exists()) {
  19. throw new IOException("...");
  20. }
  21. HttpTransport httpTransport = GoogleNetHttpTransport
  22. .newTrustedTransport();
  23. GoogleCredential credential = new GoogleCredential.Builder()
  24. .setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
  25. .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
  26. .setServiceAccountPrivateKeyFromP12File(file)
  27. .setServiceAccountScopes(AnalyticsReportingScopes.all())
  28. .build();

关于如何进入 jar 合适吗?

暂无答案!

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

相关问题