如何在Impyla连接

时间:2015-09-14 10:03:30

标签: python-2.7 python-module

我尝试从python连接impala并使用Impyla模块实现此目的。以下是用于连接的代码段。

connect(host='<impala_host>', port=21050)

当我尝试执行任何查询时,这与Authorization异常失败。确切的例外是,

impala.error.HiveServer2Error: AuthorizationException: User '<user>' does not have privileges to execute 'SELECT' on: <table>

如何为connect方法提供用户和密码?

3 个答案:

答案 0 :(得分:1)

查看代码时,默认情况下userpassword参数似乎设置为None。您应该能够使用以下内容自行设置它们:

connect(host='my.impala.host', port=21050, user='myuser', password='mypassword')

您可以在此处查看文档https://github.com/cloudera/impyla/blob/master/impala/dbapi.py

答案 1 :(得分:0)

我有同样的问题。通过添加use_ssl解决了这个问题:

connect(host='my.impala.host', port=21050, user='myuser', password='mypassword', use_ssl=True)

答案 2 :(得分:0)

用户名可以在光标参数中定义,例如

package custom;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {

    public static void main(String[] args) throws IOException {
        String startPath = "/Users/me/someProject/public";

        Files.walkFileTree(Paths.get(startPath), new SimpleFileVisitor<>(){
            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {

                if (!path.toString().endsWith(".html")) {
                    return FileVisitResult.CONTINUE;
                }
                var content = new String(Files.readAllBytes(path), "UTF-8");

                var fixedContent = replaceAfter(content,
                        "<div class=\"footer\">",
                        "http://hardcoded/something",
                        "/something");

                Files.write(path, fixedContent.getBytes());
                return FileVisitResult.CONTINUE;
            }
        });
    }

    /**
     * Replaces the `searchTerm` with `replacement` but only after the ocurrence of `afterMark`
     * If `afterMark` is not present nothing is changed
     * @param source - Original string
     * @param afterMark - Will perform the replacement after this mark is found
     * @param searchTerm - What to replace
     * @param replacement - replacement
     * @return a new string with the search term replaced
     */
    public static String replaceAfter(String source, String afterMark, String searchTerm, String replacement) {
        var i = source.indexOf(afterMark);
        if ( i < 0 ) {
            return source;
        }
        var replaced = source.substring(i).replace(searchTerm, replacement);
        return source.substring(0, i) + replaced;
    }
}