閱讀1010 返回首頁    go 阿裏雲


圖模型功能介紹__圖模型_大數據計算服務-阿裏雲

MaxCompute 客戶端提供一個Jar命令用於運行 MaxCompute GRAPH作業,其使用方式與 MapReduce中的Jar命令 相同,這裏僅作簡要介紹:

  1. Usage: jar [<GENERIC_OPTIONS>] <MAIN_CLASS> [ARGS]
  2. -conf <configuration_file> Specify an application configuration file
  3. -classpath <local_file_list> classpaths used to run mainClass
  4. -D <name>=<value> Property value pair, which will be used to run mainClass
  5. -local Run job in local mode
  6. -resources <resource_name_list> file/table resources used in graph, seperate by comma

其中 < GENERIC_OPTIONS>包括(均為可選參數):

  • -conf <configuration file > :指定JobConf配置文件;
  • -classpath <local_file_list > : 本地執行時的classpath,主要用於指定main函數所在的jar包。大多數情況下,用戶更習慣於將main函數與Graph作業編寫在一個包中,例如:單源最短距離算法 ,因此,在執行示例程序時,-resources及-classpath的參數中都出現了用戶的jar包,但二者意義不同,-resources引用的是Graph作業,運行於分布式環境中,而-classpath引用的是main函數,運行於本地,指定的jar包路徑也是本地文件路徑。包名之間使用係統默認的文件分割符作分割(通常情況下,windows係統是分號”;”,linux係統是冒號”:”);
  • -D <prop_name > = < prop_value > : 本地執行時,<mainClass > 的java屬性,可以定義多個;
  • -local:以本地模式執行Graph作業,主要用於程序調試;
  • -resources <resource_name_list > : Graph作業運行時使用的資源聲明。一般情況下,resource_name_list中需要指定Graph作業所在的資源名稱。如果用戶在Graph作業中讀取了其他ODPS資源,那麼,這些資源名稱也需要被添加到resource_name_list中。資源之間使用逗號分隔,使用跨項目空間使用資源時,需要前麵加上:PROJECT_NAME/resources/,示例:-resources otherproject/resources/resfile;

同時,用戶也可以直接運行GRAPH作業的main函數直接將作業提交到 MaxCompute ,而不是通過 MaxCompute 客戶端提交作業。以PageRank算法 為例:

  1. public static void main(String[] args) throws IOException {
  2. if (args.length < 2)
  3. printUsage();
  4. GraphJob job = new GraphJob();
  5. job.setGraphLoaderClass(PageRankVertexReader.class);
  6. job.setVertexClass(PageRankVertex.class);
  7. job.addInput(TableInfo.builder().tableName(args[0]).build());
  8. job.addOutput(TableInfo.builder().tableName(args[1]).build());
  9. // 將作業中使用的資源添加到cache resource,對應於jar命令中 -resources 和 -libjars 中指定的資源
  10. job.addCacheResource("mapreduce-examples.jar");
  11. // 將使用的jar及其他文件添加到class cache resource,對應於jar命令中 -libjars 中指定的資源
  12. job.addCacheResourceToClassPath("mapreduce-examples.jar");
  13. // 設置console中,odps_config.ini對應的配置項,使用時替換為自己的配置
  14. OdpsConf.getInstance().setProjName("project_name");
  15. OdpsConf.getInstance().setEndpoint("end_point");
  16. OdpsConf.getInstance().setAccessId("access_id");
  17. OdpsConf.getInstance().setAccessKey("access_key");
  18. // default max iteration is 30
  19. job.setMaxIteration(30);
  20. if (args.length >= 3)
  21. job.setMaxIteration(Integer.parseInt(args[2]));
  22. long startTime = System.currentTimeMillis();
  23. job.run();
  24. System.out.println("Job Finished in "
  25. + (System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
  26. }

輸入輸出

MaxCompute GRAPH作業的輸入輸出限製為表,不允許用戶自定義輸入輸出格式。

定義作業輸入,支持多路輸入:

  1. GraphJob job = new GraphJob();
  2. job.addInput(TableInfo.builder().tableName(“tblname”).build()); //表作為輸入
  3. job.addInput(TableInfo.builder().tableName(“tblname”).partSpec("pt1=a/pt2=b").build()); //分區作為輸入
  4. //隻讀取輸入表的 col2 和 col0 列,在 GraphLoader 的 load 方法中,record.get(0) 得到的是col2列,順序一致
  5. job.addInput(TableInfo.builder().tableName(“tblname”).partSpec("pt1=a/pt2=b").build(), new String[]{"col2", "col0"});

備注:

  • 關於作業輸入定義,更多的信息參見GraphJob的addInput相關方法說明,框架讀取輸入表的記錄傳給用戶自定義的GraphLoader載入圖數據;
  • 限製: 暫時不支持分區過濾條件。更多應用限製請參考 應用限製

定義作業輸出,支持多路輸出,通過label標識每路輸出:

  1. GraphJob job = new GraphJob();
  2. //輸出表為分區表時需要給到最末一級分區
  3. job.addOutput(TableInfo.builder().tableName("table_name").partSpec("pt1=a/pt2=b").build());
  4. // 下麵的參數 true 表示覆蓋tableinfo指定的分區,即INSERT OVERWRITE語義,false表示INSERT INTO語義
  5. job.addOutput(TableInfo.builder().tableName("table_name").partSpec("pt1=a/pt2=b").lable("output1").build(), true);

備注:

  • 關於作業輸出定義,更多的信息參見GraphJob的addOutput 相關方法說明;
  • Graph作業在運行時可以通過WorkerContext的write方法寫出記錄到輸出表,多路輸出需要指定標識,如上麵的 “output1”;
  • 更多應用限製請參考 應用限製

讀取資源

GRAPH程序中添加資源

除了通過jar命令指定GRAPH讀取的資源外,還可以通過GraphJob的下麵兩個方法指定:

  1. void addCacheResources(String resourceNames)
  2. void addCacheResourcesToClassPath(String resourceNames)

GRAPH程序中使用資源

在 GRAPH 程序中可以通過相應的上下文對象WorkerContext的下述方法讀取資源:

  1. public byte[] readCacheFile(String resourceName) throws IOException;
  2. public Iterable<byte[]> readCacheArchive(String resourceName) throws IOException;
  3. public Iterable<byte[]> readCacheArchive(String resourceName, String relativePath)throws IOException;
  4. public Iterable<WritableRecord> readResourceTable(String resourceName);
  5. public BufferedInputStream readCacheFileAsStream(String resourceName) throws IOException;
  6. public Iterable<BufferedInputStream> readCacheArchiveAsStream(String resourceName) throws IOException;
  7. public Iterable<BufferedInputStream> readCacheArchiveAsStream(String resourceName, String relativePath) throws IOException;

備注:

  • 通常在WorkerComputer的setup方法裏讀取資源,然後保存在Worker Value中,之後通過getWorkerValue方法取得;
  • 建議用上麵的流接口,邊讀邊處理,內存耗費少;
  • 更多應用限製請參考 應用限製

最後更新:2016-11-23 17:16:04

  上一篇:go 兼容版本SDK介紹__Java SDK介紹_MapReduce_大數據計算服務-阿裏雲
  下一篇:go 圖模型開發和調試__圖模型_大數據計算服務-阿裏雲