如何将数据从Java对象加载到Kudu表



我有一个Java代码,用于将JSON字符串转换为Java对象。我正在将该字符串的值存储到对象中。

接下来我需要做的是将这些值存储在Kudu表中。如果可能的话,我只想知道如何使用Docker容器来实现这一点。或者还有其他方法可以做到这一点。

下面是我写的代码:

import com.google.gson.Gson;
import java.util.Map;
class UserDetails {public String id; public int type; public Map<String, String> source; public String source_uri; public long timestamp; public String options; public Map<String, String> payload;}
public class JSONToJSONObject {
public static void main(String[] args) {
String json = "{'id':'46B56A42-6500-6500-59DF-68E26AD11BCE','type':1,'source':{'org':'JJV','site':'US11','area':'MFG','gen':'3GT','line':'TAM04','cell':'LF','zone':'Z2B','process':'Takeover'},'source_uri':'JJV/US11/MFG/3GT/TAM04/LF/Z2B','timestamp':1557254610759,'options':null,'payload':{'dcp_id':'DCP003','trigger_count':1593,'lot_number':'B00SDZK','pallet_id':0,'status_code':1,'station_code':1,'pallet_ts':1557254568000,'mold_open_1_ts':1557254570000,'mold_open_2_ts':1557254568000,'mold_open_3_ts':1557254573000,'mold_open_4_ts':1557254570000,'butterfly_pick_a_ts':1557254574000,'butterfly_pick_b_ts':1557254572000,'robot_pick_1_ts':1557254572000,'robot_pick_2_ts':1557254574000,'pickpallet_xfer_ts':1557254610764,'x_200ps_vac_peak':0,'x_201ps_vac_peak':0,'x_202ps_vac_peak':0,'takeover_xfer_1_PS206':0,'takeover_xfer_2_PS206':0,'takeover_xfer_3_PS206':0,'nest_a_vac':0,'nest_b_vac':0,'mold_open_1_hs':9,'mold_open_2_hs':7,'mold_open_3_hs':2,'mold_open_4_hs':9,'butterfly_pick_a_hs':4,'butterfly_pick_b_hs':5,'robot_pick_1_hs':5,'robot_pick_2_hs':4,'pallet_xfer_hs':0}}";
Gson gson = new Gson();
UserDetails user = gson.fromJson(json, UserDetails.class);
System.out.println("ID is: " + user.id);
System.out.println("Type is: " + user.type);
System.out.println("Source is: " + user.source);
System.out.println("Source URI is: " + user.source_uri);
System.out.println("Timestamp is: " + user.timestamp);
System.out.println("Options is: " + user.options);
System.out.println("Payload is: " + user.payload);
}
}

此外,在我的Ubuntu虚拟机上安装了Docker之后,我尝试按照https://kudu.apache.org/docs/quickstart.html页但是克隆git-reo会抛出如下错误:

致命:包在偏移量____处有坏对象:返回-5

我创建了一个示例,并将其记录在github repo中。为了在本地启动kudu,我使用andreysbitov/impala kudu docker映像,并使用以下docker命令启动容器:

docker run -d --name kudu-impala 
-p 8050:8050 -p 8051:8051 -p 7050:7050 -p 7051:7051 
-p 25000:25000 -p 25010:25010 -p 25020:25020 
-p 50070:50070 -p 50075:50075 
-p 21050:21050 -p 28000:28000 
-e HOSTNAME=localhost 
--hostname localhost 
andreysabitov/impala-kudu:latest

在Kudu中存储java对象就是在这个类中完成的。负责将对象转换为Kudu列的部分如下所示:

KuduTable table = clientProvider.get().openTable(tableName());
Upsert upsert = table.newUpsert();
upsert.getRow().addString("id", userInfo.getId());
upsert.getRow().addString("name", userInfo.getName());
upsert.getRow().addLong("counter", userInfo.getCounter());
session.apply(upsert);

相关内容

  • 没有找到相关文章

最新更新