Java Code Examples for org.apache.pulsar.common.naming.TopicName#toString()

The following examples show how to use org.apache.pulsar.common.naming.TopicName#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebSocketProxyStatsBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private ProxyTopicStat getStat(TopicName topicName) {
    String topicNameStr = topicName.toString();
    if (!service().getProducers().containsKey(topicNameStr) && !service().getConsumers().containsKey(topicNameStr)
            && !service().getReaders().containsKey(topicNameStr)) {
        LOG.warn("topic doesn't exist {}", topicNameStr);
        throw new RestException(Status.NOT_FOUND, "Topic does not exist");
    }
    ProxyTopicStat topicStat = new ProxyTopicStat();
    if (service().getProducers().containsKey(topicNameStr)) {
        service().getProducers().get(topicNameStr).forEach(handler -> {
            ProducerStats stat = new ProducerStats(handler);
            topicStat.producerStats.add(stat);

        });
    }

    if (service().getConsumers().containsKey(topicNameStr)) {
        service().getConsumers().get(topicNameStr).forEach(handler -> {
            topicStat.consumerStats.add(new ConsumerStats(handler));
        });
    }

    if (service().getReaders().containsKey(topicNameStr)) {
        service().getReaders().get(topicNameStr).forEach(handler -> {
            topicStat.consumerStats.add(new ConsumerStats(handler));
        });
    }
    return topicStat;
}
 
