from pyhive import hive
conn = hive.Connection(host='192.168.122.251', port=10000, username='root', database='default', auth='NOSASL')
cursor = conn.cursor()
cursor.execute('SELECT * FROM test LIMIT 10')
for i in cursor.fetchall():
print(i)
conn.close()
<configuration>
<!--Hive集成MySQL-->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://172.168.0.3/metastore?createDatabaseIfNotExist=true</value>
<description>JDBC connect string for a JDBC metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
<description>Driver class name for a JDBC metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
<description>username to use against metastore database</description>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>123456</value>
<description>password to use against metastore database</description>
</property>
<!--Hive显示设置-->
<property>
<name>hive.cli.print.header</name>
<value>true</value>
<description>Whether to print the names of the columns in query output.</description>
</property>
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
<description>Whether to include the current database in the Hive prompt.</description>
</property>
<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
<description>
Enforce metastore schema version consistency.
True: Verify that version information stored in metastore matches with one from Hive jars. Also disable automatic
schema migration attempt. Users are required to manully migrate schema after Hive upgrade which ensures
proper metastore schema migration. (Default)
False: Warn if the version information stored in metastore doesn't match with one from in Hive jars.
</description>
</property>
<property>
<name>datanucleus.schema.autoCreateAll</name>
<value>true</value>
<description>creates necessary schema on a startup if one doesn't exist. set this to false, after creating it once</description>
</property>
<!--hiveserver2 配置-->
<!--property>
<name>hive.server2.authentication</name>
<value>CUSTOM</value>
</property>
<property>
<name>hive.server2.custom.authentication.class</name>
<value>org.apache.hadoop.hive.contrib.auth.CustomPasswdAuthenticator</value>
<description>指定解析jar包 注意修改成自己的类,注意是全路径名即包名和类名</description>
</property>
<property>
<name>hive.jdbc_passwd.auth.root</name>
<value>123456</value>
<description>设置用户名和密码,如果有多个用户和密码,可以多写几个property。name: 用户名为最后一个:用户,value: 密码</description>
</property-->
<property>
<name>hive.server2.authentication</name>
<value>NOSASL</value>
</property>
</configuration>
packageorg.apache.hadoop.hive.contrib.auth;importjavax.security.sasl.AuthenticationException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hive.conf.HiveConf;importorg.slf4j.Logger;publicclassCustomPasswdAuthenticatorimplementsorg.apache.hive.service.auth.PasswdAuthenticationProvider{privateLoggerLOG=org.slf4j.LoggerFactory.getLogger(CustomPasswdAuthenticator.class);privatestaticfinalStringHIVE_JDBC_PASSWD_AUTH_PREFIX="hive.jdbc_passwd.auth.%s";privateConfigurationconf=null;@OverridepublicvoidAuthenticate(StringuserName,Stringpasswd)throwsAuthenticationException{LOG.info("user: "+userName+" try login.");StringpasswdConf=getConf().get(String.format(HIVE_JDBC_PASSWD_AUTH_PREFIX,userName));if(passwdConf==null){Stringmessage="user's ACL configration is not found. user:"+userName;LOG.info(message);thrownewAuthenticationException(message);}if(!passwd.equals(passwdConf)){Stringmessage="user name and password is mismatch. user:"+userName;thrownewAuthenticationException(message);}}publicConfigurationgetConf(){if(conf==null){this.conf=newConfiguration(newHiveConf());}returnconf;}publicvoidsetConf(Configurationconf){this.conf=conf;}}