Yes, you can go to the city. To get to the gate for your flight to Spain, you have to clear Schengen immigration controls in Amsterdam. You can just as well head to the airport exit instead of your gate (which probably won't be assigned when you arrive anyway).
As noted in a comment, there are actually different sets of immigration control desks for the airport exit and for transfers to Schengen area flights. The quickest way to get to the exit, of course, is to head there directly from your arrival gate, but if you accidentally go to the Schengen gates first, there's no need to worry; you can still get to the airport exit from there.
The officer at the immigration checkpoint for the airport exit might ask why you're there if you're about to fly to Spain. If so, all you have to do is mention the time of your departure. Your plan to see a bit of the city is very common and should not raise any concerns.
Leaving the airport of course means that you'll need to clear security to get back in. You will not have any further immigration checks in Amsterdam or Spain, however, because the Netherlands and Spain are both in the Schengen area.
You will not need a hotel booking unless you want to sleep in a hotel.
Necessary precautions are dictated by common sense: be aware of your surroundings; allow plenty of time to return to the airport in case there are delays in the trains or at the security checkpoint.