Snowflake SDKTest:错误:(137,49)java:找不到符号变量PublicKeyReader位置:类net.snowflake.ingest.example.SDKTest

问题描述 投票:0回答:2

我无法按照此文档运行SDKTest类:https://docs.snowflake.net/manuals/user-guide/data-load-snowpipe-rest-load.html

我正在使用IntelliJ和Java SDK 12.0。

 Error:(137, 49) java: cannot find symbol
      symbol:   variable PublicKeyReader
      location: class net.snowflake.ingest.example.SDKTest

有人可以帮我吗?

BR,

sdk public-key snowflake
2个回答
0
投票

我对错误的理解是,编译器不知道变量PublicKeyReader引用了什么。也许变量在代码中声明不正确,或者拼写错误?您提到的文档中有一个示例Java程序,但如果有帮助,这里也有示例程序:https://github.com/snowflakedb/snowflake-ingest-java

错误参考:


0
投票

这里是SDK测试代码的运行副本:


package com.snowflake.SalesforceTestCases;]​​>

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.Instant;
import java.util.Base64;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.crypto.EncryptedPrivateKeyInfo;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

import net.snowflake.ingest.SimpleIngestManager;
import net.snowflake.ingest.connection.HistoryRangeResponse;
import net.snowflake.ingest.connection.HistoryResponse;

public class SDKTest {

    private static final String PRIVATE_KEY_FILE = "<Path to your p8 file>/rsa_key.p8";

    public static class PrivateKeyReader {
        public static PrivateKey get(String filename) throws Exception {
            File f = new File(filename);
            FileInputStream fis = new FileInputStream(f);
            DataInputStream dis = new DataInputStream(fis);
            byte[] keyBytes = new byte[(int) f.length()];
            dis.readFully(keyBytes);
            dis.close();

            String encrypted = new String(keyBytes);
            String passphrase = "Snowflake";
            encrypted = encrypted.replace("-----BEGIN ENCRYPTED PRIVATE KEY-----", "");
            encrypted = encrypted.replace("-----END ENCRYPTED PRIVATE KEY-----", "");
            EncryptedPrivateKeyInfo pkInfo = new EncryptedPrivateKeyInfo(Base64.getMimeDecoder().decode(encrypted));
            PBEKeySpec keySpec = new PBEKeySpec(passphrase.toCharArray());
            SecretKeyFactory pbeKeyFactory = SecretKeyFactory.getInstance(pkInfo.getAlgName());
            PKCS8EncodedKeySpec encodedKeySpec = pkInfo.getKeySpec(pbeKeyFactory.generateSecret(keySpec));
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey encryptedPrivateKey = keyFactory.generatePrivate(encodedKeySpec);
            return encryptedPrivateKey;
        }
    }



    private static HistoryResponse waitForFilesHistory(final SimpleIngestManager manager, Set<String> files)
            throws Exception {
        ExecutorService service = Executors.newSingleThreadExecutor();

        class GetHistory implements Callable<HistoryResponse> {
            private Set<String> filesWatchList;

            GetHistory(Set<String> files) {
                this.filesWatchList = files;
            }

            String beginMark = null;

            public HistoryResponse call() throws Exception {
                HistoryResponse filesHistory = null;
                while (true) {
                    Thread.sleep(5000);
                    HistoryResponse response = manager.getHistory(null, null, beginMark);
                    if (response.getNextBeginMark() != null) {
                        beginMark = response.getNextBeginMark();
                    }
                    if (response != null && response.files != null) {
                        for (HistoryResponse.FileEntry entry : response.files) {
                            // if we have a complete file that we've
                            // loaded with the same name..
                            String filename = entry.getPath();
                            if (entry.getPath() != null && entry.isComplete() && filesWatchList.contains(filename)) {
                                if (filesHistory == null) {
                                    filesHistory = new HistoryResponse();
                                    filesHistory.setPipe(response.getPipe());
                                }
                                filesHistory.files.add(entry);
                                filesWatchList.remove(filename);
                                // we can return true!
                                if (filesWatchList.isEmpty()) {
                                    return filesHistory;
                                }
                            }
                        }
                    }
                }
            }
        }

        GetHistory historyCaller = new GetHistory(files);
        // fork off waiting for a load to the service
        Future<HistoryResponse> result = service.submit(historyCaller);

        HistoryResponse response = result.get(2, TimeUnit.MINUTES);
        return response;
    }

    public static void main(String[] args) {
        final String account = "<Snowflake Account Name >";
        final String hostName = "<Snowflake Account URL>";
        final String user = "<Snowflake Account Username>";
        final String pipe = "<Snowflake Pipe Path>";
        try {
            final long oneHourMillis = 1000 * 3600L;
            String startTime = Instant.ofEpochMilli(System.currentTimeMillis() - 4 * oneHourMillis).toString();
            PrivateKey privateKey = PrivateKeyReader.get(PRIVATE_KEY_FILE);
            //PublicKey publicKey =  PublicKeyReader.get(PUBLIC_KEY_FILE);

            @SuppressWarnings("deprecation")
            SimpleIngestManager manager = new SimpleIngestManager(account, user, pipe, privateKey, "https", hostName, 443);



            Set<String> files = new TreeSet<>();
            files.add("<filename>");

            manager.ingestFiles(manager.wrapFilepaths(files), null);
            HistoryResponse history = waitForFilesHistory(manager, files);
            System.out.println("Received history response: " + history.toString());
            String endTime = Instant.ofEpochMilli(System.currentTimeMillis()).toString();

            HistoryRangeResponse historyRangeResponse = manager.getHistoryRange(null, startTime, endTime);
            System.out.println("Received history range response: " + historyRangeResponse.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.