Deep learning has become a powerful technique for effectively extracting features in the field of rolling bearing fault diagnosis. However, in the case of sparse labeled sample data, the feature distribution is quite different, and simple samples are used repeatedly, leading to challenges in fault diagnosis, including insufficient feature learning and inaccurate fault classification. Therefore, a contrastive learning method is proposed for few-shot scenarios, incorporating hard example mining to address these challenges. In this framework, the similarities and differences of samples are used to construct positive and negative sample pairs, which help deepen the model to recognize and learn the internal correlation and pattern between samples. On this basis, the hard example mining strategy is adopted to further optimize the sample distribution in the feature space. These learned features are then classified by a classifier. Experimental results show that the fault diagnosis model proposed in this paper achieves high accuracy.