The career is organized around what we call Cycles, that is, the different roles and jobs you have during your professional life.
There is not a magic number of years you should stay in one job, but the time you spend in each place is meaningful in terms of building up your professional profile.
You will need to make decisions and then execute without looking back. Once you feel the cycle is completed, you will evaluate if the job has fulfilled your expectations, and continue down that road or change course.
If you are a recent graduate, it is not a surprise if you change job as you try to figure out what you want to be. Later, recruiters tend to look for consistency. The average number of years that workers stay with their employers has decreased during the last decades but changing professional role too frequently will not be positive for your job interviews. When you start in a new job, it is not advisable to abandon or seek a change when the first setback appears. You learn a lot from difficult situations and it is advisable to stay on track during a reasonable period of time, unless there is an exceptional circumstance that advises otherwise.