File tree Expand file tree Collapse file tree 10 files changed +225
-1
lines changed Expand file tree Collapse file tree 10 files changed +225
-1
lines changed Original file line number Diff line number Diff line change 1+ using System;
2+ using System.Text.Json;
3+ using System.Net.Http;
4+ using System.Collections.Generic;
5+
6+ { {> snippets/import} }
7+
8+ class Program
9+ {
10+ public static void Main(string[] args)
11+ {
12+ // read json file from url
13+ var url = " https://dashboard.algolia.com/sample_datasets/movie.json" ;
14+ var httpClient = new HttpClient();
15+ var response = httpClient.GetAsync(url).Result;
16+ var content = response.Content.ReadAsStringAsync().Result;
17+
18+ // parse json
19+ var movies = JsonSerializer.Deserialize< List< dynamic>> (content);
20+
21+ // initiate client and index
22+ {{> snippets/init} }
23+
24+ // push data to algolia
25+ try
26+ {
27+ var result = {{#dynamicSnippet} }saveObjectsMovies{ {/dynamicSnippet} }.Result;
28+ }
29+ catch (Exception e)
30+ {
31+ Console.WriteLine(e.Message);
32+ }
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "encoding/json"
5+ "fmt"
6+ "net/http"
7+ )
8+
9+ { {> snippets/import} }
10+
11+ func main() {
12+ // read json file
13+ url := " https://dashboard.algolia.com/sample_datasets/movie.json"
14+ response, err := http.Get(url)
15+
16+ if err != nil {
17+ fmt.Println(" Could not open url" )
18+ return
19+ }
20+
21+ defer response.Body.Close()
22+
23+ // parse json file to Movie struct
24+ var movies []map[string]interface{ }
25+ err = json.NewDecoder(response.Body).Decode(&movies)
26+
27+ if err != nil {
28+ fmt.Println(" Could not decode body" )
29+ return
30+ }
31+
32+ // initiate client
33+ { {> snippets/init} }
34+
35+ // push data to algolia
36+ result, err := { {#dynamicSnippet} }saveObjectsMovies{ {/dynamicSnippet} }
37+ if err != nil {
38+ fmt.Println(err)
39+ return
40+ }
41+
42+ fmt.Printf("Done! Uploaded records in %d batches.", len(result))
43+ }
Original file line number Diff line number Diff line change 1+ import java.io.InputStream;
2+ import java.net.URI;
3+ import java.net.URL;
4+ import java.util.List;
5+
6+ { {> snippets/import} }
7+
8+ import com.fasterxml.jackson.core.type.TypeReference;
9+ import com.fasterxml.jackson.databind.JsonNode;
10+ import com.fasterxml.jackson.databind.ObjectMapper;
11+
12+ public class saveObjectsMovies {
13+ public static void main(String[] args) throws Exception {
14+ // Fetch sample dataset
15+ URL url = new URI(" https://dashboard.algolia.com/sample_datasets/movie.json" ).toURL();
16+ InputStream stream = url.openStream();
17+ ObjectMapper mapper = new ObjectMapper();
18+ List< JsonNode> movies = mapper.readValue(stream, new TypeReference< List< JsonNode>> () {} );
19+ stream.close();
20+
21+ // Connect and authenticate with your Algolia app
22+ { {> snippets/init} }
23+
24+ // Save records in Algolia index
25+ { {#dynamicSnippet} }saveObjectsMovies{ {/dynamicSnippet} };
26+ client.close();
27+ }
28+ }
Original file line number Diff line number Diff line change 55// Fetch and index objects in Algolia
66const processRecords = async () => {
77 const datasetRequest = await fetch (' https://dashboard.algolia.com/sample_datasets/movie.json' );
8- const objects = await datasetRequest.json();
8+ const movies = await datasetRequest.json();
99 return {{#dynamicSnippet} }saveObjectsMovies{ {/dynamicSnippet} }
1010};
1111
Original file line number Diff line number Diff line change 1+ package org.example
2+ { {> snippets/import} }
3+ import kotlinx.serialization.builtins.ListSerializer
4+ import kotlinx.serialization.json.Json
5+ import kotlinx.serialization.json.JsonObject
6+ import java.net.URI
7+
8+ suspend fun main() {
9+ val url = URI.create(" https://dashboard.algolia.com/sample_datasets/movie.json" )
10+ val json = url.toURL().readText()
11+ val movies: List< JsonObject> = Json.decodeFromString(ListSerializer(JsonObject.serializer()), json)
12+
13+ {{> snippets/init} }
14+
15+ try {
16+ client.{{#dynamicSnippet} }saveObjectsMovies{ {/dynamicSnippet} }
17+ } catch(e: Exception) {
18+ println(e.message)
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ require(__DIR__."/vendor/autoload.php");
3+ { {> snippets/import} }
4+
5+ // Fetch sample dataset
6+ $url = "https://dashboard.algolia.com/sample_datasets/movie.json";
7+ $response = file_get_contents($url);
8+ $movies = json_decode($response, true);
9+
10+ // Connect and authenticate with your Algolia app
11+ { {> snippets/init} }
12+
13+ // Save records in Algolia index
14+ { {#dynamicSnippet} }saveObjectsMovies{ {/dynamicSnippet} }
15+
16+ print('Done!');
Original file line number Diff line number Diff line change 1+ import requests
2+ { {> snippets/import} }
3+
4+ # Fetch sample dataset
5+ url = "https://dashboard.algolia.com/sample_datasets/movie.json"
6+ movies = requests.get(url).json()
7+
8+ # Connect and authenticate with your Algolia app
9+ { {> snippets/init} }
10+
11+ # Save records in Algolia index
12+ { {#dynamicSnippet} }saveObjectsMovies{ {/dynamicSnippet} }
Original file line number Diff line number Diff line change 1+ require 'uri'
2+ { {> snippets/import} }
3+
4+ # Fetch sample dataset
5+ uri = URI('https://dashboard.algolia.com/sample_datasets/movie.json')
6+ response = Net::HTTP.get_response(uri)
7+ movies = JSON.parse(response.body)
8+
9+ # Connect and authenticate with your Algolia app
10+ { {> snippets/init} }
11+
12+ # Save records in Algolia index
13+ { {#dynamicSnippet} }saveObjectsMovies{ {/dynamicSnippet} }
14+
15+ puts('Done!')
Original file line number Diff line number Diff line change 1+ import scala.io.Source
2+ import scala.concurrent.duration.Duration
3+ import scala.concurrent.{ Await, ExecutionContextExecutor}
4+
5+ { {> snippets/import} }
6+
7+ import org.json4s.native.JsonMethods
8+ import org.json4s.jvalue2extractable
9+
10+ object Main {
11+ def main(args: Array[String]): Unit = {
12+ implicit val ec: ExecutionContextExecutor = scala.concurrent.ExecutionContext.global
13+ implicit val formats: org.json4s.Formats = org.json4s.DefaultFormats
14+
15+ // Fetch sample dataset
16+ val url = " https://dashboard.algolia.com/sample_datasets/movie.json"
17+ val result = Source.fromURL(url).mkString
18+ val movies = JsonMethods.parse(result).extract[Seq[Map[String, Any]]]
19+
20+ // Connect and authenticate with your Algolia app
21+ {{> snippets/init} }
22+
23+ // Save records in Algolia index
24+ try {
25+ Await.result(
26+ {{#dynamicSnippet} }saveObjectsMovies{ {/dynamicSnippet} },
27+ Duration(100, "sec")
28+ )
29+ } catch {
30+ case e: Exception => println(e)
31+ }
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ import Foundation
2+ import Core
3+ { {> snippets/import} }
4+
5+ Task {
6+ let url = URL(string: " https://dashboard.algolia.com/sample_datasets/movie.json" )!
7+ let (data, _) = try await URLSession.shared.data(from: url)
8+ let movies = try JSONDecoder().decode([AnyCodable].self, from: data)
9+
10+ // Connect and authenticate with your Algolia app
11+ {{> snippets/init} }
12+
13+ do {
14+ // Save records in Algolia index
15+ _ = try await {{#dynamicSnippet} }saveObjectsMovies{ {/dynamicSnippet} }
16+ exit(EXIT_SUCCESS)
17+ } catch {
18+ print(error.localizedDescription)
19+ exit(EXIT_FAILURE)
20+ }
21+ }
22+
23+ RunLoop.current.run()
You can’t perform that action at this time.
0 commit comments