Impala创建/显示表信息

来源:互联网 发布:ncut算法代码 编辑:程序博客网 时间:2024/05/16 12:06

1、使用impala shell创建表,基本语法如下:

CREATE TABLE IF NOT EXISTS database_name.table_name(c1 data_type,c2 data_type,…cn data_type);

在my_db中创建一个名为student的表:

CREATE TABLE IF NOT EXISTS my_db.student(name STRING,age INT,contace INT);

执行语句,验证结果:
这里写图片描述
2、使用impala shell 显示表信息,使用describe关键字,语法如下:

DESCRIBE table_name;

显示student表的结构信息:

DESCRIBE student;

结果如下图:
这里写图片描述
3、使用python创建表,并验证,代码如下:

# coding:utf-8from impala.dbapi import connect# 连接impalaconn = connect(host='192.168.83.144',port=21050)cur = conn.cursor()# 执行命令create_sql = ''' CREATE TABLE IF NOT EXISTS my_dbbypy.student(name STRING,age INT,contace INT); '''cur.execute(create_sql)print "Created"cur.execute('use my_dbbypy')cur.execute('show tables')for table in cur.fetchall():    print tablecur.execute('DESCRIBE student')for value in cur.fetchall():    print value# 关闭连接cur.close()conn.close()

执行程序,运行结果如下:

Created('student',)('name', 'string', '')('age', 'int', '')('contace', 'int', '')