hive查询ncdc天气数据

使用hive查询ncdc天气数据
在hive中将ncdc天气数据导入,然后执行查询shell,可以让hive自动生成mapredjob,快速去的想要的数据结果。

1. 在hive中创建ncdc表,这个表用来存放ncdc的数据

create table ncdc (
year string,
month string,
data string,
time string,
air string,
a string,
b string,
c string,
d string,
e string,
f string,
g string
)ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ' STORED AS TEXTFILE;

TERMINATED BY ' ' 是说这个表子,使用tab键分割。
2. 处理原始的数据,因为原始的数据是这样的:
1901 01 01 06   -78 -9999 10200   270   159     8 -9999 -9999
1901 01 01 13   -72 -9999 10200   290    82     4 -9999 -9999
1901 01 01 20   -94 -9999 10200     0     0     8 -9999 -9999
1901 01 02 06   -61 -9999 10183   180    82     8 -9999 -9999
1901 01 02 13   -56 -9999 10176   180    98     8 -9999 -9999
1901 01 02 20   -28 -9999 10175   180    98     8 -9999 -9999
1901 01 03 06   -67 -9999 10170   200    98     6 -9999 -9999
1901 01 03 13   -33 -9999 10174   230   118     8 -9999 -9999
1901 01 03 20   -28 -9999 10174   230   118     8 -9999 -9999
1901 01 04 06   -33 -9999 10231     0     0     8 -9999 -9999
1901 01 04 13   -44 -9999 10226   230    82     8 -9999 -9999

中间不是制表符,而是空格键,所以写了一个java程序,将文件夹中的 所有的数据统计,转换到一个文件中。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.StringTokenizer;
public class removeAnno {
 static String ofile="summary";
 static BufferedWriter bw=null;
 public static void main(String[] args) throws Exception {
  bw = new BufferedWriter(new FileWriter(ofile));
  File file1 = new File("C:\Users\Administrator\ncdc2");
  File[] listfile = file1.listFiles();
  for (int i=0;i<listfile.length;i++){
   rm("C:\Users\Administrator\ncdc2\"+listfile[i].getName());
   //System.out.println(listfile[i].getName());
  }
 }
static void rm(String filename) throws Exception{
 File file = new File(filename);
 BufferedReader br = new BufferedReader(new FileReader(file));
 String str=br.readLine();
 while(str!=null){
  //进行分割处理
  String tmp="";
  StringTokenizer st = new StringTokenizer(str);
  while (st.hasMoreTokens()){
   tmp=tmp+st.nextToken()+" ";
  }
  bw.write(tmp+" ");
  bw.flush();
  str=br.readLine();
 }
}
}



3. 导入数据到hive中

load data local inpath '/opt/software/ncdc/summary' into table ncdc

4. 查询数据
可以查询每一年的平均气温,最高气温,最低气温等等,也可以使用分组函数,和MySQL操作差不多
select year,avg(air) from ncdc group by year;




版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/mrcharles/p/4731726.html