Skip to content

Instantly share code, notes, and snippets.

@unclebean
Last active February 18, 2025 04:49
Show Gist options
  • Save unclebean/a66854865c8ffa31d1210da2c608e11a to your computer and use it in GitHub Desktop.
Save unclebean/a66854865c8ffa31d1210da2c608e11a to your computer and use it in GitHub Desktop.
event hub
import com.azure.core.credential.AzureKeyCredential;
import com.azure.search.documents.SearchClient;
import com.azure.search.documents.SearchClientBuilder;
import com.azure.search.documents.models.IndexDocumentsBatch;
import com.azure.search.documents.models.IndexDocumentsResult;
import com.azure.search.documents.models.IndexDocumentsAction;
import com.azure.search.documents.models.IndexActionType;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Service
public class AzureSearchService {
private static final String SEARCH_SERVICE_NAME = "your-search-service-name";
private static final String INDEX_NAME = "your-index-name";
private static final String API_KEY = "your-api-key";
private final SearchClient searchClient;
public AzureSearchService() {
this.searchClient = new SearchClientBuilder()
.endpoint("https://" + SEARCH_SERVICE_NAME + ".search.windows.net")
.credential(new AzureKeyCredential(API_KEY))
.indexName(INDEX_NAME)
.buildClient();
}
public void insertOrUpdateMetadata() {
// Sample metadata document
Map<String, Object> document = new HashMap<>();
document.put("id", "123"); // Unique ID
document.put("title", "Azure AI Search Guide");
document.put("tags", Arrays.asList("azure", "search", "metadata"));
document.put("description", "Updating metadata in Azure AI Search.");
document.put("category", "Cloud Services");
// Creating an index document action
IndexDocumentsAction<Map<String, Object>> action = new IndexDocumentsAction<>(
IndexActionType.MERGE_OR_UPLOAD, document);
// Batch update
IndexDocumentsBatch<Map<String, Object>> batch = new IndexDocumentsBatch<>();
batch.addActions(Arrays.asList(action));
// Execute the batch update
IndexDocumentsResult result = searchClient.indexDocuments(batch);
System.out.println("Metadata updated successfully: " + result.getResults());
}
}
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/eventgrid")
public class BlobEventController {
@PostMapping("/blob-events")
public ResponseEntity<Map<String, String>> handleEventGridEvent(@RequestBody List<Map<String, Object>> events) {
for (Map<String, Object> event : events) {
String eventType = (String) event.get("eventType");
// Handle Event Grid Validation Event
if ("Microsoft.EventGrid.SubscriptionValidationEvent".equals(eventType)) {
Map<String, Object> data = (Map<String, Object>) event.get("data");
String validationCode = (String) data.get("validationCode");
Map<String, String> response = new HashMap<>();
response.put("validationResponse", validationCode);
return ResponseEntity.ok(response); // Respond to complete validation
}
// Handle Blob Created Event
if ("Microsoft.Storage.BlobCreated".equals(eventType)) {
Map<String, Object> data = (Map<String, Object>) event.get("data");
String blobUrl = (String) data.get("url");
System.out.println("Blob Created: " + blobUrl);
}
}
return ResponseEntity.ok(Map.of("status", "Event processed successfully!"));
}
}
import com.azure.messaging.eventhubs.EventData;
import com.azure.messaging.eventhubs.EventHubConsumerAsyncClient;
import com.azure.messaging.eventhubs.EventHubClientBuilder;
import com.azure.messaging.eventhubs.models.EventContext;
import reactor.core.Disposable;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component
public class EventHubConsumer {
private EventHubConsumerAsyncClient consumer;
private Disposable subscription;
@PostConstruct
public void start() {
// Replace with your actual connection string and event hub name.
String connectionString = "Endpoint=sb://<YOUR_NAMESPACE>.servicebus.windows.net/;SharedAccessKeyName=<KEY_NAME>;SharedAccessKey=<KEY_VALUE>;EntityPath=your-eventhub-name";
String consumerGroup = "$Default"; // or your consumer group
consumer = new EventHubClientBuilder()
.connectionString(connectionString)
.consumerGroup(consumerGroup)
.buildAsyncConsumerClient();
subscription = consumer.receive(false)
.subscribe(eventContext -> handleEvent(eventContext),
error -> System.err.println("Error receiving events: " + error));
}
private void handleEvent(EventContext eventContext) {
EventData eventData = eventContext.getData();
String eventBody = eventData.getBodyAsString();
System.out.println("Received event: " + eventBody);
// TODO: Process the event, e.g., check if it indicates a blob update.
// You may parse JSON and check the eventType property.
}
@PreDestroy
public void shutdown() {
if (subscription != null) {
subscription.dispose();
}
if (consumer != null) {
consumer.close();
}
}
}
package com.example.eventhub;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.messaging.eventhubs.EventData;
import com.azure.messaging.eventhubs.EventHubProducerAsyncClient;
import com.azure.messaging.eventhubs.EventHubClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import java.util.Collections;
@Service
public class EventHubProducerService {
private final EventHubProducerAsyncClient eventHubProducerClient;
public EventHubProducerService(
@Value("${azure.eventhub.namespace}") String namespace,
@Value("${azure.eventhub.hub-name}") String hubName) {
String fullyQualifiedNamespace = namespace + ".servicebus.windows.net";
this.eventHubProducerClient = new EventHubClientBuilder()
.fullyQualifiedNamespace(fullyQualifiedNamespace)
.eventHubName(hubName)
.credential(new DefaultAzureCredentialBuilder().build()) // Uses Managed Identity
.buildAsyncProducerClient();
}
public Mono<Void> sendMessage(String message) {
EventData eventData = new EventData(message);
return eventHubProducerClient.send(Collections.singletonList(eventData))
.doOnSuccess(unused -> System.out.println("Message sent successfully"))
.doOnError(error -> System.err.println("Error sending message: " + error.getMessage()));
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.DeserializationFeature;
public class ObjectMapperConfig {
public static ObjectMapper createObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaTimeModule()) // Register support for OffsetDateTime
.registerModule(new ParameterNamesModule()) // Ensures Java records work correctly
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) // Ensures ISO date format
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // Ignore extra fields
return objectMapper;
}
}
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-eventhubs</artifactId>
<version>5.14.0</version> <!-- or the latest available version -->
</dependency>
az storage blob metadata update \
--account-name mystorageaccount \
--container-name mycontainer \
--name report2024.pdf \
--metadata reviewer=JaneSmith status=Approved
az storage blob upload \
--account-name mystorageaccount \
--container-name mycontainer \
--name report2024.pdf \
--file /path/to/report2024.pdf \
--metadata author=JohnDoe project=Finance year=2024
az eventgrid event-subscription update \
--name blob-event-subscription \
--source-resource-id /subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Storage/storageAccounts/{storage-account} \
--delivery-attribute-mapping name=classification type=Dynamic value=data.metadata.classification
import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class URLParser {
public static void main(String[] args) {
// Raw URL with illegal characters
String urlString = "http://storage.az.com/abc/213/^3424/^sfdas/aa.pdf";
try {
// Encode only illegal URL characters (not the full URL)
String safeUrlString = urlString.replace("^", "%5E") // Encode `^` manually
.replace(" ", "%20"); // Encode spaces if present
// Parse the URI
URI uri = new URI(safeUrlString);
// Get only the relevant path
String path = uri.getRawPath();
// Decode path for readability
path = URLDecoder.decode(path, StandardCharsets.UTF_8);
// Debug: Print extracted path
System.out.println("Relevant Path: " + path);
// Split path by "/"
String[] parts = path.split("/");
if (parts.length > 2) {
// Extract folder name
String folderName = parts[1];
// Extract file name
String fileName = path.substring(path.indexOf(folderName) + folderName.length() + 1);
// Sanitize file name (replace illegal characters for filesystem)
fileName = sanitizeFileName(fileName);
// Output results
System.out.println("Folder Name: " + folderName);
System.out.println("File Name: " + fileName);
} else {
System.out.println("Invalid URL format.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Function to sanitize file names for Windows/macOS/Linux
public static String sanitizeFileName(String fileName) {
return fileName.replaceAll("[\\\\/:*?\"<>|]", "_"); // Replaces illegal chars with "_"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment