山东大学软件工程应用与实践——PIG代码分析(二)

2021SC@SDUSC

总览

本次分析的是parseRegister类,此类为判断URI形式,如果为jar文件则读取其中包含的URI进行登记,不是则登记path, scriptingLang和 namespace。

parseRegister类

  • jar文件:JAR文件(Java Archive)是一种软件包文件格式,通常用于聚合大量的Java类文件、相关的元数据和资源(文本、图片等)文件到一个文件,以便开发Java平台应用软件或库。
  • ivy文件:ivy的使用完全是基于以"ivy文件"著称的模块描述符。ivy文件是xml文件,通常被称为ivy.xml,包含模块依赖的描述,它发布的制品和它的配置。
    寄存器解析器类
  • URI Scheme:URI Scheme 是统一资源标识符(Uniform Resource Identifier )的命名结构。

resolvesToJar()方法

通过URI的结尾来判断是否为jar文件。

private boolean resolvesToJar(URI uri) {
    
    
        String scheme = uri.getScheme();
	return (uri.toString().endsWith("jar") || scheme != null && scheme.toLowerCase().equals("ivy"));
    }

hasFileSystemImpl()方法

判断URI是否有有效的文件系统实现,如果有则返回true。

private boolean hasFileSystemImpl(URI uri) {
    
    
      Configuration conf = ConfigurationUtil.toConfiguration(pigServer.getPigContext().getProperties(), true);
      return HadoopShims.hasFileSystemImpl(new Path(uri), conf);
    }

resolve()方法

首先判断scheme是否为空,非空则判断是否为ivy文件,是的话下载它,不是则判断是否实现,实现了则返回数组形式的URI。

 public URI[] resolve(URI uri) throws IOException {
    
    
        String scheme = uri.getScheme();
        if (scheme != null) {
    
    
            scheme = scheme.toLowerCase();
            if (scheme.equals("ivy")) {
    
    
                DownloadResolver downloadResolver = DownloadResolver.getInstance();
                return downloadResolver.downloadArtifact(uri, pigServer);
            }
            if (!hasFileSystemImpl(uri)) {
    
    
                throw new ParserException("Invalid Scheme: " + uri.getScheme());
            }
        }
        return new URI[] {
    
     uri };
    }

parseRegister()方法

该方法为此类的主方法,首先得到URI,判断是否为jar文件,如果是,且scriptingLang, namespace都为空,则将jar中的URI存在数组中,依次进行登记;不是jar文件,则登记path, scriptingLang和 namespace。

    public void parseRegister(String path, String scriptingLang, String namespace) throws IOException {
    
    
        try {
    
    
            URI uri = new URI(path);
            if (resolvesToJar(uri)) {
    
    
                if (scriptingLang != null || namespace != null) {
    
    
                    throw new ParserException("Cannot register a jar with a scripting language or namespace");
                }
                URI[] uriList = resolve(uri);
                for (URI jarUri : uriList) {
    
    
                    pigServer.registerJar(jarUri.toString());
                }
            } else {
    
    
                pigServer.registerCode(path, scriptingLang, namespace);
            }
        } catch (URISyntaxException e) {
    
    
            throw new ParserException("URI " + path + " is incorrect.", e);
        }
    }

总结

本次分析主要为分析对URI的读取登记。

猜你喜欢

转载自blog.csdn.net/qq_45822693/article/details/120751591