Example 2
Source File: ReplicatorTestBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
MessageProducer(URL url, final TopicName dest) throws Exception {
    this.url = url;
    this.namespace = dest.getNamespace();
    this.topicName = dest.toString();
    client = PulsarClient.builder().serviceUrl(url.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    producer = client.newProducer()
        .topic(topicName)
        .enableBatching(false)
        .messageRoutingMode(MessageRoutingMode.SinglePartition)
        .create();

}
 
Example 3
Source File: ReplicatorTestBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
MessageProducer(URL url, final TopicName dest, boolean batch) throws Exception {
    this.url = url;
    this.namespace = dest.getNamespace();
    this.topicName = dest.toString();
    client = PulsarClient.builder().serviceUrl(url.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    ProducerBuilder<byte[]> producerBuilder = client.newProducer()
        .topic(topicName)
        .enableBatching(batch)
        .batchingMaxPublishDelay(1, TimeUnit.SECONDS)
        .batchingMaxMessages(5);
    producer = producerBuilder.create();

}
 
Example 4
Source File: ReplicatorTestBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
MessageConsumer(URL url, final TopicName dest, String subId) throws Exception {
    this.url = url;
    this.namespace = dest.getNamespace();
    this.topicName = dest.toString();

    client = PulsarClient.builder().serviceUrl(url.toString()).statsInterval(0, TimeUnit.SECONDS).build();

    try {
        consumer = client.newConsumer().topic(topicName).subscriptionName(subId).subscribe();
    } catch (Exception e) {
        client.close();
        throw e;
    }
}
 
Example 5
Source File: PulsarSchemaHandlers.java    From pulsar with Apache License 2.0 5 votes vote down vote up
static SchemaHandler newPulsarSchemaHandler(TopicName topicName,
                                            PulsarConnectorConfig pulsarConnectorConfig,
                                            SchemaInfo schemaInfo,
                                            List<PulsarColumnHandle> columnHandles) throws RuntimeException{
    if (schemaInfo.getType().isPrimitive()) {
        return new PulsarPrimitiveSchemaHandler(schemaInfo);
    } else if (schemaInfo.getType().isStruct()) {
        try {
            switch (schemaInfo.getType()) {
                case JSON:
                    return new JSONSchemaHandler(columnHandles);
                case AVRO:
                    return new AvroSchemaHandler(topicName, pulsarConnectorConfig, schemaInfo, columnHandles);
                default:
                    throw new PrestoException(NOT_SUPPORTED, "Not supported schema type: " + schemaInfo.getType());
            }
        } catch (PulsarClientException e) {
            throw new RuntimeException(
                    new Throwable("PulsarAdmin gets version schema fail, topicName : "
                            + topicName.toString(), e));
        }
    } else if (schemaInfo.getType().equals(SchemaType.KEY_VALUE)) {
        return new KeyValueSchemaHandler(topicName, pulsarConnectorConfig, schemaInfo, columnHandles);
    } else {
        throw new PrestoException(
                NOT_SUPPORTED,
                "Schema `" + schemaInfo.getType() + "` is not supported by presto yet : " + schemaInfo);
    }

}
 
Example 6
Source File: PulsarMetadata.java    From pulsar with Apache License 2.0 5 votes vote down vote up
static List<ColumnMetadata> getPulsarColumnsFromStructSchema(TopicName topicName,
                                                             SchemaInfo schemaInfo,
                                                             boolean withInternalColumns,
                                 PulsarColumnHandle.HandleKeyValueType handleKeyValueType) {
    String schemaJson = new String(schemaInfo.getSchema());
    if (StringUtils.isBlank(schemaJson)) {
        throw new PrestoException(NOT_SUPPORTED, "Topic " + topicName.toString()
                + " does not have a valid schema");
    }
    Schema schema;
    try {
        schema = PulsarConnectorUtils.parseSchema(schemaJson);
    } catch (SchemaParseException ex) {
        throw new PrestoException(NOT_SUPPORTED, "Topic " + topicName.toString()
                + " does not have a valid schema");
    }

    ImmutableList.Builder<ColumnMetadata> builder = ImmutableList.builder();

    builder.addAll(getColumns(null, schema, new HashSet<>(), new Stack<>(), new Stack<>(), handleKeyValueType));

    if (withInternalColumns) {
        PulsarInternalColumn.getInternalFields()
                .stream()
                .forEach(pulsarInternalColumn -> builder.add(pulsarInternalColumn.getColumnMetadata(false)));
    }
     return builder.build();
}
 
Example 7
Source File: CliCommand.java    From pulsar with Apache License 2.0 5 votes vote down vote up
static String validatePersistentTopic(List<String> params) {
    String topic = checkArgument(params);
    TopicName topicName = TopicName.get(topic);
    if (topicName.getDomain() != TopicDomain.persistent) {
        throw new ParameterException("Need to provide a persistent topic name");
    }
    return topicName.toString();
}
 
Example 8
Source File: CliCommand.java    From pulsar with Apache License 2.0 5 votes vote down vote up
static String validateNonPersistentTopic(List<String> params) {
    String topic = checkArgument(params);
    TopicName topicName = TopicName.get(topic);
    if (topicName.getDomain() != TopicDomain.non_persistent) {
        throw new ParameterException("Need to provide a non-persistent topic name");
    }
    return topicName.toString();
}
 
Example 9
Source File: MessageIdImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public static MessageId fromByteArrayWithTopic(byte[] data, TopicName topicName) throws IOException {
    checkNotNull(data);
    ByteBufCodedInputStream inputStream = ByteBufCodedInputStream.get(Unpooled.wrappedBuffer(data, 0, data.length));
    PulsarApi.MessageIdData.Builder builder = PulsarApi.MessageIdData.newBuilder();

    PulsarApi.MessageIdData idData;
    try {
        idData = builder.mergeFrom(inputStream, null).build();
    } catch (UninitializedMessageException e) {
        throw new IOException(e);
    }

    MessageId messageId;
    if (idData.hasBatchIndex()) {
        messageId = new BatchMessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition(),
                idData.getBatchIndex());
    } else {
        messageId = new MessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition());
    }
    if (idData.getPartition() > -1 && topicName != null) {
        messageId = new TopicMessageIdImpl(
                topicName.getPartition(idData.getPartition()).toString(), topicName.toString(), messageId);
    }

    inputStream.recycle();
    builder.recycle();
    idData.recycle();
    return messageId;
}
 
Example 10
Source File: PulsarMetadataReader.java    From pulsar-flink with Apache License 2.0 4 votes vote down vote up
public static String objectPath2TopicName(ObjectPath objectPath) {
    NamespaceName ns = NamespaceName.get(objectPath.getDatabaseName());
    String topic = objectPath.getObjectName();
    TopicName fullName = TopicName.get(TopicDomain.persistent.toString(), ns, topic);
    return fullName.toString();
}
 
