首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >雪花SDKTest:错误:(137,49) java:找不到符号变量PublicKeyReader位置: net.snowflake.ingest.example.SDKTest类

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

Stack Overflow用户
提问于 2019-10-08 08:15:59
回答 2查看 458关注 0票数 0

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

我正在使用IntelliJ和JavaSDK12.0。

代码语言:javascript
复制
 Error:(137, 49) java: cannot find symbol
      symbol:   variable PublicKeyReader
      location: class net.snowflake.ingest.example.SDKTest

有人能帮我吗?

BR

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-09 23:32:25

下面是正在运行的SDK测试代码副本:

包装com.snowflake.SalesforceTestCases;

代码语言:javascript
复制
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();
        }

    }
}
票数 0
EN

Stack Overflow用户

发布于 2019-10-08 18:13:43

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

错误的参考资料:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58282539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档