2.3 Configurable接口 Configurable是一个很简单的接口,也位于org.apache.hadoop.conf包中,其类图如图2-3所示。 从字面理解,Configurable的含义是可配置的,如果一个类实现了Configurable接口,意味着这个类是可配置的。也就是说,可以通过为这个类的对象
2.3 Configurable接口
Configurable是一个很简单的接口,也位于org.apache.hadoop.conf包中,其类图如图2-3所示。
从字面理解,Configurable的含义是可配置的,如果一个类实现了Configurable接口,意味着这个类是可配置的。也就是说,可以通过为这个类的对象传入一个Configuration实例,提供对象工作需要的一些配置信息。Hadoop的代码中有大量的类实现了Configurable接口,如org.apache.hadoop.mapred.SequenceFileInputFilter.RegexFilter。RegexFilter对象工作时,需要提供一个正则表达式,用于过滤读取的记录。由于RegexFilter的父类Filter中实现的Configurable接口,RegexFilter可以在它的setConf()方法中,使用Configuration.get()方法获取以字符串传入的正则表达式,并初始化成员变量p。相关代码如下:
<ol> <li><span>public void setConf(Configuration conf) { </span></li> <li><span> //在conf中获取键为"sequencefile.filter.regex"(FILTER_REGEX)的配置项 </span></li> <li> <span> String </span><span>regex</span><span> = </span><span>conf</span><span>.get(FILTER_REGEX); </span> </li> <li><span> </span></li> <li> <span> if (</span><span>regex</span><span>==null) </span> </li> <li><span> throw new RuntimeException(FILTER_REGEX + "not set"); </span></li> <li> <span> </span><span>this.p</span><span> = </span><span>Pattern</span><span>.compile(regex); </span> </li> <li> <span> </span><span>this.conf</span><span> = conf; </span> </li> <li><span>} </span></li> </ol>
Configurable.setConf()方法何时被调用呢?一般来说,对象创建以后,就应该使用setConf()方法,为对象提供进一步的初始化工作。为了简化对象创建和调用setConf()方法这两个连续的步骤,org.apache.hadoop.util.ReflectionUtils中提供了静态方法newInstance(),代码如下:
<ol><li><span><span>public static </span><span><span>T</span><span>></span><span> T newInstance(Class</span><span><span>T</span><span>></span><span>theClass, Configuration conf) </span></span></span></span></li></ol>
方法newInstance()利用Java反射机制,根据对象类型信息(参数theClass),创建一个新的相应类型的对象,然后调用ReflectionUtils中的另一个静态方法setConf()配置对象,代码如下:
<ol> <li><span>public static void setConf(Object theObject, Configuration conf) { </span></li> <li><span> if(conf != null) { </span></li> <li><span> //传入的对象实现了Configurable接口 </span></li> <li><span> if(theObject instanceof Configurable) { </span></li> <li><span> //调用对象的setConf方法,传入Configuration对象 </span></li> <li><span> ((Configurable) theObject).setConf(conf); </span></li> <li><span> } </span></li> <li><span> setJobConf(theObject, conf); </span></li> <li><span> } </span></li> <li><span>} </span></li> </ol>
在setConf()中,如果对象实现了Configurable接口,那么对象的setConf()方法会被调用,并根据Configuration类的实例conf进一步初始化对象。

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

Use the EXPLAIN command to analyze the execution plan of MySQL queries. 1. The EXPLAIN command displays the execution plan of the query to help find performance bottlenecks. 2. The execution plan includes fields such as id, select_type, table, type, possible_keys, key, key_len, ref, rows and Extra. 3. According to the execution plan, you can optimize queries by adding indexes, avoiding full table scans, optimizing JOIN operations, and using overlay indexes.

Subqueries can improve the efficiency of MySQL query. 1) Subquery simplifies complex query logic, such as filtering data and calculating aggregated values. 2) MySQL optimizer may convert subqueries to JOIN operations to improve performance. 3) Using EXISTS instead of IN can avoid multiple rows returning errors. 4) Optimization strategies include avoiding related subqueries, using EXISTS, index optimization, and avoiding subquery nesting.

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

Renaming a database in MySQL requires indirect methods. The steps are as follows: 1. Create a new database; 2. Use mysqldump to export the old database; 3. Import the data into the new database; 4. Delete the old database.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
