In the case of the cannon balls, the sides weren't able to compress because when the balls hit the stop, they weren't able to flow and fill up space like water is able to. The water in the tube is under a lot of pressure and would love to go somewhere. If you were to make a hole in that tube, you'd find that you had a miniature fountain squirting out of it. Squirting upwards, and in the right circumstances, above the water's source. This is the basic first half of a hydraulic pump's operation.
Water is accelerated down a tube that has an open end.
It builds up speed thereby momentum. Since can't just stop on command, it compresses. When a maximum pressure has been detected, another valve snaps open and the water shoots through it. This cycle then repeats. But that's only the first half of the operation.