合并资源指将多个配置文件合并,产生一个配置

Hadoop配置文件的根元素configuration包含property元素,每个property表示一个配置项。final属性用于防止合并资源时配置项被覆盖。通过Configuration类的loadResources()方法可以合并多个配置文件,后面的配置会覆盖前面的,除非前面的配置项被标记为final。直接运行Configuration.java将使用默认配置文件。
摘要由CSDN通过智能技术生成
   
   
   
     
     
     
       
       
       
         
         
         

         
         
         
  
  
  
    
    
    
      
      
      
        
        
        
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    <configuration>
      <property>
         <name>io.sort.factor</name>
         <value>10</value>
         <description>The number of streams to merge at once while sorting  
         files.  This determines the number of open file handles.</description>
      </property>
    <property>
         <name>dfs.name.dir</name>
         <value>${hadoop.tmp.dir}/dfs/name</value>
         <description>Determines where on the local filesystem the DFS name  
         nodeshould store the name table(fsimage).  ……</description>
      </property>
    <property>
         <name>dfs.web.ugi</name>
         <value>webuser,webgroup</value>
         <final>true</final>
         <description>The user account used by the web interface.  
         Syntax: USERNAME,GROUP1,GROUP2, ……</description>
      </property>
    </configuration>
复制代码

    Hadoop配置文件的根元素是configuration,一般只包含子元素property。每一个property元素就是一个配置 项,配置文件不支持分层或分级。每个配置项一般包括配置属性的名称name、值value和一个关于配置项的描述description;元素final 和Java中的关键字final类似,意味着这个配置项是“固定不变的”。final一般不出现,但在合并资源的时候,可以防止配置项的值被覆盖。
    在 上面的示例文件中,配置项dfs.web.ugi的值是“webuser,webgroup”,它是一个final配置项;从description看, 这个配置项配置了Hadoop Web界面的用户账号,包括用户名和用户组信息。这些信息可以通过Configuration类提供的方法访问。
    在 Configuration中,每个属性都是String类型的,但是值类型可能是以下多种类型,包括Java中的基本类型,如 boolean(getBoolean)、int(getInt)、long(getLong)、float(getFloat),也可以是其他类型,如 String(get)、java.io.File(getFile)、String数组(getStrings)等。以上面的配置文件为 例,getInt("io.sort.factor")将返回整数10;而getStrings("dfs.web.ugi")返回一个字符串数组,该数 组有两个元素,分别是webuser和webgroup。
    合并资源指将多个配置文件合并,产生一个配置。如果有两个配置文件,也就是两个资源,如core-default.xml和core-site.xml,通过Configuration类的loadResources()方法,把它们合并成一个配置。代码如下:
    Configurationconf = new Configuration();  
    conf.addResource("core-default.xml");  
    conf.addResource("core-site.xml");
    如 果这两个配置资源都包含了相同的配置项,而且前一个资源的配置项没有标记为final,那么,后面的配置将覆盖前面的配置。上面的例子中,core- site.xml中的配置将覆盖core-default.xml中的同名配置。如果在第一个资源(core-default.xml)中某配置项被标记 为final,那么,在加载第二个资源的时候,会有警告提示。

3 直接运行Configuration.java则会调用默认配置文件部分结果如下:

复制代码
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<configuration>
<property>
    <name>ipc.client.fallback-to-simple-auth-allowed</name>
    <value>false</value>
    <source>core-default.xml</source>
</property>
<property>
    <name>file.bytes-per-checksum</name>
    <value>512</value>
    <source>core-default.xml</source>
</property>
<property>
    <name>ipc.server.tcpnodelay</name>
    <value>false</value>
    <source>core-default.xml</source>
</property>
<property>
    <name>ftp.client-write-packet-size</name>
    <value>65536</value>
    <source>core-default.xml</source>
</property>
<property>
    <name>nfs3.mountd.port</name>
    <value>4272</value>
    <source>core-site.xml</source>
