ODPS UploadSample

package com.alibaba.odps.tunnel.samples;

import java.io.IOException;
import java.util.Date;

import com.alibaba.odps.tunnel.Column;
import com.alibaba.odps.tunnel.Account;
import com.alibaba.odps.tunnel.Configuration;
import com.alibaba.odps.tunnel.DataTunnel;
import com.alibaba.odps.tunnel.RecordSchema;
import com.alibaba.odps.tunnel.TunnelException;
import com.alibaba.odps.tunnel.Upload;
import com.alibaba.odps.tunnel.Upload.Status;
import com.alibaba.odps.tunnel.io.Record;
import com.alibaba.odps.tunnel.io.RecordWriter;

public class UploadSample {
private static String endpoint = "http://tunnelserver/";
private static String accessId = "";
private static String accessKey = "";

private static String project = "";
private static String table = "";
private static String partition = "";

public static void main(String args[]) {
Account account = new Account("aliyun",accessId,accessKey);
Configuration cfg = new Configuration(account, endpoint);
DataTunnel tunnel = new DataTunnel(cfg);

try {
Upload up = tunnel.createUpload(project, table, partition);
String id = up.getUploadId();
System.out.println("UploadId = " + id);

RecordSchema schema = up.getSchema();
System.out.println("Schema is: " + schema.toJsonString());

Status status = up.getStatus();
System.out.println("Status is: " + status.toString());

RecordWriter writer = up.openRecordWriter(0);
Record r = new Record(schema.getColumnCount());
for (int i = 0; i < schema.getColumnCount(); i++) {
Column.Type t = schema.getColumnType(i);
switch (t) {
case ODPS_BIGINT:
r.setBigint(i, 1L);
break;
case ODPS_DOUBLE:
r.setDouble(i, 0.0);
break;
case ODPS_DATETIME:
r.setDatetime(i, new Date());
break;
case ODPS_BOOLEAN:
r.setBoolean(i, true);
break;
case ODPS_STRING:
r.setString(i, "sample code");
break;
default:
throw new RuntimeException("Unknown column type: " + t);
}
}

for (int i = 0; i < 100; i++) {
writer.write(r);
}

writer.close();
Long[] blocks = {(long) 0};
up.complete(blocks);
} catch (TunnelException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}


祝你好运!!

你可能感兴趣的:(ODPS)