Java API连接HDFS并创建Orc文件

1、设置连接,参考之前文章:Java API操作HA方式下的Hadoop

    static String ClusterName = "nsstargate";
	private static final String HADOOP_URL = "hdfs://"+ClusterName;
	public static Configuration conf;

    static {
        conf = new Configuration();
        conf.set("fs.defaultFS", HADOOP_URL);
        conf.set("dfs.nameservices", ClusterName);
        conf.set("dfs.ha.namenodes."+ClusterName, "nn1,nn2");
        conf.set("dfs.namenode.rpc-address."+ClusterName+".nn1", "172.16.50.24:8020");
        conf.set("dfs.namenode.rpc-address."+ClusterName+".nn2", "172.16.50.21:8020");
        //conf.setBoolean(name, value);
        conf.set("dfs.client.failover.proxy.provider."+ClusterName, 
        		"org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
    }

注:如果只是Configuration conf = new Configuration(); 不设置hdfs连接信息的话,则会将文件写到本地磁盘上(需要配置hadoop环境信息)。

2、设置orc文件的schema

		TypeDescription schema = TypeDescription.createStruct()
				.addField("field1", TypeDescription.createLong())
		        .addField("field2", TypeDescription.createDouble())
		        .addField("field3", TypeDescription.createBoolean())
		        .addField("field4", TypeDescription.createTimestamp())
		        .addField("field5", TypeDescription.createString());

3、输出ORC文件到HDFS

		String fileName = "/user/test/test_orc_file_datatype.orc";
		Path path = new Path(fileName);
		FileSystem fs;
		try {
			fs = path.getFileSystem(conf);
			if (fs.exists(path)) {
				fs.delete(path, true);
		    }
		} catch (Exception e) {
			e.printStackTrace();
			throw new KettleFileException(e.getCause());
		}
		Writer writer = OrcFile.createWriter(path,
				OrcFile.writerOptions(conf)
		          .setSchema(schema)
		          .stripeSize(67108864)
		          .bufferSize(131072)
		          .blockSize(134217728)
		          .compress(CompressionKind.ZLIB)
		          .version(OrcFile.Version.V_0_12));
		//要写入的内容
		Object[][] contents = new Object[][]{
				{1l,1.1,false,"2016-10-21 14:56:25","abcd"},
				{2l,1.2,true,"2016-10-22 14:56:25","中文"}
				};
		
		VectorizedRowBatch batch = schema.createRowBatch();
		for(Object[] content : contents) {
			int rowCount = batch.size++;
			((LongColumnVector) batch.cols[0]).vector[rowCount] = (long) content[0];
			((DoubleColumnVector) batch.cols[1]).vector[rowCount] =(double) content[1];
			((LongColumnVector) batch.cols[2]).vector[rowCount] =content[2].equals(true)?1:0;
			((TimestampColumnVector) batch.cols[3]).time[rowCount] 
					= (Timestamp.valueOf((String) content[3])).getTime();
			((BytesColumnVector) batch.cols[4]).setVal(rowCount, content[4].toString().getBytes("UTF8"));
			//batch full
			if (batch.size == batch.getMaxSize()) {
			    writer.addRowBatch(batch);
			    batch.reset();
			}
		}
		if(batch.size>0){
			writer.addRowBatch(batch);
		}
		writer.close();

4、Hive建表及load orc文件

create table testtype(field1 bigint, f2 double, f3 boolean,field4 timestamp,f5 string) stored AS orc;

load data inpath '/user/test/test_orc_file_datatype.orc' overwrite into table testtype;

在创建文件以及将orc文件导入到Hive表中时,需要注意的是:

当字段为boolean类型时,则schema为boolean,写入为long(true为1,false为0),创建hive表为boolean,建表时字段无需与schema中字段同名,但是必须保证顺序一致。

 

可以参考:http://orc.apache.org/docs/core-java.html

转载于:https://my.oschina.net/nivalsoul/blog/777108

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值