</property>
</configuration>
复制代码
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>io.sort.factor</name> <value>10</value> <description>The number of streams to merge at once while sorting files. This determines the number of open file handles.</description> </property> <property> <name>dfs.name.dir</name> <value>${hadoop.tmp.dir}/dfs/name</value> <description>Determines where on the local filesystem the DFS name nodeshould store the name table(fsimage). ……</description> </property> <property> <name>dfs.web.ugi</name> <value>webuser,webgroup</value> <final>true</final> <description>The user account used by the web interface. Syntax: USERNAME,GROUP1,GROUP2, ……</description> </property> </configuration>
复制代码

    Hadoop配置文件的根元素是configuration,一般只包含子元素property。每一个property元素就是一个配置 项,配置文件不支持分层或分级。每个配置项一般包括配置属性的名称name、值value和一个关于配置项的描述description;元素final 和Java中的关键字final类似,意味着这个配置项是“固定不变的”。final一般不出现,但在合并资源的时候,可以防止配置项的值被覆盖。
    在 上面的示例文件中,配置项dfs.web.ugi的值是“webuser,webgroup”,它是一个final配置项;从description看, 这个配置项配置了Hadoop Web界面的用户账号,包括用户名和用户组信息。这些信息可以通过Configuration类提供的方法访问。
    在 Configuration中,每个属性都是String类型的,但是值类型可能是以下多种类型,包括Java中的基本类型,如 boolean(getBoolean)、int(getInt)、long(getLong)、float(getFloat),也可以是其他类型,如 String(get)、java.io.File(getFile)、String数组(getStrings)等。以上面的配置文件为 例,getInt("io.sort.factor")将返回整数10;而getStrings("dfs.web.ugi")返回一个字符串数组,该数 组有两个元素,分别是webuser和webgroup。
    合并资源指将多个配置文件合并,产生一个配置。如果有两个配置文件,也就是两个资源,如core-default.xml和core-site.xml,通过Configuration类的loadResources()方法,把它们合并成一个配置。代码如下:
    Configurationconf = new Configuration();  
    conf.addResource("core-default.xml");  
    conf.addResource("core-site.xml");
    如 果这两个配置资源都包含了相同的配置项,而且前一个资源的配置项没有标记为final,那么,后面的配置将覆盖前面的配置。上面的例子中,core- site.xml中的配置将覆盖core-default.xml中的同名配置。如果在第一个资源(core-default.xml)中某配置项被标记 为final,那么,在加载第二个资源的时候,会有警告提示。

3 直接运行Configuration.java则会调用默认配置文件部分结果如下:

复制代码
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<configuration>
<property>
    <name>ipc.client.fallback-to-simple-auth-allowed</name>
    <value>false</value>
    <source>core-default.xml</source>
</property>
<property>
    <name>file.bytes-per-checksum</name>
    <value>512</value>
    <source>core-default.xml</source>
</property>
<property>
    <name>ipc.server.tcpnodelay</name>
    <value>false</value>
    <source>core-default.xml</source>
</property>
<property>
    <name>ftp.client-write-packet-size</name>
    <value>65536</value>
    <source>core-default.xml</source>
</property>
<property>
    <name>nfs3.mountd.port</name>
    <value>4272</value>
    <source>core-site.xml</source>
</property>
</configuration>
复制代码



    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    <configuration>
      <property>
         <name>io.sort.factor</name>
         <value>10</value>
         <description>The number of streams to merge at once while sorting  
         files.  This determines the number of open file handles.</description>
      </property>
    <property>
         <name>dfs.name.dir</name>
         <value>${hadoop.tmp.dir}/dfs/name</value>
         <description>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将多个文件合并一个 PDF 文件,可以使用第三方库 iTextPDF。以下是一个示例代码: 首先,确保你已将 iTextPDF 添加到你的项目中。你可以通过 Maven 或手动下载 JAR 文件的方式添加依赖。 ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfCopy; import com.itextpdf.text.pdf.PdfReader; import java.io.FileOutputStream; import java.io.IOException; public class PDFMerger { public static void main(String[] args) { String[] sourceFiles = {"path/to/file1.pdf", "path/to/file2.pdf", "path/to/file3.pdf"}; String targetFile = "path/to/merged.pdf"; mergePDFs(sourceFiles, targetFile); System.out.println("Files merged successfully."); } public static void mergePDFs(String[] sourceFiles, String targetFile) { Document document = new Document(); try { PdfCopy copy = new PdfCopy(document, new FileOutputStream(targetFile)); document.open(); for (String sourceFile : sourceFiles) { PdfReader reader = new PdfReader(sourceFile); copy.addDocument(reader); reader.close(); } copy.close(); document.close(); } catch (IOException | DocumentException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们首先指定了要合并的源文件的路径数组 `sourceFiles`,以及目标文件的路径 `targetFile`。然后,我们定义了一个 `mergePDFs` 方法来执行合并操作。 在 `mergePDFs` 方法中,我们首先创建一个 `Document` 对象,并使用 `PdfCopy` 对象将其与目标文件关联起来。然后,我们遍历源文件数组,对于每个源文件,我们创建一个 `PdfReader` 对象,并使用 `copy.addDocument` 方法将其添加到目标文件中。最后,我们关闭 `PdfCopy` 和 `Document` 对象。 请确保替换示例代码中的文件路径为你实际的文件路径,并根据你的项目配置进行必要的导入和依赖管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值