private void parseDataSource(String file) throws Exception {
    Digester digester = new Digester();
    digester.setValidating(false);
    digester.push(this);
    digester.addCallMethod("datasources/datasource", "addDataSource", 6);

    digester.addCallParam("datasources/datasource/name", 0);
    digester.addCallParam("datasources/datasource/driver", 1);
    digester.addCallParam("datasources/datasource/url", 2);
    digester.addCallParam("datasources/datasource/username", 3);
    digester.addCallParam("datasources/datasource/password", 4);
    digester.addCallParam("datasources/datasource/internallogon", 5);
    digester.parse(new File(file));
  }
 @Test
 // <certification>在程序中对应一个字符串,故而没有列出。
 public void digest() {
   Digester digester = new Digester();
   // 注意,此处并没有象上例一样使用push,是因为此处从根元素创建了一个对//象实例
   digester.addObjectCreate("academy", Academy.class);
   // 将< academy >的属性与对象的属性关联
   digester.addSetProperties("academy");
   digester.addObjectCreate("academy/student", Student.class);
   digester.addSetProperties("academy/student");
   digester.addObjectCreate("academy/student/course", Course.class);
   digester.addBeanPropertySetter("academy/student/course/id");
   digester.addBeanPropertySetter("academy/student/course/name");
   digester.addSetNext("academy/student/course", "addCourse");
   digester.addSetNext("academy/student", "addStudent");
   digester.addObjectCreate("academy/teacher", Teacher.class);
   digester.addSetProperties("academy/teacher");
   // 当遇到academy/teacher/certification时,调用addCertification
   digester.addCallMethod("academy/teacher/certification", "addCertification", 1);
   // 设置addCertification的参数值,此处的0表示这个元素体的第一个值
   // 为参数值传入addCertification。在此处,即为<certification>的值。
   // (因为就只有一个)
   digester.addCallParam("academy/teacher/certification", 0);
   digester.addSetNext("academy/teacher", "addTeacher");
   try {
     Academy a = (Academy) digester.parse(xmlFile);
     System.out.print(a);
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (SAXException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
Beispiel #3
0
  /**
   * 解析base.mapred.xml配置信息到java对象
   *
   * @return 配置信息pojo对象
   */
  public static MapreduceConfigInfo parseConfig(String extraPartitions) {
    Digester digester = new Digester();
    digester.setValidating(false);

    digester.addObjectCreate("mapred", MapreduceConfigInfo.class);
    digester.addBeanPropertySetter("mapred/baseId");
    digester.addBeanPropertySetter("mapred/projectId");
    digester.addBeanPropertySetter("mapred/resourceName");
    digester.addBeanPropertySetter("mapred/idePath");

    digester.addBeanPropertySetter("mapred/mapOutputKey");
    digester.addBeanPropertySetter("mapred/mapOutputValue");
    digester.addBeanPropertySetter("mapred/partitionColumns");
    digester.addBeanPropertySetter("mapred/outputKeySortColumns");
    digester.addBeanPropertySetter("mapred/outputKeySortOrders");
    digester.addBeanPropertySetter("mapred/outputGroupingColumns");
    digester.addBeanPropertySetter("mapred/numReduceTask");
    digester.addBeanPropertySetter("mapred/memoryForMapTask");
    digester.addBeanPropertySetter("mapred/memoryForReduceTask");

    digester.addBeanPropertySetter("mapred/jobLauncher");
    digester.addBeanPropertySetter("mapred/mapper");
    digester.addBeanPropertySetter("mapred/reducer");
    digester.addBeanPropertySetter("mapred/combiner");

    digester.addObjectCreate("mapred/inputTables/table", OdpsTableInfo.class);
    digester.addBeanPropertySetter("mapred/inputTables/table/name");
    digester.addCallMethod("mapred/inputTables/table/partitions/partition", "addPartition", 1);
    digester.addCallParam("mapred/inputTables/table/partitions/partition", 0);
    digester.addSetNext("mapred/inputTables/table", "addInputTable");

    digester.addObjectCreate("mapred/outputTable", OdpsTableInfo.class);
    digester.addBeanPropertySetter("mapred/outputTable/name");
    digester.addCallMethod("mapred/outputTable/partition", "addPartition", 1);
    digester.addCallParam("mapred/outputTable/partition", 0);
    digester.addSetNext("mapred/outputTable", "setOutputTable");

    InputStream is = ClassLoader.getSystemResourceAsStream("META-INF/base.mapred.xml");
    try {
      MapreduceConfigInfo conf = digester.parse(is);

      // 将额外分区合并入输入表和输出表
      if (!extraPartitions.isEmpty()) {
        String[] eps = extraPartitions.split(":");
        for (String ep : eps) {
          int pos = ep.indexOf("/");
          String tableName = ep.substring(0, pos);
          String partition = ep.substring(pos + 1);

          for (OdpsTableInfo t : conf.getInputTables()) {
            if (t.getName().equals(tableName)) {
              t.addPartition(partition);
            }
          }

          if (conf.getOutputTable().getName().equals(tableName)) {
            conf.getOutputTable().addPartition(partition);
          }
        }
      }

      return conf;
    } catch (Exception e) {
      return null;
    }
  }