java工作常用

读取resources目录下的文件(idea中和jar运行都可用)

InputStream inputStream = App.class.getClassLoader().getResourceAsStream("data.csv");

读取csv文件

添加依赖

        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.7.1</version>
        </dependency>

代码

     /**
   * 按行读取csv文件数据
   */
  public void readCsv() {
      String file = "D:\\tmp\\20.csv";
      // 一行一行读取
      try (CSVReader reader = new CSVReader(new BufferedReader(new FileReader(file)));) {
          HeaderColumnNameMappingStrategy<LineEntity> strategy = new HeaderColumnNameMappingStrategy<>();
          strategy.setType(LineEntity.class);
          CsvToBean<LineEntity> csvToBean = new CsvToBeanBuilder<LineEntity>(reader).withMappingStrategy(strategy).build();
          csvToBean.iterator().forEachRemaining(lineEntity -> {
              // todo 处理每行数据
              System.out.println();
          });
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
import com.opencsv.bean.CsvBindByName;
import lombok.Data;

// csv行数据映射为对象
@Data
public class LineEntity {
    @CsvBindByName(column = "主键")// csv列名
    private String id;
}

读取excel文件

添加依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.1</version>
        </dependency>

代码

    /**
     * 按行读取excel文件数据
     */
    public void readExcel() {
        String file = "D:\\tmp\\1.xlsx";
        EasyExcel.read(file, ExcelLineEntity.class, new ReadListener<ExcelLineEntity>() {

            @Override
            public void invoke(ExcelLineEntity data, AnalysisContext context) {
                // todo 处理每行数据
                System.out.println();
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                System.out.println("Excel所有数据读取结束");
            }

        }).sheet().doRead();
    }
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;

// excel行数据映射为对象
@Data
public class ExcelLineEntity {
    @ExcelProperty("主键")// excel列名
    private String id;
}

查找进程并停止

ps -ef | grep awesome-java-0.1.jar | grep -v grep | awk '{print $2}' | xargs kill -9

指定启动参数 @Value 或 不占端口启动

java -jar awesome-java-0.1.jar --server.port=9999 --spring.main.web-application-type=none

瘦包

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--                瘦包-->
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot.experimental</groupId>
                        <artifactId>spring-boot-thin-layout</artifactId>
                        <version>1.0.28.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>

下载项目所需的所有依赖: java -Dthin.dryrun=true -Dthin.root=. -jar awesome-java-0.1.jar
运行瘦包:java -Dthin.root=. -jar awesome-java-0.1.jar

分页_多线程处理数据

pageHelper分页,大表时性能非常差
        <!-- 选择适合当前springBoot的版本-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.1</version>
        </dependency>
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

// 批次计算
@Slf4j
public class BatchCalUtil {
    final static int CPU_NUM = Runtime.getRuntime().availableProcessors();

    final static ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(CPU_NUM);
    
    // 分页_多线程处理数据
    public String batchCal() {
        int pageSize = 10000;
        for (int i = 1; i < Integer.MAX_VALUE; i++) {
            // 分页查询, 数据量大性能很差, 必须使用 where id>? limit 1000
            PageHelper.startPage(i, pageSize);
            List<Object> pageData = select();
            if (!pageData.isEmpty()) {
                log.info("读取第{}页", i);
                for (Object data : pageData) {
                    pool.execute(() -> {
                        try {
                            // todo 处理数据
                        } catch (Exception e) {
                            log.error("异常:", e);
                        }
                    });
                }
                // 控制生产速度
                while (pool.getQueue().size() > 2 * pageSize) {
                    log.info("wait: {}, finish: {}", pool.getQueue().size(), pool.getCompletedTaskCount());
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

            } else {
                log.info("处理完毕");
                break;
            }
        }
        pool.shutdown();
        return "处理完毕";
    }

    List<Object> select() {
        throw new RuntimeException();
    }
}

高效率的批量insert

	// 注意控制list大小使每次sql的长度不超过`show global variables like 'max_allowed_packet';`的值, mysql8为64M
    @Insert("<script> INSERT INTO `test_table`(`id`, `age`) VALUES <foreach collection=\"list\" separator=\",\" item=\"item\">  (#{item.id}, #{item.age}) </foreach> </script>")
    int insertBatch(List<Entity> list);

minio工具类

import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;

import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;

/**
 * minio 工具类
 * wget https://dl.minio.org.cn/server/minio/release/linux-amd64/minio
 * chmod +x minio
 * export MINIO_ROOT_USER=minioadmin
 * export MINIO_ROOT_PASSWORD=minioadmin
 * ./minio server data --address :9005
 *
 * <dependency>
 * <groupId>io.minio</groupId>
 * <artifactId>minio</artifactId>
 * <version>8.4.3</version>
 * </dependency>
 */
public class MinioUtil {

    private final static MinioClient minioClient;

    static {
        minioClient = MinioClient.builder().endpoint("http://172.18.240.55:9000").credentials("minioadmin", "minioadmin").build();
    }

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String bucket = "testbucket";
        boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (!bucketExists) {
            // 新建桶
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
        }
        String pathname = "D:\\e02a7f8ca1c6c8.pdf";
        File uploadFile = new File(pathname);
        String fileKey = "file1.key";
//        ObjectWriteResponse writeResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(fileKey).stream(new BufferedInputStream(new FileInputStream(uploadFile)), uploadFile.length(), 5242880L).build());
        ObjectWriteResponse writeResponse = minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucket).object(fileKey).filename(pathname).build());
        System.out.println();
        String fileUrl = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().expiry(1, TimeUnit.DAYS).bucket(bucket).method(Method.GET).object(fileKey).build());
        System.out.println();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值