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-8
from impala.dbapi import connect
# 连接impala
conn = 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 table
cur.execute('DESCRIBE student')
for value in cur.fetchall():
print value
# 关闭连接
cur.close()
conn.close()
执行程序,运行结果如下:
Created
('student',)
('name', 'string', '')
('age', 'int', '')
('contace', 'int', '')