6 package com.vesoft.nebula.client.graph.data;
8 import com.vesoft.nebula.ErrorCode;
9 import com.vesoft.nebula.Row;
10 import com.vesoft.nebula.Value;
11 import com.vesoft.nebula.graph.ExecutionResponse;
12 import com.vesoft.nebula.graph.PlanDescription;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Spliterator;
17 import java.util.function.Consumer;
20 private final ExecutionResponse response;
21 private final List<String> columnNames =
new ArrayList<>();
22 private final String decodeType =
"utf-8";
23 private final int timezoneOffset;
25 public static class Record
implements Iterable<ValueWrapper> {
26 private final List<ValueWrapper>
colValues =
new ArrayList<>();
27 private List<String> columnNames =
new ArrayList<>();
29 public Record(List<String> columnNames, Row row, String decodeType,
int timezoneOffset) {
30 if (columnNames ==
null) {
34 if (row ==
null || row.values ==
null) {
38 for (Value value : row.values) {
39 this.colValues.add(
new ValueWrapper(value, decodeType, timezoneOffset));
42 this.columnNames = columnNames;
46 public Iterator<ValueWrapper> iterator() {
47 return this.colValues.iterator();
51 public void forEach(Consumer<? super ValueWrapper> action) {
52 this.colValues.forEach(action);
56 public Spliterator<ValueWrapper> spliterator() {
57 return this.colValues.spliterator();
62 public String toString() {
63 List<String> valueStr =
new ArrayList<>();
65 valueStr.add(v.toString());
67 return String.format(
"ColumnName: %s, Values: %s",
68 columnNames.toString(), valueStr.toString());
77 if (index >= columnNames.size()) {
78 throw new IllegalArgumentException(
79 String.format(
"Cannot get field because the key '%d' out of range", index));
81 return this.colValues.get(index);
90 int index = columnNames.indexOf(columnName);
92 throw new IllegalArgumentException(
93 "Cannot get field because the columnName '"
94 + columnName +
"' is not exists");
96 return this.colValues.get(index);
103 public List<ValueWrapper> values() {
112 return this.columnNames.size();
120 public boolean contains(String columnName) {
121 return this.columnNames.contains(columnName);
126 public ResultSet(ExecutionResponse resp,
int timezoneOffset) {
128 throw new RuntimeException(
"Input an null `ExecutionResponse' object");
130 this.response = resp;
131 this.timezoneOffset = timezoneOffset;
132 if (resp.data !=
null) {
134 for (
byte[] column : resp.data.column_names) {
135 this.columnNames.add(
new String(column));
145 return response.error_code == ErrorCode.SUCCEEDED;
153 return response.data ==
null || response.data.rows.isEmpty();
161 return response.error_code.getValue();
169 if (response.space_name ==
null) {
173 return new String(response.space_name);
181 if (response.error_msg ==
null) {
185 return new String(response.error_msg);
193 if (response.comment ==
null) {
197 return new String(response.comment);
205 return response.latency_in_us;
213 return response.getPlan_desc();
237 if (response.data ==
null) {
240 return response.data.rows.size();
249 if (response.data ==
null) {
250 throw new RuntimeException(
"Empty data");
252 if (index >= response.data.rows.size()) {
253 throw new ArrayIndexOutOfBoundsException();
255 return new Record(columnNames, response.data.rows.get(index), decodeType, timezoneOffset);
263 public List<ValueWrapper>
colValues(String columnName) {
264 if (response.data ==
null) {
265 throw new RuntimeException(
"Empty data");
267 int index = columnNames.indexOf(columnName);
269 throw new ArrayIndexOutOfBoundsException();
271 List<ValueWrapper> values =
new ArrayList<>();
272 for (
int i = 0; i < response.data.rows.size(); i++) {
273 values.add(
new ValueWrapper(response.data.rows.get(i).values.get(index),
285 if (response.data ==
null) {
286 throw new RuntimeException(
"Empty data");
288 return response.data.rows;
292 public String toString() {
295 return response.toString();
298 List<String> rowStrs =
new ArrayList<>();
300 List<String> valueStrs =
new ArrayList<>();
301 for (ValueWrapper value :
rowValues(i)) {
302 valueStrs.add(value.toString());
304 rowStrs.add(String.join(
",", valueStrs));
307 return String.format(
"ColumnName: %s, Rows: %s",
308 columnNames.toString(), rowStrs.toString());
int rowsSize()
get the size of rows
Record rowValues(int index)
get row values with the row index
String getComment()
get the comment from the server
List< String > keys()
get keys of the dataset
List< Row > getRows()
get all rows, the value is the origin value, the string is binary
int getErrorCode()
get errorCode of execute result
PlanDescription getPlanDesc()
get the PalnDesc
List< ValueWrapper > colValues(String columnName)
get col values on the column key
String getErrorMessage()
get the error message of the execute result
boolean isSucceeded()
the execute result is succeeded
boolean isEmpty()
the result data is empty
String getSpaceName()
get the space name of current session
List< String > getColumnNames()
get column names of the dataset
long getLatency()
get latency of the query execute time