Example 11
Source File: NamespaceServiceTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testSplitMapWithRefreshedStatMap() throws Exception {

    OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());

    ManagedLedger ledger = mock(ManagedLedger.class);
    when(ledger.getCursors()).thenReturn(Lists.newArrayList());

    doReturn(CompletableFuture.completedFuture(null)).when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
    Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
    ownership.setAccessible(true);
    ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);

    NamespaceService namespaceService = pulsar.getNamespaceService();
    NamespaceName nsname = NamespaceName.get("pulsar/global/ns1");
    TopicName topicName = TopicName.get("persistent://pulsar/global/ns1/topic-1");
    NamespaceBundles bundles = namespaceService.getNamespaceBundleFactory().getBundles(nsname);
    NamespaceBundle originalBundle = bundles.findBundle(topicName);

    PersistentTopic topic = new PersistentTopic(topicName.toString(), ledger, pulsar.getBrokerService());
    Method method = pulsar.getBrokerService().getClass().getDeclaredMethod("addTopicToStatsMaps",
            TopicName.class, Topic.class);
    method.setAccessible(true);
    method.invoke(pulsar.getBrokerService(), topicName, topic);
    String nspace = originalBundle.getNamespaceObject().toString();
    List<Topic> list = this.pulsar.getBrokerService().getAllTopicsFromNamespaceBundle(nspace,
            originalBundle.toString());
    assertNotNull(list);

    // Split bundle and take ownership of split bundles
    CompletableFuture<Void> result = namespaceService.splitAndOwnBundle(originalBundle, false, NamespaceBundleSplitAlgorithm.rangeEquallyDivide);
    try {
        result.get();
    } catch (Exception e) {
        // make sure: no failure
        fail("split bundle failed", e);
    }

    // old bundle should be removed from status-map
    list = this.pulsar.getBrokerService().getAllTopicsFromNamespaceBundle(nspace, originalBundle.toString());
    assertTrue(list.isEmpty());

    // status-map should be updated with new split bundles
    NamespaceBundle splitBundle = pulsar.getNamespaceService().getBundle(topicName);
    assertFalse(CollectionUtils.isEmpty(
        this.pulsar.getBrokerService()
            .getAllTopicsFromNamespaceBundle(nspace, splitBundle.toString())));

}
 
Example 12
Source File: TestPulsarMetadata.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "rewriteNamespaceDelimiter", singleThreaded = true)
public void testGetTableMetadata(String delimiter) {
    updateRewriteNamespaceDelimiterIfNeeded(delimiter);
    List<TopicName> allTopics = new LinkedList<>();
    allTopics.addAll(topicNames.stream().filter(topicName -> !topicName.equals(NON_SCHEMA_TOPIC)).collect(Collectors.toList()));
    allTopics.addAll(partitionedTopicNames);

    for (TopicName topic : allTopics) {
        PulsarTableHandle pulsarTableHandle = new PulsarTableHandle(
                topic.toString(),
                topic.getNamespace(),
                topic.getLocalName(),
                topic.getLocalName()
        );

        ConnectorTableMetadata tableMetadata = this.pulsarMetadata.getTableMetadata(mock(ConnectorSession.class),
                pulsarTableHandle);

        assertEquals(tableMetadata.getTable().getSchemaName(), topic.getNamespace());
        assertEquals(tableMetadata.getTable().getTableName(), topic.getLocalName());
        assertEquals(tableMetadata.getColumns().size(),
                fooColumnHandles.size());

        List<String> fieldNames = new LinkedList<>(fooFieldNames.keySet());

        for (PulsarInternalColumn internalField : PulsarInternalColumn.getInternalFields()) {
            fieldNames.add(internalField.getName());
        }

        for (ColumnMetadata column : tableMetadata.getColumns()) {
            if (PulsarInternalColumn.getInternalFieldsMap().containsKey(column.getName())) {
                assertEquals(column.getComment(),
                        PulsarInternalColumn.getInternalFieldsMap()
                                .get(column.getName()).getColumnMetadata(true).getComment());
            }

            fieldNames.remove(column.getName());
        }

        assertTrue(fieldNames.isEmpty());
    }
}