org.apache.camel.component.hbase.mapping.HeaderMappingStrategy Maven / Gradle / Ivy
/*
* 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.
*/
package org.apache.camel.component.hbase.mapping;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.component.hbase.HBaseAttribute;
import org.apache.camel.component.hbase.model.HBaseCell;
import org.apache.camel.component.hbase.model.HBaseData;
import org.apache.camel.component.hbase.model.HBaseRow;
/**
* A default {@link CellMappingStrategy} implementation. It distinguishes between multiple cell, by reading headers with
* index suffix.
*
* In case of multiple headers:
*
* - First header is expected to have no suffix
* - Suffixes start from number 2
* - Suffixes need to be sequential
*
*/
public class HeaderMappingStrategy implements CellMappingStrategy {
/**
* Resolves the cell that the {@link Exchange} refers to.
*/
private HBaseRow resolveRow(Message message, int index) {
HBaseRow hRow = new HBaseRow();
HBaseCell hCell = new HBaseCell();
if (message != null) {
Object id = message.getHeader(HBaseAttribute.HBASE_ROW_ID.asHeader(index));
String rowClassName = message.getHeader(HBaseAttribute.HBASE_ROW_TYPE.asHeader(index), String.class);
Class> rowClass = rowClassName == null || rowClassName.isEmpty()
? String.class : message.getExchange().getContext().getClassResolver().resolveClass(rowClassName);
String columnFamily = (String) message.getHeader(HBaseAttribute.HBASE_FAMILY.asHeader(index));
String columnName = (String) message.getHeader(HBaseAttribute.HBASE_QUALIFIER.asHeader(index));
Object value = message.getHeader(HBaseAttribute.HBASE_VALUE.asHeader(index));
String valueClassName = message.getHeader(HBaseAttribute.HBASE_VALUE_TYPE.asHeader(index), String.class);
Class> valueClass = valueClassName == null || valueClassName.isEmpty()
? String.class : message.getExchange().getContext().getClassResolver().resolveClass(valueClassName);
//Id can be accepted as null when using get, scan etc.
if (id == null && columnFamily == null && columnName == null) {
return null;
}
hRow.setId(id);
hRow.setRowType(rowClass);
if (columnFamily != null && columnName != null) {
hCell.setQualifier(columnName);
hCell.setFamily(columnFamily);
hCell.setValue(value);
// String is the default value type
hCell.setValueType((valueClass != null) ? valueClass : String.class);
hRow.getCells().add(hCell);
}
}
return hRow;
}
/**
* Resolves the cells that the {@link org.apache.camel.Exchange} refers to.
*/
@Override
public HBaseData resolveModel(Message message) {
int index = 1;
HBaseData data = new HBaseData();
//We use a LinkedHashMap to preserve the order.
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy