morningman commented on a change in pull request #3230: Support load json-data 
into Doris by RoutineLoad or StreamLoad
URL: https://github.com/apache/incubator-doris/pull/3230#discussion_r410710996
 
 

 ##########
 File path: be/src/exec/json_scanner.cpp
 ##########
 @@ -0,0 +1,485 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+
+#include "exec/json_scanner.h"
+
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/mem_tracker.h"
+#include "runtime/raw_value.h"
+#include "runtime/runtime_state.h"
+#include "exprs/expr.h"
+#include "env/env.h"
+#include "exec/local_file_reader.h"
+#include "exec/broker_reader.h"
+#include "exprs/json_functions.h"
+
+namespace doris {
+
+JsonScanner::JsonScanner(RuntimeState* state,
+                         RuntimeProfile* profile,
+                         const TBrokerScanRangeParams& params,
+                         const std::vector<TBrokerRangeDesc>& ranges,
+                         const std::vector<TNetworkAddress>& broker_addresses,
+                         ScannerCounter* counter) : BaseScanner(state, 
profile, params, counter),
+                          _ranges(ranges),
+                          _broker_addresses(broker_addresses),
+                          _cur_file_reader(nullptr),
+                          _next_range(0),
+                          _cur_file_eof(false),
+                          _scanner_eof(false) {
+
+}
+
+JsonScanner::~JsonScanner() {
+    close();
+}
+
+Status JsonScanner::open() {
+    return BaseScanner::open();
+}
+
+Status JsonScanner::get_next(Tuple* tuple, MemPool* tuple_pool, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    // Get one line
+    while (!_scanner_eof) {
+        if (_cur_file_reader == nullptr || _cur_file_eof) {
+            RETURN_IF_ERROR(open_next_reader());
+            // If there isn't any more reader, break this
+            if (_scanner_eof) {
+                continue;
+            }
+            _cur_file_eof = false;
+        }
+        RETURN_IF_ERROR(_cur_file_reader->read(_src_tuple, _src_slot_descs, 
tuple_pool, &_cur_file_eof));
+
+        if (_cur_file_eof) {
+            continue; // read next file
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+        if (fill_dest_tuple(Slice(), tuple, tuple_pool)) {
+            break;// break if true
+        }
+    }
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+Status JsonScanner::open_next_reader() {
+    if (_cur_file_reader != nullptr) {
+        delete _cur_file_reader;
+        _cur_file_reader = nullptr;
+        if (_stream_load_pipe != nullptr) {
+            _stream_load_pipe.reset();
+        }
+    }
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    const TBrokerRangeDesc& range = _ranges[_next_range++];
+    int64_t start_offset = range.start_offset;
+    if (start_offset != 0) {
+        start_offset -= 1;
+    }
+    FileReader *file = NULL;
+    switch (range.file_type) {
+    case TFileType::FILE_LOCAL: {
+        LocalFileReader* file_reader = new LocalFileReader(range.path, 
start_offset);
+        RETURN_IF_ERROR(file_reader->open());
+        file = file_reader;
+        break;
+    }
+    case TFileType::FILE_BROKER: {
+        BrokerReader* broker_reader = new BrokerReader(
+            _state->exec_env(), _broker_addresses, _params.properties, 
range.path, start_offset);
+        RETURN_IF_ERROR(broker_reader->open());
+        file = broker_reader;
+        break;
+    }
+
+    case TFileType::FILE_STREAM: {
+        _stream_load_pipe = 
_state->exec_env()->load_stream_mgr()->get(range.load_id);
+        if (_stream_load_pipe == nullptr) {
+            VLOG(3) << "unknown stream load id: " << UniqueId(range.load_id);
+            return Status::InternalError("unknown stream load id");
+        }
+        file = _stream_load_pipe.get();
+        break;
+    }
+    default: {
+        std::stringstream ss;
+        ss << "Unknown file type, type=" << range.file_type;
+        return Status::InternalError(ss.str());
+    }
+    }
+
+    std::string jsonpath = "";
+    std::string jsonpath_file = "";
+    if (range.__isset.jsonpath) {
+        jsonpath = range.jsonpath;
+    } else if (range.__isset.jsonpath_file) {
+        jsonpath_file = range.jsonpath_file;
+    }
+    _cur_file_reader = new JsonReader(_state->exec_env()->small_file_mgr(), 
_profile, file, jsonpath, jsonpath_file);
+
+    return Status::OK();
+}
+
+void JsonScanner::close() {
+    if (_cur_file_reader != nullptr) {
+        delete _cur_file_reader;
+        _cur_file_reader = nullptr;
+        if (_stream_load_pipe != nullptr) {
+            _stream_load_pipe.reset();
+        }
+    }
+}
+
+////// class JsonDataInternal
+JsonDataInternal::JsonDataInternal(rapidjson::Value* v) :
+         _jsonValues(v), _iterator(v->Begin()) {
+}
+
+JsonDataInternal::~JsonDataInternal() {
+
+}
+bool JsonDataInternal::isEnd() {
+    return _jsonValues->End() == _iterator;
+}
+
+rapidjson::Value::ConstValueIterator JsonDataInternal::getNext() {
+    if (isEnd()) {
+        return nullptr;
+    }
+    return _iterator++;
+}
+
+
+////// class JsonReader
+JsonReader::JsonReader(
+        SmallFileMgr *fileMgr,
+        RuntimeProfile* profile,
+        FileReader* file_reader,
+        std::string& jsonpath,
+        std::string& jsonpath_file) :
+            _next_line(0),
+            _total_lines(0),
+            _profile(profile),
+            _file_reader(file_reader),
+            _closed(false) {
+    _bytes_read_counter = ADD_COUNTER(_profile, "BytesRead", TUnit::BYTES);
+    _read_timer = ADD_TIMER(_profile, "FileReadTime");
+
+    //parse jsonpath
+    if (!jsonpath.empty()) {
 
 Review comment:
   It's better to create a `init()` method for JsonReader, like:
   ```
   JsonReader* reader = new JsonReader();
   Status st = reader->init();
   if (!st.ok()) {
       // handle the error
   }
   ```
   
   And `_parseJsonPathFlag` is only used for simple or complex json flag

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to