hadoop的程序运行有, 具体有3种, 除了上一篇(http://www.cnblogs.com/wenbronk/p/6662119.html)提过的直接发布jar上传到hadoop以外, 还有本地运行和真实服务器运行两种方式

1, 本地模式

1.1 解压hadoop.tar.gz到windows目录下

1.2 在hadoop/bin/下, 拷贝一个debug小工具, winutils.exe

1.3 修改项目jre为本地安装的jdk, 即 项目右键-properties-java build path ,

  remove掉原来的jre, 更改为本地安装的jdk

1.4 修改hadoop的源码文件, 即把java放在src下, 可被优先加载

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.io.nativeio;
  19.  
  20. import java.io.File;
  21. import java.io.FileDescriptor;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.RandomAccessFile;
  26. import java.lang.reflect.Field;
  27. import java.nio.ByteBuffer;
  28. import java.nio.MappedByteBuffer;
  29. import java.util.Map;
  30. import java.util.concurrent.ConcurrentHashMap;
  31.  
  32. import org.apache.hadoop.classification.InterfaceAudience;
  33. import org.apache.hadoop.classification.InterfaceStability;
  34. import org.apache.hadoop.conf.Configuration;
  35. import org.apache.hadoop.fs.CommonConfigurationKeys;
  36. import org.apache.hadoop.io.SecureIOUtils.AlreadyExistsException;
  37. import org.apache.hadoop.util.NativeCodeLoader;
  38. import org.apache.hadoop.util.Shell;
  39. import org.apache.commons.logging.Log;
  40. import org.apache.commons.logging.LogFactory;
  41.  
  42. import sun.misc.Unsafe;
  43.  
  44. import com.google.common.annotations.VisibleForTesting;
  45.  
  46. /**
  47. * JNI wrappers for various native IO-related calls not available in Java.
  48. * These functions should generally be used alongside a fallback to another
  49. * more portable mechanism.
  50. */
  51. @InterfaceAudience.Private
  52. @InterfaceStability.Unstable
  53. public class NativeIO {
  54. public static class POSIX {
  55. // Flags for open() call from bits/fcntl.h
  56. public static final int O_RDONLY = ;
  57. public static final int O_WRONLY = ;
  58. public static final int O_RDWR = ;
  59. public static final int O_CREAT = ;
  60. public static final int O_EXCL = ;
  61. public static final int O_NOCTTY = ;
  62. public static final int O_TRUNC = ;
  63. public static final int O_APPEND = ;
  64. public static final int O_NONBLOCK = ;
  65. public static final int O_SYNC = ;
  66. public static final int O_ASYNC = ;
  67. public static final int O_FSYNC = O_SYNC;
  68. public static final int O_NDELAY = O_NONBLOCK;
  69.  
  70. // Flags for posix_fadvise() from bits/fcntl.h
  71. /* No further special treatment. */
  72. public static final int POSIX_FADV_NORMAL = ;
  73. /* Expect random page references. */
  74. public static final int POSIX_FADV_RANDOM = ;
  75. /* Expect sequential page references. */
  76. public static final int POSIX_FADV_SEQUENTIAL = ;
  77. /* Will need these pages. */
  78. public static final int POSIX_FADV_WILLNEED = ;
  79. /* Don't need these pages. */
  80. public static final int POSIX_FADV_DONTNEED = ;
  81. /* Data will be accessed once. */
  82. public static final int POSIX_FADV_NOREUSE = ;
  83.  
  84. /* Wait upon writeout of all pages
  85. in the range before performing the
  86. write. */
  87. public static final int SYNC_FILE_RANGE_WAIT_BEFORE = ;
  88. /* Initiate writeout of all those
  89. dirty pages in the range which are
  90. not presently under writeback. */
  91. public static final int SYNC_FILE_RANGE_WRITE = ;
  92.  
  93. /* Wait upon writeout of all pages in
  94. the range after performing the
  95. write. */
  96. public static final int SYNC_FILE_RANGE_WAIT_AFTER = ;
  97.  
  98. private static final Log LOG = LogFactory.getLog(NativeIO.class);
  99.  
  100. private static boolean nativeLoaded = false;
  101. private static boolean fadvisePossible = true;
  102. private static boolean syncFileRangePossible = true;
  103.  
  104. static final String WORKAROUND_NON_THREADSAFE_CALLS_KEY =
  105. "hadoop.workaround.non.threadsafe.getpwuid";
  106. static final boolean WORKAROUND_NON_THREADSAFE_CALLS_DEFAULT = true;
  107.  
  108. private static long cacheTimeout = -;
  109.  
  110. private static CacheManipulator cacheManipulator = new CacheManipulator();
  111.  
  112. public static CacheManipulator getCacheManipulator() {
  113. return cacheManipulator;
  114. }
  115.  
  116. public static void setCacheManipulator(CacheManipulator cacheManipulator) {
  117. POSIX.cacheManipulator = cacheManipulator;
  118. }
  119.  
  120. /**
  121. * Used to manipulate the operating system cache.
  122. */
  123. @VisibleForTesting
  124. public static class CacheManipulator {
  125. public void mlock(String identifier, ByteBuffer buffer,
  126. long len) throws IOException {
  127. POSIX.mlock(buffer, len);
  128. }
  129.  
  130. public long getMemlockLimit() {
  131. return NativeIO.getMemlockLimit();
  132. }
  133.  
  134. public long getOperatingSystemPageSize() {
  135. return NativeIO.getOperatingSystemPageSize();
  136. }
  137.  
  138. public void posixFadviseIfPossible(String identifier,
  139. FileDescriptor fd, long offset, long len, int flags)
  140. throws NativeIOException {
  141. NativeIO.POSIX.posixFadviseIfPossible(identifier, fd, offset,
  142. len, flags);
  143. }
  144.  
  145. public boolean verifyCanMlock() {
  146. return NativeIO.isAvailable();
  147. }
  148. }
  149.  
  150. /**
  151. * A CacheManipulator used for testing which does not actually call mlock.
  152. * This allows many tests to be run even when the operating system does not
  153. * allow mlock, or only allows limited mlocking.
  154. */
  155. @VisibleForTesting
  156. public static class NoMlockCacheManipulator extends CacheManipulator {
  157. public void mlock(String identifier, ByteBuffer buffer,
  158. long len) throws IOException {
  159. LOG.info("mlocking " + identifier);
  160. }
  161.  
  162. public long getMemlockLimit() {
  163. return 1125899906842624L;
  164. }
  165.  
  166. public long getOperatingSystemPageSize() {
  167. return ;
  168. }
  169.  
  170. public boolean verifyCanMlock() {
  171. return true;
  172. }
  173. }
  174.  
  175. static {
  176. if (NativeCodeLoader.isNativeCodeLoaded()) {
  177. try {
  178. Configuration conf = new Configuration();
  179. workaroundNonThreadSafePasswdCalls = conf.getBoolean(
  180. WORKAROUND_NON_THREADSAFE_CALLS_KEY,
  181. WORKAROUND_NON_THREADSAFE_CALLS_DEFAULT);
  182.  
  183. initNative();
  184. nativeLoaded = true;
  185.  
  186. cacheTimeout = conf.getLong(
  187. CommonConfigurationKeys.HADOOP_SECURITY_UID_NAME_CACHE_TIMEOUT_KEY,
  188. CommonConfigurationKeys.HADOOP_SECURITY_UID_NAME_CACHE_TIMEOUT_DEFAULT) *
  189. ;
  190. LOG.debug("Initialized cache for IDs to User/Group mapping with a " +
  191. " cache timeout of " + cacheTimeout/ + " seconds.");
  192.  
  193. } catch (Throwable t) {
  194. // This can happen if the user has an older version of libhadoop.so
  195. // installed - in this case we can continue without native IO
  196. // after warning
  197. LOG.error("Unable to initialize NativeIO libraries", t);
  198. }
  199. }
  200. }
  201.  
  202. /**
  203. * Return true if the JNI-based native IO extensions are available.
  204. */
  205. public static boolean isAvailable() {
  206. return NativeCodeLoader.isNativeCodeLoaded() && nativeLoaded;
  207. }
  208.  
  209. private static void assertCodeLoaded() throws IOException {
  210. if (!isAvailable()) {
  211. throw new IOException("NativeIO was not loaded");
  212. }
  213. }
  214.  
  215. /** Wrapper around open(2) */
  216. public static native FileDescriptor open(String path, int flags, int mode) throws IOException;
  217. /** Wrapper around fstat(2) */
  218. private static native Stat fstat(FileDescriptor fd) throws IOException;
  219.  
  220. /** Native chmod implementation. On UNIX, it is a wrapper around chmod(2) */
  221. private static native void chmodImpl(String path, int mode) throws IOException;
  222.  
  223. public static void chmod(String path, int mode) throws IOException {
  224. if (!Shell.WINDOWS) {
  225. chmodImpl(path, mode);
  226. } else {
  227. try {
  228. chmodImpl(path, mode);
  229. } catch (NativeIOException nioe) {
  230. if (nioe.getErrorCode() == ) {
  231. throw new NativeIOException("No such file or directory",
  232. Errno.ENOENT);
  233. } else {
  234. LOG.warn(String.format("NativeIO.chmod error (%d): %s",
  235. nioe.getErrorCode(), nioe.getMessage()));
  236. throw new NativeIOException("Unknown error", Errno.UNKNOWN);
  237. }
  238. }
  239. }
  240. }
  241.  
  242. /** Wrapper around posix_fadvise(2) */
  243. static native void posix_fadvise(
  244. FileDescriptor fd, long offset, long len, int flags) throws NativeIOException;
  245.  
  246. /** Wrapper around sync_file_range(2) */
  247. static native void sync_file_range(
  248. FileDescriptor fd, long offset, long nbytes, int flags) throws NativeIOException;
  249.  
  250. /**
  251. * Call posix_fadvise on the given file descriptor. See the manpage
  252. * for this syscall for more information. On systems where this
  253. * call is not available, does nothing.
  254. *
  255. * @throws NativeIOException if there is an error with the syscall
  256. */
  257. static void posixFadviseIfPossible(String identifier,
  258. FileDescriptor fd, long offset, long len, int flags)
  259. throws NativeIOException {
  260. if (nativeLoaded && fadvisePossible) {
  261. try {
  262. posix_fadvise(fd, offset, len, flags);
  263. } catch (UnsupportedOperationException uoe) {
  264. fadvisePossible = false;
  265. } catch (UnsatisfiedLinkError ule) {
  266. fadvisePossible = false;
  267. }
  268. }
  269. }
  270.  
  271. /**
  272. * Call sync_file_range on the given file descriptor. See the manpage
  273. * for this syscall for more information. On systems where this
  274. * call is not available, does nothing.
  275. *
  276. * @throws NativeIOException if there is an error with the syscall
  277. */
  278. public static void syncFileRangeIfPossible(
  279. FileDescriptor fd, long offset, long nbytes, int flags)
  280. throws NativeIOException {
  281. if (nativeLoaded && syncFileRangePossible) {
  282. try {
  283. sync_file_range(fd, offset, nbytes, flags);
  284. } catch (UnsupportedOperationException uoe) {
  285. syncFileRangePossible = false;
  286. } catch (UnsatisfiedLinkError ule) {
  287. syncFileRangePossible = false;
  288. }
  289. }
  290. }
  291.  
  292. static native void mlock_native(
  293. ByteBuffer buffer, long len) throws NativeIOException;
  294. static native void munlock_native(
  295. ByteBuffer buffer, long len) throws NativeIOException;
  296.  
  297. /**
  298. * Locks the provided direct ByteBuffer into memory, preventing it from
  299. * swapping out. After a buffer is locked, future accesses will not incur
  300. * a page fault.
  301. *
  302. * See the mlock(2) man page for more information.
  303. *
  304. * @throws NativeIOException
  305. */
  306. static void mlock(ByteBuffer buffer, long len)
  307. throws IOException {
  308. assertCodeLoaded();
  309. if (!buffer.isDirect()) {
  310. throw new IOException("Cannot mlock a non-direct ByteBuffer");
  311. }
  312. mlock_native(buffer, len);
  313. }
  314.  
  315. /**
  316. * Unlocks a locked direct ByteBuffer, allowing it to swap out of memory.
  317. * This is a no-op if the ByteBuffer was not previously locked.
  318. *
  319. * See the munlock(2) man page for more information.
  320. *
  321. * @throws NativeIOException
  322. */
  323. public static void munlock(ByteBuffer buffer, long len)
  324. throws IOException {
  325. assertCodeLoaded();
  326. if (!buffer.isDirect()) {
  327. throw new IOException("Cannot munlock a non-direct ByteBuffer");
  328. }
  329. munlock_native(buffer, len);
  330. }
  331.  
  332. /**
  333. * Unmaps the block from memory. See munmap(2).
  334. *
  335. * There isn't any portable way to unmap a memory region in Java.
  336. * So we use the sun.nio method here.
  337. * Note that unmapping a memory region could cause crashes if code
  338. * continues to reference the unmapped code. However, if we don't
  339. * manually unmap the memory, we are dependent on the finalizer to
  340. * do it, and we have no idea when the finalizer will run.
  341. *
  342. * @param buffer The buffer to unmap.
  343. */
  344. public static void munmap(MappedByteBuffer buffer) {
  345. if (buffer instanceof sun.nio.ch.DirectBuffer) {
  346. sun.misc.Cleaner cleaner =
  347. ((sun.nio.ch.DirectBuffer)buffer).cleaner();
  348. cleaner.clean();
  349. }
  350. }
  351.  
  352. /** Linux only methods used for getOwner() implementation */
  353. private static native long getUIDforFDOwnerforOwner(FileDescriptor fd) throws IOException;
  354. private static native String getUserName(long uid) throws IOException;
  355.  
  356. /**
  357. * Result type of the fstat call
  358. */
  359. public static class Stat {
  360. private int ownerId, groupId;
  361. private String owner, group;
  362. private int mode;
  363.  
  364. // Mode constants
  365. public static final int S_IFMT = ; /* type of file */
  366. public static final int S_IFIFO = ; /* named pipe (fifo) */
  367. public static final int S_IFCHR = ; /* character special */
  368. public static final int S_IFDIR = ; /* directory */
  369. public static final int S_IFBLK = ; /* block special */
  370. public static final int S_IFREG = ; /* regular */
  371. public static final int S_IFLNK = ; /* symbolic link */
  372. public static final int S_IFSOCK = ; /* socket */
  373. public static final int S_IFWHT = ; /* whiteout */
  374. public static final int S_ISUID = ; /* set user id on execution */
  375. public static final int S_ISGID = ; /* set group id on execution */
  376. public static final int S_ISVTX = ; /* save swapped text even after use */
  377. public static final int S_IRUSR = ; /* read permission, owner */
  378. public static final int S_IWUSR = ; /* write permission, owner */
  379. public static final int S_IXUSR = ; /* execute/search permission, owner */
  380.  
  381. Stat(int ownerId, int groupId, int mode) {
  382. this.ownerId = ownerId;
  383. this.groupId = groupId;
  384. this.mode = mode;
  385. }
  386.  
  387. Stat(String owner, String group, int mode) {
  388. if (!Shell.WINDOWS) {
  389. this.owner = owner;
  390. } else {
  391. this.owner = stripDomain(owner);
  392. }
  393. if (!Shell.WINDOWS) {
  394. this.group = group;
  395. } else {
  396. this.group = stripDomain(group);
  397. }
  398. this.mode = mode;
  399. }
  400.  
  401. @Override
  402. public String toString() {
  403. return "Stat(owner='" + owner + "', group='" + group + "'" +
  404. ", mode=" + mode + ")";
  405. }
  406.  
  407. public String getOwner() {
  408. return owner;
  409. }
  410. public String getGroup() {
  411. return group;
  412. }
  413. public int getMode() {
  414. return mode;
  415. }
  416. }
  417.  
  418. /**
  419. * Returns the file stat for a file descriptor.
  420. *
  421. * @param fd file descriptor.
  422. * @return the file descriptor file stat.
  423. * @throws IOException thrown if there was an IO error while obtaining the file stat.
  424. */
  425. public static Stat getFstat(FileDescriptor fd) throws IOException {
  426. Stat stat = null;
  427. if (!Shell.WINDOWS) {
  428. stat = fstat(fd);
  429. stat.owner = getName(IdCache.USER, stat.ownerId);
  430. stat.group = getName(IdCache.GROUP, stat.groupId);
  431. } else {
  432. try {
  433. stat = fstat(fd);
  434. } catch (NativeIOException nioe) {
  435. if (nioe.getErrorCode() == ) {
  436. throw new NativeIOException("The handle is invalid.",
  437. Errno.EBADF);
  438. } else {
  439. LOG.warn(String.format("NativeIO.getFstat error (%d): %s",
  440. nioe.getErrorCode(), nioe.getMessage()));
  441. throw new NativeIOException("Unknown error", Errno.UNKNOWN);
  442. }
  443. }
  444. }
  445. return stat;
  446. }
  447.  
  448. private static String getName(IdCache domain, int id) throws IOException {
  449. Map<Integer, CachedName> idNameCache = (domain == IdCache.USER)
  450. ? USER_ID_NAME_CACHE : GROUP_ID_NAME_CACHE;
  451. String name;
  452. CachedName cachedName = idNameCache.get(id);
  453. long now = System.currentTimeMillis();
  454. if (cachedName != null && (cachedName.timestamp + cacheTimeout) > now) {
  455. name = cachedName.name;
  456. } else {
  457. name = (domain == IdCache.USER) ? getUserName(id) : getGroupName(id);
  458. if (LOG.isDebugEnabled()) {
  459. String type = (domain == IdCache.USER) ? "UserName" : "GroupName";
  460. LOG.debug("Got " + type + " " + name + " for ID " + id +
  461. " from the native implementation");
  462. }
  463. cachedName = new CachedName(name, now);
  464. idNameCache.put(id, cachedName);
  465. }
  466. return name;
  467. }
  468.  
  469. static native String getUserName(int uid) throws IOException;
  470. static native String getGroupName(int uid) throws IOException;
  471.  
  472. private static class CachedName {
  473. final long timestamp;
  474. final String name;
  475.  
  476. public CachedName(String name, long timestamp) {
  477. this.name = name;
  478. this.timestamp = timestamp;
  479. }
  480. }
  481.  
  482. private static final Map<Integer, CachedName> USER_ID_NAME_CACHE =
  483. new ConcurrentHashMap<Integer, CachedName>();
  484.  
  485. private static final Map<Integer, CachedName> GROUP_ID_NAME_CACHE =
  486. new ConcurrentHashMap<Integer, CachedName>();
  487.  
  488. private enum IdCache { USER, GROUP }
  489.  
  490. public final static int MMAP_PROT_READ = 0x1;
  491. public final static int MMAP_PROT_WRITE = 0x2;
  492. public final static int MMAP_PROT_EXEC = 0x4;
  493.  
  494. public static native long mmap(FileDescriptor fd, int prot,
  495. boolean shared, long length) throws IOException;
  496.  
  497. public static native void munmap(long addr, long length)
  498. throws IOException;
  499. }
  500.  
  501. private static boolean workaroundNonThreadSafePasswdCalls = false;
  502.  
  503. public static class Windows {
  504. // Flags for CreateFile() call on Windows
  505. public static final long GENERIC_READ = 0x80000000L;
  506. public static final long GENERIC_WRITE = 0x40000000L;
  507.  
  508. public static final long FILE_SHARE_READ = 0x00000001L;
  509. public static final long FILE_SHARE_WRITE = 0x00000002L;
  510. public static final long FILE_SHARE_DELETE = 0x00000004L;
  511.  
  512. public static final long CREATE_NEW = ;
  513. public static final long CREATE_ALWAYS = ;
  514. public static final long OPEN_EXISTING = ;
  515. public static final long OPEN_ALWAYS = ;
  516. public static final long TRUNCATE_EXISTING = ;
  517.  
  518. public static final long FILE_BEGIN = ;
  519. public static final long FILE_CURRENT = ;
  520. public static final long FILE_END = ;
  521.  
  522. /** Wrapper around CreateFile() on Windows */
  523. public static native FileDescriptor createFile(String path,
  524. long desiredAccess, long shareMode, long creationDisposition)
  525. throws IOException;
  526.  
  527. /** Wrapper around SetFilePointer() on Windows */
  528. public static native long setFilePointer(FileDescriptor fd,
  529. long distanceToMove, long moveMethod) throws IOException;
  530.  
  531. /** Windows only methods used for getOwner() implementation */
  532. private static native String getOwner(FileDescriptor fd) throws IOException;
  533.  
  534. /** Supported list of Windows access right flags */
  535. public static enum AccessRight {
  536. ACCESS_READ (0x0001), // FILE_READ_DATA
  537. ACCESS_WRITE (0x0002), // FILE_WRITE_DATA
  538. ACCESS_EXECUTE (0x0020); // FILE_EXECUTE
  539.  
  540. private final int accessRight;
  541. AccessRight(int access) {
  542. accessRight = access;
  543. }
  544.  
  545. public int accessRight() {
  546. return accessRight;
  547. }
  548. };
  549.  
  550. /** Windows only method used to check if the current process has requested
  551. * access rights on the given path. */
  552. private static native boolean access0(String path, int requestedAccess);
  553.  
  554. /**
  555. * Checks whether the current process has desired access rights on
  556. * the given path.
  557. *
  558. * Longer term this native function can be substituted with JDK7
  559. * function Files#isReadable, isWritable, isExecutable.
  560. *
  561. * @param path input path
  562. * @param desiredAccess ACCESS_READ, ACCESS_WRITE or ACCESS_EXECUTE
  563. * @return true if access is allowed
  564. * @throws IOException I/O exception on error
  565. */
  566. public static boolean access(String path, AccessRight desiredAccess)
  567. throws IOException {
  568. return true;
  569. //return access0(path, desiredAccess.accessRight());
  570. }
  571.  
  572. static {
  573. if (NativeCodeLoader.isNativeCodeLoaded()) {
  574. try {
  575. initNative();
  576. nativeLoaded = true;
  577. } catch (Throwable t) {
  578. // This can happen if the user has an older version of libhadoop.so
  579. // installed - in this case we can continue without native IO
  580. // after warning
  581. LOG.error("Unable to initialize NativeIO libraries", t);
  582. }
  583. }
  584. }
  585. }
  586.  
  587. private static final Log LOG = LogFactory.getLog(NativeIO.class);
  588.  
  589. private static boolean nativeLoaded = false;
  590.  
  591. static {
  592. if (NativeCodeLoader.isNativeCodeLoaded()) {
  593. try {
  594. initNative();
  595. nativeLoaded = true;
  596. } catch (Throwable t) {
  597. // This can happen if the user has an older version of libhadoop.so
  598. // installed - in this case we can continue without native IO
  599. // after warning
  600. LOG.error("Unable to initialize NativeIO libraries", t);
  601. }
  602. }
  603. }
  604.  
  605. /**
  606. * Return true if the JNI-based native IO extensions are available.
  607. */
  608. public static boolean isAvailable() {
  609. return NativeCodeLoader.isNativeCodeLoaded() && nativeLoaded;
  610. }
  611.  
  612. /** Initialize the JNI method ID and class ID cache */
  613. private static native void initNative();
  614.  
  615. /**
  616. * Get the maximum number of bytes that can be locked into memory at any
  617. * given point.
  618. *
  619. * @return 0 if no bytes can be locked into memory;
  620. * Long.MAX_VALUE if there is no limit;
  621. * The number of bytes that can be locked into memory otherwise.
  622. */
  623. static long getMemlockLimit() {
  624. return isAvailable() ? getMemlockLimit0() : ;
  625. }
  626.  
  627. private static native long getMemlockLimit0();
  628.  
  629. /**
  630. * @return the operating system's page size.
  631. */
  632. static long getOperatingSystemPageSize() {
  633. try {
  634. Field f = Unsafe.class.getDeclaredField("theUnsafe");
  635. f.setAccessible(true);
  636. Unsafe unsafe = (Unsafe)f.get(null);
  637. return unsafe.pageSize();
  638. } catch (Throwable e) {
  639. LOG.warn("Unable to get operating system page size. Guessing 4096.", e);
  640. return ;
  641. }
  642. }
  643.  
  644. private static class CachedUid {
  645. final long timestamp;
  646. final String username;
  647. public CachedUid(String username, long timestamp) {
  648. this.timestamp = timestamp;
  649. this.username = username;
  650. }
  651. }
  652. private static final Map<Long, CachedUid> uidCache =
  653. new ConcurrentHashMap<Long, CachedUid>();
  654. private static long cacheTimeout;
  655. private static boolean initialized = false;
  656.  
  657. /**
  658. * The Windows logon name has two part, NetBIOS domain name and
  659. * user account name, of the format DOMAIN\UserName. This method
  660. * will remove the domain part of the full logon name.
  661. *
  662. * @param the full principal name containing the domain
  663. * @return name with domain removed
  664. */
  665. private static String stripDomain(String name) {
  666. int i = name.indexOf('\\');
  667. if (i != -)
  668. name = name.substring(i + );
  669. return name;
  670. }
  671.  
  672. public static String getOwner(FileDescriptor fd) throws IOException {
  673. ensureInitialized();
  674. if (Shell.WINDOWS) {
  675. String owner = Windows.getOwner(fd);
  676. owner = stripDomain(owner);
  677. return owner;
  678. } else {
  679. long uid = POSIX.getUIDforFDOwnerforOwner(fd);
  680. CachedUid cUid = uidCache.get(uid);
  681. long now = System.currentTimeMillis();
  682. if (cUid != null && (cUid.timestamp + cacheTimeout) > now) {
  683. return cUid.username;
  684. }
  685. String user = POSIX.getUserName(uid);
  686. LOG.info("Got UserName " + user + " for UID " + uid
  687. + " from the native implementation");
  688. cUid = new CachedUid(user, now);
  689. uidCache.put(uid, cUid);
  690. return user;
  691. }
  692. }
  693.  
  694. /**
  695. * Create a FileInputStream that shares delete permission on the
  696. * file opened, i.e. other process can delete the file the
  697. * FileInputStream is reading. Only Windows implementation uses
  698. * the native interface.
  699. */
  700. public static FileInputStream getShareDeleteFileInputStream(File f)
  701. throws IOException {
  702. if (!Shell.WINDOWS) {
  703. // On Linux the default FileInputStream shares delete permission
  704. // on the file opened.
  705. //
  706. return new FileInputStream(f);
  707. } else {
  708. // Use Windows native interface to create a FileInputStream that
  709. // shares delete permission on the file opened.
  710. //
  711. FileDescriptor fd = Windows.createFile(
  712. f.getAbsolutePath(),
  713. Windows.GENERIC_READ,
  714. Windows.FILE_SHARE_READ |
  715. Windows.FILE_SHARE_WRITE |
  716. Windows.FILE_SHARE_DELETE,
  717. Windows.OPEN_EXISTING);
  718. return new FileInputStream(fd);
  719. }
  720. }
  721.  
  722. /**
  723. * Create a FileInputStream that shares delete permission on the
  724. * file opened at a given offset, i.e. other process can delete
  725. * the file the FileInputStream is reading. Only Windows implementation
  726. * uses the native interface.
  727. */
  728. public static FileInputStream getShareDeleteFileInputStream(File f, long seekOffset)
  729. throws IOException {
  730. if (!Shell.WINDOWS) {
  731. RandomAccessFile rf = new RandomAccessFile(f, "r");
  732. if (seekOffset > ) {
  733. rf.seek(seekOffset);
  734. }
  735. return new FileInputStream(rf.getFD());
  736. } else {
  737. // Use Windows native interface to create a FileInputStream that
  738. // shares delete permission on the file opened, and set it to the
  739. // given offset.
  740. //
  741. FileDescriptor fd = NativeIO.Windows.createFile(
  742. f.getAbsolutePath(),
  743. NativeIO.Windows.GENERIC_READ,
  744. NativeIO.Windows.FILE_SHARE_READ |
  745. NativeIO.Windows.FILE_SHARE_WRITE |
  746. NativeIO.Windows.FILE_SHARE_DELETE,
  747. NativeIO.Windows.OPEN_EXISTING);
  748. if (seekOffset > )
  749. NativeIO.Windows.setFilePointer(fd, seekOffset, NativeIO.Windows.FILE_BEGIN);
  750. return new FileInputStream(fd);
  751. }
  752. }
  753.  
  754. /**
  755. * Create the specified File for write access, ensuring that it does not exist.
  756. * @param f the file that we want to create
  757. * @param permissions we want to have on the file (if security is enabled)
  758. *
  759. * @throws AlreadyExistsException if the file already exists
  760. * @throws IOException if any other error occurred
  761. */
  762. public static FileOutputStream getCreateForWriteFileOutputStream(File f, int permissions)
  763. throws IOException {
  764. if (!Shell.WINDOWS) {
  765. // Use the native wrapper around open(2)
  766. try {
  767. FileDescriptor fd = NativeIO.POSIX.open(f.getAbsolutePath(),
  768. NativeIO.POSIX.O_WRONLY | NativeIO.POSIX.O_CREAT
  769. | NativeIO.POSIX.O_EXCL, permissions);
  770. return new FileOutputStream(fd);
  771. } catch (NativeIOException nioe) {
  772. if (nioe.getErrno() == Errno.EEXIST) {
  773. throw new AlreadyExistsException(nioe);
  774. }
  775. throw nioe;
  776. }
  777. } else {
  778. // Use the Windows native APIs to create equivalent FileOutputStream
  779. try {
  780. FileDescriptor fd = NativeIO.Windows.createFile(f.getCanonicalPath(),
  781. NativeIO.Windows.GENERIC_WRITE,
  782. NativeIO.Windows.FILE_SHARE_DELETE
  783. | NativeIO.Windows.FILE_SHARE_READ
  784. | NativeIO.Windows.FILE_SHARE_WRITE,
  785. NativeIO.Windows.CREATE_NEW);
  786. NativeIO.POSIX.chmod(f.getCanonicalPath(), permissions);
  787. return new FileOutputStream(fd);
  788. } catch (NativeIOException nioe) {
  789. if (nioe.getErrorCode() == ) {
  790. // ERROR_FILE_EXISTS
  791. // 80 (0x50)
  792. // The file exists
  793. throw new AlreadyExistsException(nioe);
  794. }
  795. throw nioe;
  796. }
  797. }
  798. }
  799.  
  800. private synchronized static void ensureInitialized() {
  801. if (!initialized) {
  802. cacheTimeout =
  803. new Configuration().getLong("hadoop.security.uid.cache.secs",
  804. **) * ;
  805. LOG.info("Initialized cache for UID to User mapping with a cache" +
  806. " timeout of " + cacheTimeout/ + " seconds.");
  807. initialized = true;
  808. }
  809. }
  810.  
  811. /**
  812. * A version of renameTo that throws a descriptive exception when it fails.
  813. *
  814. * @param src The source path
  815. * @param dst The destination path
  816. *
  817. * @throws NativeIOException On failure.
  818. */
  819. public static void renameTo(File src, File dst)
  820. throws IOException {
  821. if (!nativeLoaded) {
  822. if (!src.renameTo(dst)) {
  823. throw new IOException("renameTo(src=" + src + ", dst=" +
  824. dst + ") failed.");
  825. }
  826. } else {
  827. renameTo0(src.getAbsolutePath(), dst.getAbsolutePath());
  828. }
  829. }
  830.  
  831. /**
  832. * A version of renameTo that throws a descriptive exception when it fails.
  833. *
  834. * @param src The source path
  835. * @param dst The destination path
  836. *
  837. * @throws NativeIOException On failure.
  838. */
  839. private static native void renameTo0(String src, String dst)
  840. throws NativeIOException;
  841. }

org.apache.hadoop.io.nativeio.NativeIO.java

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18.  
  19. package org.apache.hadoop.mapred;
  20.  
  21. import java.io.IOException;
  22. import java.nio.ByteBuffer;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Vector;
  30.  
  31. import org.apache.commons.logging.Log;
  32. import org.apache.commons.logging.LogFactory;
  33. import org.apache.hadoop.classification.InterfaceAudience.Private;
  34. import org.apache.hadoop.conf.Configuration;
  35. import org.apache.hadoop.fs.FileContext;
  36. import org.apache.hadoop.fs.FileStatus;
  37. import org.apache.hadoop.fs.Path;
  38. import org.apache.hadoop.fs.UnsupportedFileSystemException;
  39. import org.apache.hadoop.io.DataOutputBuffer;
  40. import org.apache.hadoop.io.Text;
  41. import org.apache.hadoop.ipc.ProtocolSignature;
  42. import org.apache.hadoop.mapred.ClientCache;
  43. import org.apache.hadoop.mapred.JobConf;
  44. import org.apache.hadoop.mapred.Master;
  45. import org.apache.hadoop.mapred.ResourceMgrDelegate;
  46. import org.apache.hadoop.mapred.YARNRunner;
  47. import org.apache.hadoop.mapreduce.Cluster.JobTrackerStatus;
  48. import org.apache.hadoop.mapreduce.ClusterMetrics;
  49. import org.apache.hadoop.mapreduce.Counters;
  50. import org.apache.hadoop.mapreduce.JobContext;
  51. import org.apache.hadoop.mapreduce.JobID;
  52. import org.apache.hadoop.mapreduce.JobStatus;
  53. import org.apache.hadoop.mapreduce.MRJobConfig;
  54. import org.apache.hadoop.mapreduce.QueueAclsInfo;
  55. import org.apache.hadoop.mapreduce.QueueInfo;
  56. import org.apache.hadoop.mapreduce.TaskAttemptID;
  57. import org.apache.hadoop.mapreduce.TaskCompletionEvent;
  58. import org.apache.hadoop.mapreduce.TaskReport;
  59. import org.apache.hadoop.mapreduce.TaskTrackerInfo;
  60. import org.apache.hadoop.mapreduce.TaskType;
  61. import org.apache.hadoop.mapreduce.TypeConverter;
  62. import org.apache.hadoop.mapreduce.protocol.ClientProtocol;
  63. import org.apache.hadoop.mapreduce.security.token.delegation.DelegationTokenIdentifier;
  64. import org.apache.hadoop.mapreduce.v2.LogParams;
  65. import org.apache.hadoop.mapreduce.v2.api.MRClientProtocol;
  66. import org.apache.hadoop.mapreduce.v2.api.protocolrecords.GetDelegationTokenRequest;
  67. import org.apache.hadoop.mapreduce.v2.jobhistory.JobHistoryUtils;
  68. import org.apache.hadoop.mapreduce.v2.util.MRApps;
  69. import org.apache.hadoop.security.Credentials;
  70. import org.apache.hadoop.security.SecurityUtil;
  71. import org.apache.hadoop.security.UserGroupInformation;
  72. import org.apache.hadoop.security.authorize.AccessControlList;
  73. import org.apache.hadoop.security.token.Token;
  74. import org.apache.hadoop.yarn.api.ApplicationConstants;
  75. import org.apache.hadoop.yarn.api.ApplicationConstants.Environment;
  76. import org.apache.hadoop.yarn.api.records.ApplicationAccessType;
  77. import org.apache.hadoop.yarn.api.records.ApplicationId;
  78. import org.apache.hadoop.yarn.api.records.ApplicationReport;
  79. import org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext;
  80. import org.apache.hadoop.yarn.api.records.ContainerLaunchContext;
  81. import org.apache.hadoop.yarn.api.records.LocalResource;
  82. import org.apache.hadoop.yarn.api.records.LocalResourceType;
  83. import org.apache.hadoop.yarn.api.records.LocalResourceVisibility;
  84. import org.apache.hadoop.yarn.api.records.Resource;
  85. import org.apache.hadoop.yarn.api.records.URL;
  86. import org.apache.hadoop.yarn.api.records.YarnApplicationState;
  87. import org.apache.hadoop.yarn.conf.YarnConfiguration;
  88. import org.apache.hadoop.yarn.exceptions.YarnException;
  89. import org.apache.hadoop.yarn.factories.RecordFactory;
  90. import org.apache.hadoop.yarn.factory.providers.RecordFactoryProvider;
  91. import org.apache.hadoop.yarn.security.client.RMDelegationTokenSelector;
  92. import org.apache.hadoop.yarn.util.ConverterUtils;
  93.  
  94. import com.google.common.annotations.VisibleForTesting;
  95.  
  96. /**
  97. * This class enables the current JobClient (0.22 hadoop) to run on YARN.
  98. */
  99. @SuppressWarnings("unchecked")
  100. public class YARNRunner implements ClientProtocol {
  101.  
  102. private static final Log LOG = LogFactory.getLog(YARNRunner.class);
  103.  
  104. private final RecordFactory recordFactory = RecordFactoryProvider.getRecordFactory(null);
  105. private ResourceMgrDelegate resMgrDelegate;
  106. private ClientCache clientCache;
  107. private Configuration conf;
  108. private final FileContext defaultFileContext;
  109.  
  110. /**
  111. * Yarn runner incapsulates the client interface of
  112. * yarn
  113. * @param conf the configuration object for the client
  114. */
  115. public YARNRunner(Configuration conf) {
  116. this(conf, new ResourceMgrDelegate(new YarnConfiguration(conf)));
  117. }
  118.  
  119. /**
  120. * Similar to {@link #YARNRunner(Configuration)} but allowing injecting
  121. * {@link ResourceMgrDelegate}. Enables mocking and testing.
  122. * @param conf the configuration object for the client
  123. * @param resMgrDelegate the resourcemanager client handle.
  124. */
  125. public YARNRunner(Configuration conf, ResourceMgrDelegate resMgrDelegate) {
  126. this(conf, resMgrDelegate, new ClientCache(conf, resMgrDelegate));
  127. }
  128.  
  129. /**
  130. * Similar to {@link YARNRunner#YARNRunner(Configuration, ResourceMgrDelegate)}
  131. * but allowing injecting {@link ClientCache}. Enable mocking and testing.
  132. * @param conf the configuration object
  133. * @param resMgrDelegate the resource manager delegate
  134. * @param clientCache the client cache object.
  135. */
  136. public YARNRunner(Configuration conf, ResourceMgrDelegate resMgrDelegate,
  137. ClientCache clientCache) {
  138. this.conf = conf;
  139. try {
  140. this.resMgrDelegate = resMgrDelegate;
  141. this.clientCache = clientCache;
  142. this.defaultFileContext = FileContext.getFileContext(this.conf);
  143. } catch (UnsupportedFileSystemException ufe) {
  144. throw new RuntimeException("Error in instantiating YarnClient", ufe);
  145. }
  146. }
  147.  
  148. @Private
  149. /**
  150. * Used for testing mostly.
  151. * @param resMgrDelegate the resource manager delegate to set to.
  152. */
  153. public void setResourceMgrDelegate(ResourceMgrDelegate resMgrDelegate) {
  154. this.resMgrDelegate = resMgrDelegate;
  155. }
  156.  
  157. @Override
  158. public void cancelDelegationToken(Token<DelegationTokenIdentifier> arg0)
  159. throws IOException, InterruptedException {
  160. throw new UnsupportedOperationException("Use Token.renew instead");
  161. }
  162.  
  163. @Override
  164. public TaskTrackerInfo[] getActiveTrackers() throws IOException,
  165. InterruptedException {
  166. return resMgrDelegate.getActiveTrackers();
  167. }
  168.  
  169. @Override
  170. public JobStatus[] getAllJobs() throws IOException, InterruptedException {
  171. return resMgrDelegate.getAllJobs();
  172. }
  173.  
  174. @Override
  175. public TaskTrackerInfo[] getBlacklistedTrackers() throws IOException,
  176. InterruptedException {
  177. return resMgrDelegate.getBlacklistedTrackers();
  178. }
  179.  
  180. @Override
  181. public ClusterMetrics getClusterMetrics() throws IOException,
  182. InterruptedException {
  183. return resMgrDelegate.getClusterMetrics();
  184. }
  185.  
  186. @VisibleForTesting
  187. void addHistoryToken(Credentials ts) throws IOException, InterruptedException {
  188. /* check if we have a hsproxy, if not, no need */
  189. MRClientProtocol hsProxy = clientCache.getInitializedHSProxy();
  190. if (UserGroupInformation.isSecurityEnabled() && (hsProxy != null)) {
  191. /*
  192. * note that get delegation token was called. Again this is hack for oozie
  193. * to make sure we add history server delegation tokens to the credentials
  194. */
  195. RMDelegationTokenSelector tokenSelector = new RMDelegationTokenSelector();
  196. Text service = resMgrDelegate.getRMDelegationTokenService();
  197. if (tokenSelector.selectToken(service, ts.getAllTokens()) != null) {
  198. Text hsService = SecurityUtil.buildTokenService(hsProxy
  199. .getConnectAddress());
  200. if (ts.getToken(hsService) == null) {
  201. ts.addToken(hsService, getDelegationTokenFromHS(hsProxy));
  202. }
  203. }
  204. }
  205. }
  206.  
  207. @VisibleForTesting
  208. Token<?> getDelegationTokenFromHS(MRClientProtocol hsProxy)
  209. throws IOException, InterruptedException {
  210. GetDelegationTokenRequest request = recordFactory
  211. .newRecordInstance(GetDelegationTokenRequest.class);
  212. request.setRenewer(Master.getMasterPrincipal(conf));
  213. org.apache.hadoop.yarn.api.records.Token mrDelegationToken;
  214. mrDelegationToken = hsProxy.getDelegationToken(request)
  215. .getDelegationToken();
  216. return ConverterUtils.convertFromYarn(mrDelegationToken,
  217. hsProxy.getConnectAddress());
  218. }
  219.  
  220. @Override
  221. public Token<DelegationTokenIdentifier> getDelegationToken(Text renewer)
  222. throws IOException, InterruptedException {
  223. // The token is only used for serialization. So the type information
  224. // mismatch should be fine.
  225. return resMgrDelegate.getDelegationToken(renewer);
  226. }
  227.  
  228. @Override
  229. public String getFilesystemName() throws IOException, InterruptedException {
  230. return resMgrDelegate.getFilesystemName();
  231. }
  232.  
  233. @Override
  234. public JobID getNewJobID() throws IOException, InterruptedException {
  235. return resMgrDelegate.getNewJobID();
  236. }
  237.  
  238. @Override
  239. public QueueInfo getQueue(String queueName) throws IOException,
  240. InterruptedException {
  241. return resMgrDelegate.getQueue(queueName);
  242. }
  243.  
  244. @Override
  245. public QueueAclsInfo[] getQueueAclsForCurrentUser() throws IOException,
  246. InterruptedException {
  247. return resMgrDelegate.getQueueAclsForCurrentUser();
  248. }
  249.  
  250. @Override
  251. public QueueInfo[] getQueues() throws IOException, InterruptedException {
  252. return resMgrDelegate.getQueues();
  253. }
  254.  
  255. @Override
  256. public QueueInfo[] getRootQueues() throws IOException, InterruptedException {
  257. return resMgrDelegate.getRootQueues();
  258. }
  259.  
  260. @Override
  261. public QueueInfo[] getChildQueues(String parent) throws IOException,
  262. InterruptedException {
  263. return resMgrDelegate.getChildQueues(parent);
  264. }
  265.  
  266. @Override
  267. public String getStagingAreaDir() throws IOException, InterruptedException {
  268. return resMgrDelegate.getStagingAreaDir();
  269. }
  270.  
  271. @Override
  272. public String getSystemDir() throws IOException, InterruptedException {
  273. return resMgrDelegate.getSystemDir();
  274. }
  275.  
  276. @Override
  277. public long getTaskTrackerExpiryInterval() throws IOException,
  278. InterruptedException {
  279. return resMgrDelegate.getTaskTrackerExpiryInterval();
  280. }
  281.  
  282. @Override
  283. public JobStatus submitJob(JobID jobId, String jobSubmitDir, Credentials ts)
  284. throws IOException, InterruptedException {
  285.  
  286. addHistoryToken(ts);
  287.  
  288. // Construct necessary information to start the MR AM
  289. ApplicationSubmissionContext appContext =
  290. createApplicationSubmissionContext(conf, jobSubmitDir, ts);
  291.  
  292. // Submit to ResourceManager
  293. try {
  294. ApplicationId applicationId =
  295. resMgrDelegate.submitApplication(appContext);
  296.  
  297. ApplicationReport appMaster = resMgrDelegate
  298. .getApplicationReport(applicationId);
  299. String diagnostics =
  300. (appMaster == null ?
  301. "application report is null" : appMaster.getDiagnostics());
  302. if (appMaster == null
  303. || appMaster.getYarnApplicationState() == YarnApplicationState.FAILED
  304. || appMaster.getYarnApplicationState() == YarnApplicationState.KILLED) {
  305. throw new IOException("Failed to run job : " +
  306. diagnostics);
  307. }
  308. return clientCache.getClient(jobId).getJobStatus(jobId);
  309. } catch (YarnException e) {
  310. throw new IOException(e);
  311. }
  312. }
  313.  
  314. private LocalResource createApplicationResource(FileContext fs, Path p, LocalResourceType type)
  315. throws IOException {
  316. LocalResource rsrc = recordFactory.newRecordInstance(LocalResource.class);
  317. FileStatus rsrcStat = fs.getFileStatus(p);
  318. rsrc.setResource(ConverterUtils.getYarnUrlFromPath(fs
  319. .getDefaultFileSystem().resolvePath(rsrcStat.getPath())));
  320. rsrc.setSize(rsrcStat.getLen());
  321. rsrc.setTimestamp(rsrcStat.getModificationTime());
  322. rsrc.setType(type);
  323. rsrc.setVisibility(LocalResourceVisibility.APPLICATION);
  324. return rsrc;
  325. }
  326.  
  327. public ApplicationSubmissionContext createApplicationSubmissionContext(
  328. Configuration jobConf,
  329. String jobSubmitDir, Credentials ts) throws IOException {
  330. ApplicationId applicationId = resMgrDelegate.getApplicationId();
  331.  
  332. // Setup resource requirements
  333. Resource capability = recordFactory.newRecordInstance(Resource.class);
  334. capability.setMemory(
  335. conf.getInt(
  336. MRJobConfig.MR_AM_VMEM_MB, MRJobConfig.DEFAULT_MR_AM_VMEM_MB
  337. )
  338. );
  339. capability.setVirtualCores(
  340. conf.getInt(
  341. MRJobConfig.MR_AM_CPU_VCORES, MRJobConfig.DEFAULT_MR_AM_CPU_VCORES
  342. )
  343. );
  344. LOG.debug("AppMaster capability = " + capability);
  345.  
  346. // Setup LocalResources
  347. Map<String, LocalResource> localResources =
  348. new HashMap<String, LocalResource>();
  349.  
  350. Path jobConfPath = new Path(jobSubmitDir, MRJobConfig.JOB_CONF_FILE);
  351.  
  352. URL yarnUrlForJobSubmitDir = ConverterUtils
  353. .getYarnUrlFromPath(defaultFileContext.getDefaultFileSystem()
  354. .resolvePath(
  355. defaultFileContext.makeQualified(new Path(jobSubmitDir))));
  356. LOG.debug("Creating setup context, jobSubmitDir url is "
  357. + yarnUrlForJobSubmitDir);
  358.  
  359. localResources.put(MRJobConfig.JOB_CONF_FILE,
  360. createApplicationResource(defaultFileContext,
  361. jobConfPath, LocalResourceType.FILE));
  362. if (jobConf.get(MRJobConfig.JAR) != null) {
  363. Path jobJarPath = new Path(jobConf.get(MRJobConfig.JAR));
  364. LocalResource rc = createApplicationResource(defaultFileContext,
  365. jobJarPath,
  366. LocalResourceType.PATTERN);
  367. String pattern = conf.getPattern(JobContext.JAR_UNPACK_PATTERN,
  368. JobConf.UNPACK_JAR_PATTERN_DEFAULT).pattern();
  369. rc.setPattern(pattern);
  370. localResources.put(MRJobConfig.JOB_JAR, rc);
  371. } else {
  372. // Job jar may be null. For e.g, for pipes, the job jar is the hadoop
  373. // mapreduce jar itself which is already on the classpath.
  374. LOG.info("Job jar is not present. "
  375. + "Not adding any jar to the list of resources.");
  376. }
  377.  
  378. // TODO gross hack
  379. for (String s : new String[] {
  380. MRJobConfig.JOB_SPLIT,
  381. MRJobConfig.JOB_SPLIT_METAINFO }) {
  382. localResources.put(
  383. MRJobConfig.JOB_SUBMIT_DIR + "/" + s,
  384. createApplicationResource(defaultFileContext,
  385. new Path(jobSubmitDir, s), LocalResourceType.FILE));
  386. }
  387.  
  388. // Setup security tokens
  389. DataOutputBuffer dob = new DataOutputBuffer();
  390. ts.writeTokenStorageToStream(dob);
  391. ByteBuffer securityTokens = ByteBuffer.wrap(dob.getData(), , dob.getLength());
  392.  
  393. // Setup the command to run the AM
  394. List<String> vargs = new ArrayList<String>();
  395. /*vargs.add(MRApps.crossPlatformifyMREnv(jobConf, Environment.JAVA_HOME)
  396. + "/bin/java");*/
  397. vargs.add("$JAVA_HOME/bin/java");
  398. // TODO: why do we use 'conf' some places and 'jobConf' others?
  399. long logSize = jobConf.getLong(MRJobConfig.MR_AM_LOG_KB,
  400. MRJobConfig.DEFAULT_MR_AM_LOG_KB) << ;
  401. String logLevel = jobConf.get(
  402. MRJobConfig.MR_AM_LOG_LEVEL, MRJobConfig.DEFAULT_MR_AM_LOG_LEVEL);
  403. int numBackups = jobConf.getInt(MRJobConfig.MR_AM_LOG_BACKUPS,
  404. MRJobConfig.DEFAULT_MR_AM_LOG_BACKUPS);
  405. MRApps.addLog4jSystemProperties(logLevel, logSize, numBackups, vargs);
  406.  
  407. // Check for Java Lib Path usage in MAP and REDUCE configs
  408. warnForJavaLibPath(conf.get(MRJobConfig.MAP_JAVA_OPTS,""), "map",
  409. MRJobConfig.MAP_JAVA_OPTS, MRJobConfig.MAP_ENV);
  410. warnForJavaLibPath(conf.get(MRJobConfig.MAPRED_MAP_ADMIN_JAVA_OPTS,""), "map",
  411. MRJobConfig.MAPRED_MAP_ADMIN_JAVA_OPTS, MRJobConfig.MAPRED_ADMIN_USER_ENV);
  412. warnForJavaLibPath(conf.get(MRJobConfig.REDUCE_JAVA_OPTS,""), "reduce",
  413. MRJobConfig.REDUCE_JAVA_OPTS, MRJobConfig.REDUCE_ENV);
  414. warnForJavaLibPath(conf.get(MRJobConfig.MAPRED_REDUCE_ADMIN_JAVA_OPTS,""), "reduce",
  415. MRJobConfig.MAPRED_REDUCE_ADMIN_JAVA_OPTS, MRJobConfig.MAPRED_ADMIN_USER_ENV);
  416.  
  417. // Add AM admin command opts before user command opts
  418. // so that it can be overridden by user
  419. String mrAppMasterAdminOptions = conf.get(MRJobConfig.MR_AM_ADMIN_COMMAND_OPTS,
  420. MRJobConfig.DEFAULT_MR_AM_ADMIN_COMMAND_OPTS);
  421. warnForJavaLibPath(mrAppMasterAdminOptions, "app master",
  422. MRJobConfig.MR_AM_ADMIN_COMMAND_OPTS, MRJobConfig.MR_AM_ADMIN_USER_ENV);
  423. vargs.add(mrAppMasterAdminOptions);
  424.  
  425. // Add AM user command opts
  426. String mrAppMasterUserOptions = conf.get(MRJobConfig.MR_AM_COMMAND_OPTS,
  427. MRJobConfig.DEFAULT_MR_AM_COMMAND_OPTS);
  428. warnForJavaLibPath(mrAppMasterUserOptions, "app master",
  429. MRJobConfig.MR_AM_COMMAND_OPTS, MRJobConfig.MR_AM_ENV);
  430. vargs.add(mrAppMasterUserOptions);
  431.  
  432. vargs.add(MRJobConfig.APPLICATION_MASTER_CLASS);
  433. vargs.add("1>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR +
  434. Path.SEPARATOR + ApplicationConstants.STDOUT);
  435. vargs.add("2>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR +
  436. Path.SEPARATOR + ApplicationConstants.STDERR);
  437.  
  438. Vector<String> vargsFinal = new Vector<String>();
  439. // Final command
  440. StringBuilder mergedCommand = new StringBuilder();
  441. for (CharSequence str : vargs) {
  442. mergedCommand.append(str).append(" ");
  443. }
  444. vargsFinal.add(mergedCommand.toString());
  445.  
  446. LOG.debug("Command to launch container for ApplicationMaster is : "
  447. + mergedCommand);
  448.  
  449. // Setup the CLASSPATH in environment
  450. // i.e. add { Hadoop jars, job jar, CWD } to classpath.
  451. Map<String, String> environment = new HashMap<String, String>();
  452. MRApps.setClasspath(environment, conf);
  453.  
  454. // Setup the environment variables for Admin first
  455. MRApps.setEnvFromInputString(environment,
  456. conf.get(MRJobConfig.MR_AM_ADMIN_USER_ENV), conf);
  457. // Setup the environment variables (LD_LIBRARY_PATH, etc)
  458. MRApps.setEnvFromInputString(environment,
  459. conf.get(MRJobConfig.MR_AM_ENV), conf);
  460.  
  461. // Parse distributed cache
  462. MRApps.setupDistributedCache(jobConf, localResources);
  463.  
  464. Map<ApplicationAccessType, String> acls
  465. = new HashMap<ApplicationAccessType, String>();
  466. acls.put(ApplicationAccessType.VIEW_APP, jobConf.get(
  467. MRJobConfig.JOB_ACL_VIEW_JOB, MRJobConfig.DEFAULT_JOB_ACL_VIEW_JOB));
  468. acls.put(ApplicationAccessType.MODIFY_APP, jobConf.get(
  469. MRJobConfig.JOB_ACL_MODIFY_JOB,
  470. MRJobConfig.DEFAULT_JOB_ACL_MODIFY_JOB));
  471. replaceEnvironment(environment);
  472.  
  473. // Setup ContainerLaunchContext for AM container
  474. ContainerLaunchContext amContainer =
  475. ContainerLaunchContext.newInstance(localResources, environment,
  476. vargsFinal, null, securityTokens, acls);
  477.  
  478. Collection<String> tagsFromConf =
  479. jobConf.getTrimmedStringCollection(MRJobConfig.JOB_TAGS);
  480.  
  481. // Set up the ApplicationSubmissionContext
  482. ApplicationSubmissionContext appContext =
  483. recordFactory.newRecordInstance(ApplicationSubmissionContext.class);
  484. appContext.setApplicationId(applicationId); // ApplicationId
  485. appContext.setQueue( // Queue name
  486. jobConf.get(JobContext.QUEUE_NAME,
  487. YarnConfiguration.DEFAULT_QUEUE_NAME));
  488. appContext.setApplicationName( // Job name
  489. jobConf.get(JobContext.JOB_NAME,
  490. YarnConfiguration.DEFAULT_APPLICATION_NAME));
  491. appContext.setCancelTokensWhenComplete(
  492. conf.getBoolean(MRJobConfig.JOB_CANCEL_DELEGATION_TOKEN, true));
  493. appContext.setAMContainerSpec(amContainer); // AM Container
  494. appContext.setMaxAppAttempts(
  495. conf.getInt(MRJobConfig.MR_AM_MAX_ATTEMPTS,
  496. MRJobConfig.DEFAULT_MR_AM_MAX_ATTEMPTS));
  497. appContext.setResource(capability);
  498. appContext.setApplicationType(MRJobConfig.MR_APPLICATION_TYPE);
  499. if (tagsFromConf != null && !tagsFromConf.isEmpty()) {
  500. appContext.setApplicationTags(new HashSet<String>(tagsFromConf));
  501. }
  502. return appContext;
  503. }
  504.  
  505. @Override
  506. public void setJobPriority(JobID arg0, String arg1) throws IOException,
  507. InterruptedException {
  508. resMgrDelegate.setJobPriority(arg0, arg1);
  509. }
  510.  
  511. @Override
  512. public long getProtocolVersion(String arg0, long arg1) throws IOException {
  513. return resMgrDelegate.getProtocolVersion(arg0, arg1);
  514. }
  515.  
  516. @Override
  517. public long renewDelegationToken(Token<DelegationTokenIdentifier> arg0)
  518. throws IOException, InterruptedException {
  519. throw new UnsupportedOperationException("Use Token.renew instead");
  520. }
  521.  
  522. @Override
  523. public Counters getJobCounters(JobID arg0) throws IOException,
  524. InterruptedException {
  525. return clientCache.getClient(arg0).getJobCounters(arg0);
  526. }
  527.  
  528. @Override
  529. public String getJobHistoryDir() throws IOException, InterruptedException {
  530. return JobHistoryUtils.getConfiguredHistoryServerDoneDirPrefix(conf);
  531. }
  532.  
  533. @Override
  534. public JobStatus getJobStatus(JobID jobID) throws IOException,
  535. InterruptedException {
  536. JobStatus status = clientCache.getClient(jobID).getJobStatus(jobID);
  537. return status;
  538. }
  539.  
  540. @Override
  541. public TaskCompletionEvent[] getTaskCompletionEvents(JobID arg0, int arg1,
  542. int arg2) throws IOException, InterruptedException {
  543. return clientCache.getClient(arg0).getTaskCompletionEvents(arg0, arg1, arg2);
  544. }
  545.  
  546. @Override
  547. public String[] getTaskDiagnostics(TaskAttemptID arg0) throws IOException,
  548. InterruptedException {
  549. return clientCache.getClient(arg0.getJobID()).getTaskDiagnostics(arg0);
  550. }
  551.  
  552. @Override
  553. public TaskReport[] getTaskReports(JobID jobID, TaskType taskType)
  554. throws IOException, InterruptedException {
  555. return clientCache.getClient(jobID)
  556. .getTaskReports(jobID, taskType);
  557. }
  558.  
  559. @Override
  560. public void killJob(JobID arg0) throws IOException, InterruptedException {
  561. /* check if the status is not running, if not send kill to RM */
  562. JobStatus status = clientCache.getClient(arg0).getJobStatus(arg0);
  563. if (status.getState() != JobStatus.State.RUNNING) {
  564. try {
  565. resMgrDelegate.killApplication(TypeConverter.toYarn(arg0).getAppId());
  566. } catch (YarnException e) {
  567. throw new IOException(e);
  568. }
  569. return;
  570. }
  571.  
  572. try {
  573. /* send a kill to the AM */
  574. clientCache.getClient(arg0).killJob(arg0);
  575. long currentTimeMillis = System.currentTimeMillis();
  576. long timeKillIssued = currentTimeMillis;
  577. while ((currentTimeMillis < timeKillIssued + 10000L) && (status.getState()
  578. != JobStatus.State.KILLED)) {
  579. try {
  580. Thread.sleep(1000L);
  581. } catch(InterruptedException ie) {
  582. /** interrupted, just break */
  583. break;
  584. }
  585. currentTimeMillis = System.currentTimeMillis();
  586. status = clientCache.getClient(arg0).getJobStatus(arg0);
  587. }
  588. } catch(IOException io) {
  589. LOG.debug("Error when checking for application status", io);
  590. }
  591. if (status.getState() != JobStatus.State.KILLED) {
  592. try {
  593. resMgrDelegate.killApplication(TypeConverter.toYarn(arg0).getAppId());
  594. } catch (YarnException e) {
  595. throw new IOException(e);
  596. }
  597. }
  598. }
  599.  
  600. @Override
  601. public boolean killTask(TaskAttemptID arg0, boolean arg1) throws IOException,
  602. InterruptedException {
  603. return clientCache.getClient(arg0.getJobID()).killTask(arg0, arg1);
  604. }
  605.  
  606. @Override
  607. public AccessControlList getQueueAdmins(String arg0) throws IOException {
  608. return new AccessControlList("*");
  609. }
  610.  
  611. @Override
  612. public JobTrackerStatus getJobTrackerStatus() throws IOException,
  613. InterruptedException {
  614. return JobTrackerStatus.RUNNING;
  615. }
  616.  
  617. @Override
  618. public ProtocolSignature getProtocolSignature(String protocol,
  619. long clientVersion, int clientMethodsHash) throws IOException {
  620. return ProtocolSignature.getProtocolSignature(this, protocol, clientVersion,
  621. clientMethodsHash);
  622. }
  623.  
  624. @Override
  625. public LogParams getLogFileParams(JobID jobID, TaskAttemptID taskAttemptID)
  626. throws IOException {
  627. return clientCache.getClient(jobID).getLogFilePath(jobID, taskAttemptID);
  628. }
  629.  
  630. private static void warnForJavaLibPath(String opts, String component,
  631. String javaConf, String envConf) {
  632. if (opts != null && opts.contains("-Djava.library.path")) {
  633. LOG.warn("Usage of -Djava.library.path in " + javaConf + " can cause " +
  634. "programs to no longer function if hadoop native libraries " +
  635. "are used. These values should be set as part of the " +
  636. "LD_LIBRARY_PATH in the " + component + " JVM env using " +
  637. envConf + " config settings.");
  638. }
  639. }
  640.  
  641. private void replaceEnvironment(Map<String, String> environment) {
  642. String tmpClassPath = environment.get("CLASSPATH");
  643. tmpClassPath=tmpClassPath.replaceAll(";", ":");
  644. tmpClassPath=tmpClassPath.replaceAll("%PWD%", "\\$PWD");
  645. tmpClassPath=tmpClassPath.replaceAll("%HADOOP_MAPRED_HOME%", "\\$HADOOP_MAPRED_HOME");
  646. tmpClassPath= tmpClassPath.replaceAll("\\\\", "/" );
  647. environment.put("CLASSPATH",tmpClassPath);
  648. }
  649. }

org.apache.hadoop.mapred.YARNRunner

1,5 确保src下没有服务器hadoop的配置文件, 因为是本地调用, 配置文件中有主机等配置

1.6 代码中的调用方式改变

  1. // 初始化时加载src或classpath下所有的配置文件
  2. Configuration configuration = new Configuration();
  3. configuration.set("fs.default", "hdfs://wenbronk.hdfs.com:8020 ");
  4. configuration.set("yarn.resourcemanager", "hdfs://192.168.208.106");

然后, 就可以右键, run as java-application 来执行了

2, 服务器模式: 本地调用, 执行过程在服务器上进行

1, 将服务器的配置文件拷贝至本地src目录下

2, 代码修改, 在 RunMapReduce.java中

  1. // 初始化时加载src或classpath下所有的配置文件
  2. Configuration configuration = new Configuration();
  3.  
  4. // 本地执行
  5. // configuration.set("fs.default", "hdfs://wenbronk.hdfs.com:8020 ");
  6. // configuration.set("yarn.resourcemanager", "hdfs://192.168.208.106");
  7.  
  8. // 服务器执行, 指定本地jar包路径
  9. configuration.set("mapred.jar", "C:\\Users\\wenbr\\Desktop.wc.jar");

3, 本地执行main方法, Servlet调用MR

  

3, 直接在服务器运行, 打成jar上传到hadoop中

http://www.cnblogs.com/wenbronk/p/6662119.html

在网页查看运行状态

  1. http://wenbronk.hdfs.com:8088/cluster

14-hadoop-运行的2种方式的更多相关文章

  1. dubbo服务运行的三种方式

    dubbo服务运行,也就是让生产服务的进程一直启动.如果生产者进程挂掉,也就不存在生产者,消费者不能进行消费. Dubbo服务运行的三种方式如下:1.使用Servlet容器运行(Tomcat.Jett ...

  2. 【dubbo】服务提供者运行的三种方式

    [dubbo]服务提供者运行的三种方式 学习了:https://blog.csdn.net/yxwb1253587469/article/details/78712451 1,使用容器: 2,使用自建 ...

  3. Android-AnimationDrawable(三)运行的几种方式

    项目开发用到了AnimationDrawable,调用start后没有运行,很纳闷.google搜了下.记录一下. 这个AnimationDrawable.start不能直接写在onClick,onS ...

  4. VSCode的Python扩展下程序运行的几种方式与环境变量管理

    在VSCode中编写Python程序时,由于有些地方要使用环境变量,但是发现设置的环境变量有时不起作用,花了点时间研究了一下,过程不表,直接说结论. 首先,环境变量的设置,Python扩展中有三种方式 ...

  5. Linux上后台保持Terminal交互运行的三种方式:nohub、screen和tmux

    镜像下载.域名解析.时间同步请点击 阿里云开源镜像站 后台运行 Linux上,如果一个进程需要保持后台运行,尤其是在Linux服务器上,后台运行程序.避免因为SSH连接断开而导致进程停止运行时,该怎么 ...

  6. web项目嵌入Jetty运行的两种方式(Jetty插件和自制Jetty服务器)

    在开发Java web项目时候,可以在项目中嵌入Jetty服务的方式来运行web程序. 由于最近开发web项目,自己使用的是比较旧的eclipse不支持导入tomcat来运行项目,于是就学习了下使用项 ...

  7. Linux 进程后台运行的几种方式(screen)

    Ctrl+z/bg/nohup/setsid/& 在Linux中,如果要让进程在后台运行,一般情况下,我们在命令后面加上&即可,实际上,这样是将命令放入到一个作业队列中了: ./rsy ...

  8. C++实现程序单实例运行的两种方式

    简介 在我们编写程序的时候,经常会注意到的一个问题就是如何能够让程序只运行一个实例,确保不会让同一个程序多次运行,从而产生诸多相同进程,给我们的带来不便呢?那么常用的有以下四种方法,第一种方法是通过扫 ...

  9. nodejs运行的两种方式<小记>

    在mac上: 1.方式一:使用IDE运行 配置需要运行的js文件: 配置并运行 ①配置运行的js文件和运行的文件不一致时会导致报错.如图备注 ②当运行另一个文件提示端口8080被占用 ,需要改为其他端 ...

  10. linux后台运行的几种方式

    1.nohup将程序以忽略挂起信号的方式运行起来 补充说明nohup命令 可以将程序以忽略挂起信号的方式运行起来,被运行的程序的输出信息将不会显示到终端.无论是否将 nohup 命令的输出重定向到终端 ...

随机推荐

  1. Eclipse的bug,SunTlsRsaPremasterSecret KeyGenerator not available

    这个bug出现在安装完java后,不设置环境变量,直接打开eclipse 以后只要用到SSL相关(即RSA密钥加密相关)的代码,都会报这个错误,该方法在lib\ext的sunjce_provider. ...

  2. [译]在 Andriod/IOS 程序中使用自己的字体

    原文链接:http://firemonkeyblog.blogspot.com/2014/12/using-custom-fonts-in-android-delphi.html 你应该能够在 And ...

  3. 查看sql server数据库连接数的三种方法

    怎样才能查看sql server数据库连接数呢?下面就将为您介绍三种查看的方法,供您参考,希望能够帮助到您. 1.通过系统的“性能”来查看:开始->管理工具->性能(或者是运行里面输入 m ...

  4. 用C#开发的双色球走势图(原创)值得园友拥有

    首先声明,个人纯粹无聊之作,不作商业用途. 我相信每个人都拥有一个梦想那就是有朝一日能中500W,这个也一直是我的梦想,并默默每一期双色球或多或少要贡献自己一点点力量,本人并不属于那种铁杆的彩票迷,每 ...

  5. MS SQL 分页存储过程

    最近换了家新公司,但是新公司没有使用分页的存储过程.那我就自个写一个往项目上套 (效率怎么样就不怎么清楚没有详细的测试过) CREATE PROCEDURE [dbo].[pro_common_pag ...

  6. ASP.NET MVC 实现带论坛功能的网站 第一步——-实现用户注册.

    首先我们要实现用户的注册功能.进入visual studio 点击文件->新建->项目->选择ASP.NET Web应用程序(.NET Framework)->选择的模板为MV ...

  7. day74天中间件介绍

    一. importlib settings 执行结果: 两个process_request  process_response按照注册顺序的倒叙进行执行 PROCESS_VIEW  Process_v ...

  8. Flask从入门到精通之flask安装

    使用虚拟环境 安装Flask最简单的方式是使用虚拟环境,虚拟环境是python解释器的一个私有副本,在这个环境中你可以安装私有包,而且不会影响系统中安装的全局的Python解释器.虚拟环境非常有用,可 ...

  9. [Swift实际操作]七、常见概念-(7)日历Calendar和日期组件DateComponents

    本文将为你演示日历和日期组件的使用.通过日历的日期部件,可以获得日期的各个部分. 首先引入需要用到的界面工具框架 import UIKit 初始化一个日期对象,其值为当前的日期. let dt = D ...

  10. 话谈C#第二天

    今天做了几个小小的练习,和大家分享一下. 1.用*打印出等腰三角形,代码如下: static void Main(string[] args) { int n = 5; for (int i = 1; ...