What is this hatch above the cockpit of a 747?
It's an emergency escape hatch. There are inertial reels in the cockpit ceiling adjacent to the hatch to allow the cockpit crew to exit the airplane. See this link for an account of their use escaping the aircraft during a hijacking.

There were five reels in the 747-100/200 aircraft. The cockpit had five seats: pilot, co-pilot, flight engineer, and two observer seats.

It's also sometimes used in third world countries to send a guy out to clean the cockpit windshield. In Mumbai I once made the mistake of asking the windshield to be cleaned, thinking, of course, they'd do it with a cherry picker. Much to my surprise, bare feet appeared at the top of the windshield followed by this poor fellow who was maneuvering into place to clean the windshield. He had a rope tied around his waist, but had he fallen it would have been bad because the rope was anchored through the hatch, which meant he would have swung badly had he fallen.